如果你是一名前端er,又想在移動設(shè)備上開發(fā)出自己的應(yīng)用,那怎么實現(xiàn)呢?幸好,webkit內(nèi)核的瀏覽器能幫助我們完成這一切。接觸 webkit webApp的開發(fā)已經(jīng)有一段時間了,現(xiàn)把一些技巧分享給大家 :
1. viewport:
也就是可視區(qū)域。對于桌面瀏覽器,我們都很清楚viewport是什么,就是出去了所有工具欄、狀態(tài)欄、滾動條等等之后用于看網(wǎng)頁的區(qū)域,
這是真正有效的區(qū)域。由于移動設(shè)備屏幕寬度不同于傳統(tǒng)web,因此我們需要改變viewport;
實際上我們可以操作的屬性有4 個:
width - // viewport 的寬度 (范圍從200 到10,000,默認(rèn)為980 像素)height - // viewport 的高度 (范圍從223 到10,000)initial-scale - // 初始的縮放比例 (范圍從>0 到10)minimum-scale - // 允許用戶縮放到的最小比例maximum-scale - // 允許用戶縮放到的最大比例user-scalable - // 用戶是否可以手動縮 (no,yes) |
那么到底這些設(shè)置如何讓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” /> // 離線應(yīng)用的另一個技巧 <meta name=”apple-mobile-web-app-status-bar-style” content=black” /> // 隱藏狀態(tài)欄 <meta content="black" name="apple-mobile-web-app-status-bar-style" /> //指定的iphone中safari頂端的狀態(tài)條的樣式 <meta content="telephone=no" name="format-detection" /> //告訴設(shè)備忽略將頁面中的數(shù)字識別為電話號碼 <meta name="Author" contect="Mr.He"/ > |
在設(shè)置了initial-scale=1 之后,我們終于可以以1:1 的比例進行頁面設(shè)計了。關(guān)于viewport,還有一個很重要的概念是:iphone 的safari 瀏覽器完全沒有滾動條,而且不是簡單的“隱藏滾動條”,是根本沒有這個功能。iphone 的safari 瀏覽器實際上從一開始就完整顯示了這個網(wǎng)頁,然后用viewport 查看其中的一部分。當(dāng)你用手指拖動時,其實拖的不是頁面,而是viewport。瀏覽器行為的改變不止是滾動條,交互事件也跟普通桌面不一樣。(請參考:指尖的下JS 系列文章)
2. link:
<link rel=”apple-touch-startup-image” href=”startup.png” /> // 設(shè)置開始頁面圖片<link rel=”apple-touch-icon” href=”iphon_tetris_icon.png”/> // 在設(shè)置書簽的時候可以顯示好看的圖標(biāo)<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"> // 肖像模式樣式 <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" // 風(fēng)景模式樣式//豎屏?xí)r使用的樣式 <style media="all and (orientation:portrait)" type="text/css">#landscape { display: none; }</style>//橫屏?xí)r使用的樣式 <style media="all and (orientation:landscape)" type="text/css">#portrait { display: none; }</style> |
3. 事件 : (請參考:指尖的下JS 系列文章)
// 手勢事件touchstart //當(dāng)手指接觸屏幕時觸發(fā)touchmove //當(dāng)已經(jīng)接觸屏幕的手指開始移動后觸發(fā)touchend //當(dāng)手指離開屏幕時觸發(fā)touchcancel// 觸摸事件gesturestart //當(dāng)兩個手指接觸屏幕時觸發(fā)gesturechange //當(dāng)兩個手指接觸屏幕后開始移動時觸發(fā)gestureend// 屏幕旋轉(zhuǎn)事件 onorientationchange // 檢測觸摸屏幕的手指何時改變方向 orientationchange // touch事件支持的相關(guān)屬性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. 屏幕旋轉(zhuǎn)事件:onorientationchange
添加屏幕旋轉(zhuǎn)事件偵聽,可隨時發(fā)現(xiàn)屏幕旋轉(zhuǎn)狀態(tài)(左旋、右旋還是沒旋)。例子:
// 判斷屏幕是否旋轉(zhuǎn)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("風(fēng)景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height); break; };<br>};// 添加事件監(jiān)聽 addEventListener('load', function(){ orientationChange(); window.onorientationchange = orientationChange; }); |
5. 隱藏地址欄 & 處理事件的時候,防止?jié)L動條出現(xiàn):
// 隱藏地址欄 & 處理事件的時候 ,防止?jié)L動條出現(xiàn)addEventListener('load', function(){ setTimeout(function(){ window.scrollTo(0, 1); }, 100); }); |
6. 雙手指滑動事件:
// 雙手指滑動事件addEventListener('load', function(){ window.onmousewheel = twoFingerScroll;}, false // 兼容各瀏覽器,表示在冒泡階段調(diào)用事件處理程序 (true 捕獲階段)); function twoFingerScroll(ev) { var delta =ev.wheelDelta/120; //對 delta 值進行判斷(比如正負(fù)) ,而后執(zhí)行相應(yīng)操作 return true; }; |
7. 判斷是否為iPhone:
// 判斷是否為 iPhone : function isAppleMobile() { return (navigator.platform.indexOf('iPad') != -1); }; |
8. localStorage:
例子 :(注意數(shù)據(jù)名稱 n 要用引號引起來)
var v = localStorage.getItem('n') ? localStorage.getItem('n') : ""; // 如果名稱是 n 的數(shù)據(jù)存在 ,則將其讀出 ,賦予變量 v 。 localStorage.setItem('n', v); // 寫入名稱為 n、值為 v 的數(shù)據(jù) localStorage.removeItem('n'); // 刪除名稱為 n 的數(shù)據(jù) |
9. 使用特殊鏈接:
如果你關(guān)閉自動識別后 ,又希望某些電話號碼能夠鏈接到 iPhone 的撥號功能 ,那么可以通過這樣來聲明電話鏈接 ,
<a href="tel:12345654321">打電話給我</a><a href="sms:12345654321">發(fā)短信</a>或用于單元格:<td onclick="location.href='tel:122'"> |
10. 自動大寫與自動修正
要關(guān)閉這兩項功能,可以通過autocapitalize 與autocorrect 這兩個選項:
<input type="text" autocapitalize="off" autocorrect="off" /> |
11. WebKit CSS:
①“盒模型”的具體描述性質(zhì)的包圍盒塊內(nèi)容,包括邊界,填充等等。
-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; //容器陰影(參數(shù)分別為:水平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; //內(nèi)置的CSS 表現(xiàn),暫時只支持push-button |
②“視覺格式化模型”描述性質(zhì),確定了位置和大小的塊元素。
direction: rtlunicode-bidi: bidi-override; 常量:bidi-override/embed/normal |
③“視覺效果”描述屬性,調(diào)整的視覺效果塊內(nèi)容,包括溢出行為,調(diào)整行為,能見度,動畫,變換,和過渡。
clip: rect(10px, 5px, 10px, 5px)resize: auto; 常量:auto/both/horizontal/none/verticalvisibility: visible; 常量: collapse/hidden/visible-webkit-transition: opacity 1s linear; 動畫效果 ease/linear/ease-in/ease-out/ease-in-out-webkit-backface-visibility: visibler; 常量:visible(默認(rèn)值)/hidden-webkit-box-reflect: right 1px; 鏡向反轉(zhuǎn)-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(默認(rèn))-webkit-perspective-origin: left top;-webkit-transform: rotate(5deg);-webkit-transform-style: preserve-3d; 常量:flat/preserve-3d; (2D 與3D) |
④“生成的內(nèi)容,自動編號,并列出”描述屬性,允許您更改內(nèi)容的一個組成部分,創(chuàng)建自動編號的章節(jié)和標(biāo)題,和操縱的風(fēng)格清單的內(nèi)容。
content: “Item” counter(section) ” “;This resets the counter.First section>two sectionthree sectioncounter-increment: section 1;counter-reset: section; |
⑤“分頁媒體”描述性能與外觀的屬性,控制印刷版本的網(wǎng)頁,如分頁符的行為。
page-break-after: auto; 常量:always/auto/avoid/left/rightpage-break-before: auto; 常量:always/auto/avoid/left/rightpage-break-inside: auto; 常量:auto/avoid |
⑥“顏色和背景”描述屬性控制背景下的塊級元素和顏色的文本內(nèi)容的組成部分。
-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 |
⑦ “字型”的具體描述性質(zhì)的文字字體的選擇范圍內(nèi)的一個因素。報告還描述屬性用于下載字體定義。
unicode-range: U+00-FF, U+980-9FF; |
⑧“文本”描述屬性的特定文字樣式,間距和自動滾屏。
text-shadow: #00FFFC 10px 10px 5px;text-transform: capitalize; 常量:capitalize/lowercase/none/uppercaseword-wrap: break-word; 常量:break-word/normal-webkit-marquee: right large infinite normal 10s; 常量:direction(方向) increment(迭代次數(shù)) repetition(重復(fù)) 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 |
⑨“表格”描述的布局和設(shè)計性能表的具體內(nèi)容。
-webkit-border-horizontal-spacing: 2px;-webkit-border-vertical-spacing: 2px;-webkit-column-break-after: right; 常量:always/auto/avoid/left/right-webkit-column-break-before: right; 常量:always/auto/avoid/left/right–webkit-column-break-inside: logical; 常量:avoid/auto-webkit-column-count: 3; //分欄-webkit-column-rule: 1px solid #fff;style:dashed,dotted,double,groove,hidden,inset,none,outset,ridge,solid |
⑩“用戶界面”描述屬性,涉及到用戶界面元素在瀏覽器中,如滾動文字區(qū),滾動條,等等。報告還描述屬性,范圍以外的網(wǎng)頁內(nèi)容,如光標(biāo)的標(biāo)注樣式和顯示當(dāng)您按住觸摸觸摸
目標(biāo),如在iPhone上的鏈接。
-webkit-box-align: baseline,center,end,start,stretch 常量:baseline/center/end/start/stretch-webkit-box-direction: normal;常量:normal/reverse-webkit-box-flex: flex_valuet-webkit-box-flex-group: group_number-webkit-box-lines: multiple; 常量:multiple/single-webkit-box-ordinal-group: group_number-webkit-box-orient: block-axis; 常量:block-axis/horizontal/inline-axis/vertical/orientation–webkit-box-pack: alignment; 常量:center/end/justify/start |
12. 動畫過渡
這是 Webkit 中最具創(chuàng)新力的特性:使用過渡函數(shù)定義動畫。
-webkit-animation: title infinite ease-in-out 3s;animation 有這幾個屬性:-webkit-animation-name: //屬性名,就是我們定義的keyframes-webkit-animation-duration:3s //持續(xù)時間-webkit-animation-timing-function: //過渡類型:ease/ linear(線性) /ease-in(慢到快)/ease-out(快到慢) /ease-in-out(慢到快再到慢) /cubic-bezier-webkit-animation-delay:10ms //動畫延遲(默認(rèn)0)-webkit-animation-iteration-count: //循環(huán)次數(shù)(默認(rèn)1),infinite 為無限-webkit-animation-direction: //動畫方式:normal(默認(rèn) 正向播放); alternate(交替方向,第偶數(shù)次正向播放,第奇數(shù)次反向播放) |
這些同樣是可以簡寫的。但真正讓我覺的很爽的是keyframes,它能定義一個動畫的轉(zhuǎn)變過程供調(diào)用,過程為0%到100%或from(0%)到to(100%)。簡單點說,只要你有想法,你想讓元素在這個過程中以什么樣的方式改變都是很簡單的。
-webkit-transform: 類型(縮放scale/旋轉(zhuǎn)rotate/傾斜skew/位移translate)scale(num,num) 放大倍率。scaleX 和 scaleY(3),可以簡寫為:scale(* , *)rotate(*deg) 轉(zhuǎn)動角度。rotateX 和 rotateY,可以簡寫為:rotate(* , *)Skew(*deg) 傾斜角度。skewX 和skewY,可簡寫為:skew(* , *)translate(*,*) 坐標(biāo)移動。translateX 和translateY,可簡寫為:translate(* , *)。 |
實現(xiàn)模擬彈出消息框(Alert)的例子:
①定義過渡(在<style type="text/css">段中描述keyframes):
@-webkit-keyframes DivZoom{0% { -webkit-transform: scale(0.01) }60% { -webkit-transform: scale(1.05) }80% { -webkit-transform: scale(0.95) }100% { -webkit-transform: scale(1.00) }}.sZoom { -webkit-animation: DivZoom 0.5s ease-in-out } |
(很容易看懂,將元素從縮小的0.01 倍--很小但不能為0 倍,放大到1.05 倍,再縮小到0.95倍,最后到1 倍即正常大小。整個過渡過程事件為0.5 秒,動畫方式為ease-in-out,即慢到快再到慢,默認(rèn)只進行1 次過渡。這正是大家經(jīng)常看到的 iPhone 彈出的提示信息的動畫效果?。?br />②定義元素(在<body>段中):
<div id="layerH" style="-webkit-border-radius:12px; border:2px solid #FFF;-webkit-box-shadow: 0px 2px 4px #888;position: absolute; left: 24px; top: 106px;<br>width: 256px; height: 268px; padding-left: 8px; padding-right: 8px;color: #FFFFFF; text-shadow: 1px 1px 1px #000; text-align: center;background-color: RGBA(32,48,96,0.9);background-image:url('BG-Msg.png'); background-repeat:no-repeat;z-index: 1; visibility: hidden; "><p><span style="font-size: 16pt; font-weight: bold">使用說明</span></p><hr noshade size="1"><div id="HelpText" style="height: 120px">說明文字</div><hr noshade size="1"><form name="formV" method="POST"><input type="button" value="確認(rèn)" name="B1"style="width: 100%; height: 40px; font-size: 14pt; ont-weight: bold;color: #FFFFFF; text-shadow: 0px -1px 1px #000;"onclick=" layerH.style.visibility='hidden'"></form></div> |
③啟動動畫(在 javascript 定義的函數(shù)中)
function pHelp(){layerH.style.visibility = 'visible'layerH.style.cssText = "-webkit-animation-delay: " + Math.random() + "ms"layerH.className = 'sZoom'} |
(這個啟動函數(shù)就很好理解了。但是為什么要使用-webkit-animation-delay 這句呢?因為當(dāng)一個元素過渡顯示完成后,若其樣式?jīng)]有變化,下一次將無法進行過渡動畫顯示。我們巧妙的利用其動畫延遲時間定義,使其有所變化,就避免了上述問題。其中使用隨機數(shù)函數(shù)Math.random(),產(chǎn)生一個大于0 小于1 的隨機數(shù)。當(dāng)然,延遲零點幾毫秒,用戶是不會察覺的。)
補充:
1. 鎖定 viewport
ontouchmove="event.preventDefault()" //鎖定viewport,任何屏幕操作不移動用戶界面(彈出鍵盤除外)。 |
2. 被點擊元素的外觀變化,可以使用樣式來設(shè)定:
-webkit-tap-highlight-color: 顏色 |
3. 偵測iPhone/iPod
開發(fā)特定設(shè)備的移動網(wǎng)站,首先要做的就是設(shè)備偵測了。下面是使用Javascript偵測iPhone/iPod的UA,然后轉(zhuǎn)向到專屬的URL。
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { if (document.cookie.indexOf("iphone_redirect=false") == -1) { window.location = "http://m.example.com"; }} |
雖然Javascript是可以在水果設(shè)備上運行的,但是用戶還是可以禁用。它也會造成客戶端刷新和額外的數(shù)據(jù)傳輸,所以下面是服務(wù)器端偵測和轉(zhuǎn)向:
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { header('Location: http://yoursite.com/iphone'); exit();} |
4. 阻止旋轉(zhuǎn)屏幕時自動調(diào)整字體大小
html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 {-webkit-text-size-adjust:none;} |
5. iPhone才識別的CSS
如果不想設(shè)備偵測,可以用CSS媒體查詢來專為iPhone/iPod定義樣式。
@media screen and (max-device-width: 480px) {} |
6. 縮小圖片
網(wǎng)站的大圖通常寬度都超過480像素,如果用前面的代碼限制了縮放,這些圖片在iPhone版顯示顯然會超過屏幕。好在iPhone機能還夠,我們可以用CSS讓iPhone自動將大圖片縮小顯示。
@media screen and (max-device-width: 480px){ img{max-width:100%;height:auto;}} |
7. 模擬:hover偽類
因為iPhone并沒有鼠標(biāo)指針,所以沒有hover事件。那么CSS :hover偽類就沒用了。但是iPhone有Touch事件,onTouchStart 類似 onMouseOver,onTouchEnd 類似 onMouseOut。所以我們可以用它來模擬hover。使用Javascript:
var myLinks = document.getElementsByTagName('a');for(var i = 0; i < myLinks.length; i++){ myLinks[i].addEventListener(’touchstart’, function(){this.className = “hover”;}, false); myLinks[i].addEventListener(’touchend’, function(){this.className = “”;}, false);} |
然后用CSS增加hover效果:
a:hover, a.hover { /* 你的hover效果 */ } |
這樣設(shè)計一個鏈接,感覺可以更像按鈕。并且,這個模擬可以用在任何元素上。
上一篇:微信小程序省市聯(lián)動
下一篇:微信小程序-閱讀小程序demo