移動端開發經驗 javascript
一、300ms延遲由來css
300 毫秒延遲的主要緣由是解決雙擊縮放(double tap to zoom)。雙擊縮放,顧名思義,即用手指在屏幕上快速點擊兩次,iOS 自帶的 Safari 瀏覽器會將網頁縮放至原始比例。 那麼這和 300 毫秒延遲有什麼聯繫呢? 假定這麼一個場景。用戶在 iOS Safari 裏邊點擊了一個連接。因爲用戶能夠進行雙擊縮放或者雙擊滾動的操做,當用戶一次點擊屏幕以後,瀏覽器並不能馬上判斷用戶是確實要打開這個連接,仍是想要進行雙擊操做。所以,iOS Safari 就等待 300 毫秒,以判斷用戶是否再次點擊了屏幕。 鑑於iPhone的成功,其餘移動瀏覽器都複製了 iPhone Safari 瀏覽器的多數約定,包括雙擊縮放,幾乎如今全部的移動端瀏覽器都有這個功能。、html
二、解決方案java
(1)添加viewpoint meta標籤android
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
(2)FastClickios
https://github.com/ftlabs/fastclickgit
移動端事件觸發順序:在移動端,手指點擊一個元素,會通過:touchstart --> touchmove -> touchend -->click。github
fastclick.js的原理是:FastClick的實現原理是在檢測到touchend事件的時候,會經過DOM自定義事件當即出發模擬一個click事件,並把瀏覽器在300ms以後真正的click事件阻止掉。web
fastclick一樣能夠解決移動端點透現象。api
點透現象:當A/B兩個層上下z軸重疊,上層的A點擊後消失或移開(這一點很重要),而且B元素自己有默認click事件(如a標籤)或綁定了click事件。在這種狀況下,點擊A/B重疊的部分,就會出現點透的現象。點透現象的關鍵點:
A/B兩個層上下z軸重疊。
上層的A點擊後消失或移開。(這一點很重要)
B元素自己有默認click事件(如a標籤) 或 B綁定了click事件。
在以上狀況下,點擊A/B重疊的部分,就會出現點透的現象。
示例代碼:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>移動端點透現象</title> <style> * { margin: 0px; padding: 0px; } #div1 { /*紅色半透明遮蓋層A*/ width: 300px; height: 300px; background-color: rgba(255, 0, 0, 0.25); } #div2 { /*黃色內容層B*/ width: 240px; height: 240px; background-color: yellow; position: absolute; left: 30px; top: 30px; z-index: -1; } #console { /*綠色狀態輸出框*/ border: 1px solid green; position: absolute; top: 300px; width: 100%; } </style> </head> <body> <div id="div1"></div> <div id="div2"> <a href="https://www.baidu.com/">www.baidu.com</a> </div> <div id="console"></div> <script type="text/javascript"> var div1 = document.getElementById("div1"); var div2 = document.getElementById('div2'); function handle(e) { var tar = e.target, eve = e.type; console.log("target:" + tar.id + " event:" + eve) if(tar.id === "div1") { div1.style.display = "none"; } } div1.addEventListener("touchend", handle); div1.addEventListener("touchstart", handle); div2.addEventListener('click', handle); </script> </body> </html>
解決方法:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>移動端點透現象解決方法</title> <style> * { margin: 0px; padding: 0px; } #div1 { /*紅色半透明遮蓋層A*/ width: 300px; height: 300px; background-color: rgba(255, 0, 0, 0.25); } #div2 { /*黃色內容層B*/ width: 240px; height: 240px; background-color: yellow; position: absolute; left: 30px; top: 30px; z-index: -1; } #console { /*綠色狀態輸出框*/ border: 1px solid green; position: absolute; top: 300px; width: 100%; } </style> </head> <body> <div id="div1"></div> <div id="div2"> <a href="https://www.baidu.com/">www.baidu.com</a> </div> <div id="console"></div> <script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script> <script type="text/javascript"> if('addEventListener' in document) { document.addEventListener('DOMContentLoaded', function() { FastClick.attach(document.body); }, false); } var div1 = document.getElementById("div1"); var div2 = document.getElementById('div2'); function handle(e) { var tar = e.target, eve = e.type; console.log("target:" + tar.id + " event:" + eve) if(tar.id === "div1") { div1.style.display = "none"; } } div1.addEventListener("touchend", handle); div1.addEventListener("touchstart", handle); div2.addEventListener('click', handle); </script> </body> </html>
一、移動端頁面設置視口寬度等於設備寬度,並禁止縮放。
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
二、移動端頁面設置視口寬度等於定寬(如640px),並禁止縮放,經常使用於微信瀏覽器頁面。
<meta name="viewport" content="width=640,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
三、禁止將頁面中的數字識別爲電話號碼
<meta name="format-detection" content="telephone=no" />
四、忽略Android平臺中對郵箱地址的識別
<meta name="format-detection" content="email=no" />
五、當網站添加到主屏幕快速啓動方式,可隱藏地址欄,僅針對ios的safari
<meta name="apple-mobile-web-app-capable" content="yes" /> <!-- ios7.0版本之後,safari上已看不到效果 -->
六、將網站添加到主屏幕快速啓動方式,僅針對ios的safari頂端狀態條的樣式
<meta name="apple-mobile-web-app-status-bar-style" content="black" /> <!-- 可選default、black、black-translucent -->
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport"> 6 <meta content="yes" name="apple-mobile-web-app-capable"> 7 <meta content="black" name="apple-mobile-web-app-status-bar-style"> 8 <meta content="telephone=no" name="format-detection"> 9 <meta content="email=no" name="format-detection"> 10 <title>title</title> 11 <link rel="stylesheet" href="index.css"> 12 </head> 13 14 <body> 15 content... 16 </body> 17 18 </html>
一、禁止ios和android用戶選中文字
.css{-webkit-user-select:none}
二、禁止ios長按時觸發系統的菜單,禁止ios&android長按時下載圖片
.css{-webkit-touch-callout: none}
三、webkit去除表單元素的默認樣式
.css{-webkit-appearance:none;}
四、修改webkit表單輸入框placeholder的樣式
input::-webkit-input-placeholder{color:#AAAAAA;} input:focus::-webkit-input-placeholder{color:#EEEEEE;}
五、去除android a/button/input標籤被點擊時產生的邊框 & 去除ios a標籤被點擊時產生的半透明灰色背景
a,button,input{-webkit-tap-highlight-color:rgba(255,0,0,0);}
六、ios使用-webkit-text-size-adjust禁止調整字體大小
body{-webkit-text-size-adjust: 100%!important;}
七、android 上去掉語音輸入按鈕
input::-webkit-input-speech-button {display: none}
八、移動端定義字體,移動端沒有微軟雅黑字體
/* 移動端定義字體的代碼 */ body{font-family:Helvetica;}
九、禁用Webkit內核瀏覽器的文字大小調整功能。
-webkit-text-size-adjust: none;
十、去掉 input[type=search]
搜索框默認的叉號
1 input[type="search"]{ 2 -webkit-appearance:none; 3 } 4 input::-webkit-search-cancel-button { 5 display: none; 6 }
一、手機拍照和上傳圖片
1 <!-- 選擇照片 --> 2 <input type=file accept="image/*"> 3 <!-- 選擇視頻 --> 4 <input type=file accept="video/*">
二、取消input在ios下,輸入的時候英文首字母的默認大寫
<input autocapitalize="off" autocorrect="off" />
三、打電話和發短信
1 <a href="tel:0755-10086">打電話給:0755-10086</a> 2 <a href="sms:10086">發短信給: 10086</a>
1 /* hcysun */ 2 @charset "utf-8"; 3 /* reset */ 4 html{ 5 -webkit-text-size-adjust:none; 6 -webkit-user-select:none; 7 -webkit-touch-callout: none 8 font-family: Helvetica; 9 } 10 body{font-size:12px;} 11 body,h1,h2,h3,h4,h5,h6,p,dl,dd,ul,ol,pre,form,input,textarea,th,td,select{margin:0; padding:0; font-weight: normal;text-indent: 0;} 12 a,button,input,textarea,select{ background: none; -webkit-tap-highlight-color:rgba(255,0,0,0); outline:none; -webkit-appearance:none;} 13 em{font-style:normal} 14 li{list-style:none} 15 a{text-decoration:none;} 16 img{border:none; vertical-align:top;} 17 table{border-collapse:collapse;} 18 textarea{ resize:none; overflow:auto;} 19 /* end reset */
1 /* public */ 2 3 /* 清除浮動 */ 4 .clear { zoom:1; } 5 .clear:after { content:''; display:block; clear:both; } 6 7 /* 定義盒模型爲怪異和模型(寬高不受邊框影響) */ 8 .boxSiz{ 9 -webkit-box-sizing: border-box; 10 -moz-box-sizing: border-box; 11 -ms-box-sizing: border-box; 12 -o-box-sizing: border-box; 13 box-sizing: border-box; 14 } 15 16 /* 強制換行 */ 17 .toWrap{ 18 word-break: break-all; /* 只對英文起做用,以字母做爲換行依據。 */ 19 word-wrap: break-word; /* 只對英文起做用,以單詞做爲換行依據。*/ 20 white-space: pre-wrap; /* 只對中文起做用,強制換行。*/ 21 } 22 23 /* 禁止換行 */ 24 .noWrap{ 25 white-space:nowrap; 26 } 27 28 /* 禁止換行,超出省略號 */ 29 .noWrapEllipsis{ 30 white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 31 } 32 33 /* 多行顯示省略號,less寫法,@line是行數 */ 34 .ellipsisLn(@line) { 35 overflow: hidden; 36 text-overflow: ellipsis; 37 display: -webkit-box; 38 -webkit-box-orient: vertical; 39 -webkit-line-clamp: @line; 40 } 41 42 /* 1px 邊框解決方案,示例中設置上邊框,能夠調整 top、right、bottom、left 的值分別設置上下左右邊框 */ 43 #box2:after{ 44 content: " "; 45 position: absolute; 46 left: 0; 47 top: 0; 48 right: 0; 49 height: 1px; 50 border-top: 1px solid #000; 51 color: #C7C7C7; 52 transform-origin: 0 0; 53 transform: scaleY(0.5); 54 } 55 56 /* 文字兩端對齊 */ 57 .text-justify{ 58 text-align:justify; 59 text-justify:inter-ideograph; 60 } 61 62 /* 定義盒模型爲 flex佈局兼容寫法並讓內容水平垂直居中 */ 63 .flex-center{ 64 display: -webkit-box; 65 display: -moz-box; 66 display: -ms-flexbox; 67 display: -o-box; 68 display: box; 69 70 -webkit-box-pack: center; 71 -moz-box-pack: center; 72 -ms-flex-pack: center; 73 -o-box-pack: center; 74 box-pack: center; 75 76 -webkit-box-align: center; 77 -moz-box-align: center; 78 -ms-flex-align: center; 79 -o-box-align: center; 80 box-align: center; 81 } 82 83 /* public end */
一、定義彈性盒模型兼容寫法
1 /* 2 box 3 inline-box 4 */ 5 display: -webkit-box; 6 display: -moz-box; 7 display: -ms-flexbox; 8 display: -o-box; 9 display: box; 10 二、box-orient 定義盒模型內伸縮項目的佈局方向 11 12 /** 13 * vertical column 垂直 14 * horizontal row 水平 默認值 15 */ 16 -webkit-box-orient: horizontal; 17 -moz-box-orient: horizontal; 18 -ms-flex-direction: row; 19 -o-box-orient: horizontal; 20 box-orient: horizontal; 21 三、box-direction 定義盒模型內伸縮項目的正序(normal默認值)、倒敘(reverse) 22 23 /* Firefox */ 24 display:-moz-box; 25 -moz-box-direction:reverse; 26 /* Safari、Opera 以及 Chrome */ 27 display:-webkit-box; 28 -webkit-box-direction:reverse; 29 四、box-pack 對盒子水平富裕空間的管理 30 31 /* 32 start 33 end 34 center 35 justify 36 */ 37 -webkit-box-pack: center; 38 -moz-box-pack: center; 39 -ms-flex-pack: center; 40 -o-box-pack: center; 41 box-pack: center; 42 五、box-pack 對盒子垂直方向富裕空間的管理 43 44 /* 45 start 46 end 47 center 48 */ 49 /* box-align */ 50 -webkit-box-align: center; 51 -moz-box-align: center; 52 -ms-flex-align: center; 53 -o-box-align: center; 54 box-align: center; 55 六、定義伸縮項目的具體位置 56 57 /*-moz-box-ordinal-group:1;*/ /* Firefox */ 58 /*-webkit-box-ordinal-group:1;*/ /* Safari 和 Chrome */ 59 .box div:nth-of-type(1){-webkit-box-ordinal-group:1;} 60 .box div:nth-of-type(2){-webkit-box-ordinal-group:2;} 61 .box div:nth-of-type(3){-webkit-box-ordinal-group:3;} 62 .box div:nth-of-type(4){-webkit-box-ordinal-group:4;} 63 .box div:nth-of-type(5){-webkit-box-ordinal-group:5;} 64 七、定義伸縮項目佔空間的份數 65 66 -moz-box-flex:2.0; /* Firefox */ 67 -webkit-box-flex:2.0; /* Safari 和 Chrome */ 68 69 .box div:nth-of-type(1){-webkit-box-flex:1;} 70 .box div:nth-of-type(2){-webkit-box-flex:2;} 71 .box div:nth-of-type(3){-webkit-box-flex:3;} 72 .box div:nth-of-type(4){-webkit-box-flex:4;} 73 .box div:nth-of-type(5){-webkit-box-flex:5;}