1. viewport:
也就是可視區域。對于桌面瀏覽器,我們都很清楚viewport是什么,就是出去了所有工具欄、狀態欄、滾動條等等之后用于看網頁的區域,
這是真正有效的區域。由于移動設備屏幕寬度不同于傳統web,因此我們需要改變viewport;
實際上我們可以操作的屬性有4 個:
width - // viewport 的寬度 (范圍從200 到10,000,默認為980 像素)
height - // viewport 的高度 (范圍從223 到10,000)
initial-scale - // 初始的縮放比例 (范圍從>0 到10)
minimum-scale - // 允許用戶縮放到的最小比例
maximum-scale - // 允許用戶縮放到的最大比例
user-scalable - // 用戶是否可以手動縮 (no,yes)
那么到底這些設置如何讓Safari 知道?其實很簡單,就一個meta,形如:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> //編碼
<meta id="viewport" name="viewport" content="width=320; initial-scale=1.0;maximum-scale=1.0; user-scalable=no;"/>
<meta name=”apple-mobile-web-app-capable” content=”yes” /> // 離線應用的另一個技巧
<meta name=”apple-mobile-web-app-status-bar-style” content=black” /> // 隱藏狀態欄
<meta content="black" name="apple-mobile-web-app-status-bar-style" /> //指定的iphone中safari頂端的狀態條的樣式
<meta content="telephone=no" name="format-detection" /> //告訴設備忽略將頁面中的數字識別為電話號碼
<meta name="Author" contect="Mr.He"/ >
在設置了initial-scale=1 之后,我們終于可以以1:1 的比例進行頁面設計了。關于viewport,還有一個很重要的概念是:iphone 的safari 瀏覽器完全沒有滾動條,而且不是簡單的“隱藏滾動條”,是根本沒有這個功能。iphone 的safari 瀏覽器實際上從一開始就完整顯示了這個網頁,然后用viewport 查看其中的一部分。當你用手指拖動時,其實拖的不是頁面,而是viewport。瀏覽器行為的改變不止是滾動條,交互事件也跟普通桌面不一樣。2. link:
<link rel=”apple-touch-startup-image” href=”startup.png” /> // 設置開始頁面圖片
<link rel=”apple-touch-icon” href=”iphon_tetris_icon.png”/> // 在設置書簽的時候可以顯示好看的圖標
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"> // 肖像模式樣式
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" // 風景模式樣式
//豎屏時使用的樣式
<style media="all and (orientation:portrait)" type="text/css">
#landscape { display: none; }
</style>
//橫屏時使用的樣式
<style media="all and (orientation:landscape)" type="text/css">
#portrait { display: none; }
</style>
3. 事件 :
// 手勢事件
touchstart //當手指接觸屏幕時觸發
touchmove //當已經接觸屏幕的手指開始移動后觸發
touchend //當手指離開屏幕時觸發
touchcancel
// 觸摸事件
gesturestart //當兩個手指接觸屏幕時觸發
gesturechange //當兩個手指接觸屏幕后開始移動時觸發
gestureend
// 屏幕旋轉事件
onorientationchange
// 檢測觸摸屏幕的手指何時改變方向
orientationchange
// touch事件支持的相關屬性
touches
targetTouches
changedTouches
clientX // X coordinate of touch relative to the viewport (excludes scroll offset)
clientY // Y coordinate of touch relative to the viewport (excludes scroll offset)
screenX // Relative to the screen
screenY // Relative to the screen
pageX // Relative to the full page (includes scrolling)
pageY // Relative to the full page (includes scrolling)
target // Node the touch event originated from
identifier // An identifying number, unique to each touch event
4. 屏幕旋轉事件:onorientationchange
添加屏幕旋轉事件偵聽,可隨時發現屏幕旋轉狀態(左旋、右旋還是沒旋)。例子:
// 判斷屏幕是否旋轉
function orientationChange() {
switch(window.orientation) {
case 0:
alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);
break;
case -90:
alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);
break;
case 90:
alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);
break;
case 180:
alert("風景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);
break;
};<br>};
// 添加事件監聽
addEventListener('load', function(){
orientationChange();
window.onorientationchange = orientationChange;
});
5. 隱藏地址欄 & 處理事件的時候,防止滾動條出現:
// 隱藏地址欄 & 處理事件的時候 ,防止滾動條出現
addEventListener('load', function(){
setTimeout(function(){ window.scrollTo(0, 1); }, 100);
});
6. 雙手指滑動事件:
// 雙手指滑動事件
addEventListener('load', function(){ window.onmousewheel = twoFingerScroll;},
false // 兼容各瀏覽器,表示在冒泡階段調用事件處理程序 (true 捕獲階段)
);
function twoFingerScroll(ev) {
var delta =ev.wheelDelta/120; //對 delta 值進行判斷(比如正負) ,而后執行相應操作
return true;
};
7. 判斷是否為iPhone:
// 判斷是否為 iPhone :
function isAppleMobile() {
return (navigator.platform.indexOf('iPad') != -1);
};
8. localStorage:
例子 :(注意數據名稱 n 要用引號引起來)
var v = localStorage.getItem('n') ? localStorage.getItem('n') : ""; // 如果名稱是 n 的數據存在 ,則將其讀出 ,賦予變量 v 。
localStorage.setItem('n', v); // 寫入名稱為 n、值為 v 的數據
localStorage.removeItem('n'); // 刪除名稱為 n 的數據
9. 使用特殊鏈接:
如果你關閉自動識別后 ,又希望某些電話號碼能夠鏈接到 iPhone 的撥號功能 ,那么可以通過這樣來聲明電話鏈接 ,
<a href="tel:12345654321">打電話給我</a>
<a href="sms:12345654321">發短信</a>
或用于單元格:
<td onclick="location.href='tel:122'">
10. 自動大寫與自動修正
要關閉這兩項功能,可以通過autocapitalize 與autocorrect 這兩個選項:
<input type="text" autocapitalize="off" autocorrect="off" />
11. WebKit CSS:
①“盒模型”的具體描述性質的包圍盒塊內容,包括邊界,填充等等。
-webkit-border-bottom-left-radius: radius;
-webkit-border-top-left-radius: horizontal_radius vertical_radius;
-webkit-border-radius: radius; //容器圓角
-webkit-box-sizing: sizing_model; 邊框常量值:border-box/content-box
-webkit-box-shadow: hoff voff blur color; //容器陰影(參數分別為:水平X 方向偏移量;垂直Y 方向偏移量;高斯模糊半徑值;陰影顏色值)
-webkit-margin-bottom-collapse: collapse_behavior; 常量值:collapse/discard/separate
-webkit-margin-start: width;
-webkit-padding-start: width;
-webkit-border-image: url(borderimg.gif) 25 25 25 25 round/stretch round/stretch;
-webkit-appearance: push-button; //內置的CSS 表現,暫時只支持push-button
②“視覺格式化模型”描述性質,確定了位置和大小的塊元素。
direction: rtl
unicode-bidi: bidi-override; 常量:bidi-override/embed/normal
③“視覺效果”描述屬性,調整的視覺效果塊內容,包括溢出行為,調整行為,能見度,動畫,變換,和過渡。
clip: rect(10px, 5px, 10px, 5px)
resize: auto; 常量:auto/both/horizontal/none/vertical
visibility: visible; 常量: collapse/hidden/visible
-webkit-transition: opacity 1s linear; 動畫效果 ease/linear/ease-in/ease-out/ease-in-out
-webkit-backface-visibility: visibler; 常量:visible(默認值)/hidden
-webkit-box-reflect: right 1px; 鏡向反轉
-webkit-box-reflect: below 4px -webkit-gradient(linear, left top, left bottom,
from(transparent), color-stop(0.5, transparent), to(white));
-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));; //CSS 遮罩/蒙板效果
-webkit-mask-attachment: fixed; 常量:fixed/scroll
-webkit-perspective: value; 常量:none(默認)
-webkit-perspective-origin: left top;
-webkit-transform: rotate(5deg);
-webkit-transform-style: preserve-3d; 常量:flat/preserve-3d; (2D 與3D)
④“生成的內容,自動編號,并列出”描述屬性,允許您更改內容的一個組成部分,創建自動編號的章節和標題,和操縱的風格清單的內容。
content: “Item” counter(section) ” “;
This resets the counter.
First section
>two section
three section
counter-increment: section 1;
counter-reset: section;
⑤“分頁媒體”描述性能與外觀的屬性,控制印刷版本的網頁,如分頁符的行為。
page-break-after: auto; 常量:always/auto/avoid/left/right
page-break-before: auto; 常量:always/auto/avoid/left/right
page-break-inside: auto; 常量:auto/avoid
⑥“顏色和背景”描述屬性控制背景下的塊級元素和顏色的文本內容的組成部分。
-webkit-background-clip: content; 常量:border/content/padding/text
-webkit-background-origin: padding; 常量:border/content/padding/text
-webkit-background-size: 55px; 常量:length/length_x/length_y
⑦ “字型”的具體描述性質的文字字體的選擇范圍內的一個因素。報告還描述屬性用于下載字體定義。
unicode-range: U+00-FF, U+980-9FF;
⑧“文本”描述屬性的特定文字樣式,間距和自動滾屏。
text-shadow: #00FFFC 10px 10px 5px;
text-transform: capitalize; 常量:capitalize/lowercase/none/uppercase
word-wrap: break-word; 常量:break-word/normal
-webkit-marquee: right large infinite normal 10s; 常量:direction(方向) increment(迭代次數) repetition(重復) style(樣式) speed(速度);
-webkit-marquee-direction: ahead/auto/backwards/down/forwards/left/reverse/right/up
-webkit-marquee-incrementt: 1-n/infinite(無窮次)
-webkit-marquee-speed: fast/normal/slow
-webkit-marquee-style: alternate/none/scroll/slide
-webkit-text-fill-color: #ff6600; 常量:capitalize, lowercase, none, uppercase
-webkit-text-security: circle; 常量:circle/disc/none/square
-webkit-text-size-adjust: none; 常量:auto/none;
-webkit-text-stroke: 15px #fff;
-webkit-line-break: after-white-space; 常量:normal/after-white-space
-webkit-appearance: caps-lock-indicator;
-webkit-nbsp-mode: space; 常量: normal/space
-webkit-rtl-ordering: logical; 常量:visual/logical
-webkit-user-drag: element; 常量:element/auto/none
-webkit-user-modify: read- only; 常量:read-write-plaintext-only/read-write/read-only
-webkit-user-select: text; 常量:text/auto/none
⑨“表格”描述的布局和設計性能表的具體內容。