移動端開發(fā)注意點

禁止自動識別電話和android自動識別郵箱

<meta content="telephone=no" name="format-detection" />
<meta content="email=no" name="format-detection" />

使用無襯線字體

body {
    font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif;
}

禁止長按鏈接與圖片彈出菜單

a, img {
    -webkit-touch-callout: none; /* 禁止長按鏈接與圖片彈出菜單 */
}

去除A連接input標(biāo)簽,點擊出現(xiàn)自帶的陰影樣式

winphone系統(tǒng)
<meta name="msapplication-tap-highlight" content="no">

a,button,input,textarea {
    -webkit-tap-highlight-color: rgba(0,0,0,0); /*ios android去除自帶陰影的樣式*/
}

偽元素改變number類型input框的默認(rèn)樣式

input[type=number]::-webkit-textfield-decoration-container {
    background-color: transparent;
}
input[type=number]::-webkit-inner-spin-button {
     -webkit-appearance: none;
}
input[type=number]::-webkit-outer-spin-button {
     -webkit-appearance: none;
}

改變webkit表單輸入框placeholder的顏色

input::-webkit-input-placeholder{color:#AAAAAA;}
input:focus::-webkit-input-placeholder{color:#EEEEEE;}

單擊延遲

click 事件因為要等待雙擊確認(rèn),會有 300ms 的延遲,體驗并不是很好。
開發(fā)者大多數(shù)會使用封裝的 tap 事件來代替 click 事件, 所謂的 tap 事件由 touchstart 事件 + touchmove 判斷 + touchend 事件封裝組成。

手機拍照和上傳圖片

<input type="file">的 accept 屬性
<!-- 選擇照片 -->
<input type="file" accept="image/*">
<!-- 選擇視頻 -->
<input type="file" accept="video/*">

防止手機中網(wǎng)頁放大和縮小,這點是最基本的,最為手機網(wǎng)站開發(fā)者來說應(yīng)該都知道的,就是設(shè)置meta中的viewport

有些手機網(wǎng)站我們看到如下聲明:

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

設(shè)置了DTD的方式是XHTML的寫法,假如我們頁面運用的是html5,可以不用設(shè)置DTD,直接聲明<!DOCTYPE html>。

使用viewport使頁面禁止縮放。 通常把user-scalable設(shè)置為0來關(guān)閉用戶對頁面視圖縮放的行為。

完整的viewport設(shè)置,當(dāng)然,user-scalable=0,有的人也寫成user-scalable=no,都可以的。

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">

蘋果手機的一些設(shè)置。

<meta name="apple-mobile-web-app-capable" content="yes">

  • 如果content設(shè)置為yes,Web應(yīng)用會以全屏模式運行,反之,則不會。
  • content的默認(rèn)值是no,表示正常顯示。
  • 你可以通過只讀屬性window.navigator.standalone來確定網(wǎng)頁是否以全屏模式顯示。

format-detection設(shè)置。

<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">

format-detection 啟動或禁用自動識別頁面中的電話號碼、郵箱地址。

上下拉動滾動條時卡頓、慢

body {
    -webkit-overflow-scrolling: touch;
    overflow-scrolling: touch;
}

Android3+和iOS5+支持CSS3的新屬性為overflow-scrolling

禁止復(fù)制、選中文本

Element {
    -webkit-user-select: none;
    -moz-user-select: none;
    -khtml-user-select: none;
    user-select: none;
}

解決移動設(shè)備可選中頁面文本(視產(chǎn)品需要而定)

長時間按住頁面出現(xiàn)閃退

element {
    -webkit-touch-callout: none;
}

iphone及ipad下輸入框默認(rèn)內(nèi)陰影

Element{
    -webkit-appearance: none;
}

ios和android下觸摸元素時出現(xiàn)半透明灰色遮罩

Element {
    -webkit-tap-highlight-color:rgba(255,255,255,0)
}

audio元素和video元素在ios和andriod中無法自動播放

$('html').one('touchstart', function(){
    audio.play()
})

active兼容處理 即 偽類 :active 失效

方法一:body 添加 ontouchstart

<body ontouchstart="">

方法二:js給 document 綁定 touchstart 或 touchend 事件

<style>
a {
  color: #000;
}
a:active {
  color: #fff;
}
</style>
<a href="foo">bar</a>
<script>
  document.addEventListener(‘touchstart‘,function(){},false);
</script>

要做到全兼容的辦法,可通過綁定ontouchstartontouchend來控制按鈕的類名


.btn-blue {
    display:block;
    height:42px;
    color:#FFFFFF;
    font-size:18px;
    line-height:42px;
    border-radius:4px;
    text-align:center;
    background-color: #4185F3;
}

.btn-blue-on{
    background-color: #357AE8;
}

<div class="btn-blue">按鈕</div>

<script type="text/javascript">
    var btnBlue = document.querySelector(".btn-blue")

    btnBlue.ontouchstart = function() {
        this.className = "btn-blue btn-blue-on"
    }

    btnBlue.ontouchend = function() {
        this.className = "btn-blue"
    }
</script>

動畫定義3D啟用硬件加速

Element {
    -webkit-transform:translate3d(0, 0, 0)
    transform: translate3d(0, 0, 0);
}

注意:3D變形會消耗更多的內(nèi)存與功耗

Retina屏的1px邊框

Element{
    border-width: thin;
}

旋轉(zhuǎn)屏幕時,字體大小調(diào)整的問題

*{
   -webkit-text-size-adjust:100%;
}

頂部狀態(tài)欄背景色

<meta name="apple-mobile-web-app-status-bar-style" content="black" />

說明:

  • 除非你先使用apple-mobile-web-app-capable指定全屏模式,否則這個meta標(biāo)簽不會起任何作用。
  • 如果content設(shè)置為default,則狀態(tài)欄正常顯示。
  • 如果設(shè)置為blank,則狀態(tài)欄會有一個黑色的背景。
  • 如果設(shè)置為blank-translucent,則狀態(tài)欄顯示為黑色半透明。
  • 如果設(shè)置為default或blank,則頁面顯示在狀態(tài)欄的下方,即狀態(tài)欄占據(jù)上方部分,頁面占據(jù)下方部分,二者沒有遮擋對方或被遮擋。
  • 如果設(shè)置為blank-translucent,則頁面會充滿屏幕,其中頁面頂部會被狀態(tài)欄遮蓋?。〞采w頁面20px高度,而iphone4和itouch4的Retina屏幕為40px)。
  • 默認(rèn)值是default。兼容性 iOS 2.1 +

設(shè)置緩存

<meta http-equiv="Cache-Control" content="no-cache" />

手機頁面通常在第一次加載后會進(jìn)行緩存,然后每次刷新會使用緩存而不是去重新向服務(wù)器發(fā)送請求。如果不希望使用緩存可以設(shè)置no-cache。

桌面圖標(biāo)

<link rel="apple-touch-icon" href="touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="76x76" href="touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />

瀏覽器私有及其它meta

QQ瀏覽器私有

全屏模式
<meta name="x5-fullscreen" content="true">

強制豎屏
<meta name="x5-orientation" content="portrait">

強制橫屏
<meta name="x5-orientation" content="landscape">

應(yīng)用模式
<meta name="x5-page-mode" content="app">

UC瀏覽器私有

全屏模式
<meta name="full-screen" content="yes">

強制豎屏
<meta name="screen-orientation" content="portrait">

強制橫屏
<meta name="screen-orientation" content="landscape">

應(yīng)用模式
<meta name="browsermode" content="application">

其它

針對手持設(shè)備優(yōu)化,主要是針對一些老的不識別viewport的瀏覽器,比如黑莓

<meta name="HandheldFriendly" content="true">

微軟的老式瀏覽器

<meta name="MobileOptimized" content="320">

windows phone 點擊無高光

<meta name="msapplication-tap-highlight" content="no">

IOS中input鍵盤事件keyup、keydown、keypress支持不是很好

  • 問題是這樣的,用input search做模糊搜索的時候,在鍵盤里面輸入關(guān)鍵詞,會通過ajax后臺查詢,然后返回數(shù)據(jù),然后再對返回的數(shù)據(jù)進(jìn)行關(guān)鍵詞標(biāo)紅。
  • 用input監(jiān)聽鍵盤keyup事件,在安卓手機瀏覽器中是可以的,但是在ios手機瀏覽器中變紅很慢,用輸入法輸入之后,并未立刻相應(yīng)keyup事件,只有在通過刪除之后才能相應(yīng)!

解決方法:可以用html5的oninput事件去代替keyup

<input type="text" id="testInput">

<script>
    document.getElementById('testInput').addEventListener('input', function(e){
        var value = e.target.value
    })
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 他從很遠(yuǎn)很遠(yuǎn)的地方走來 背著青春走在寂寞的路上 長發(fā)遮住了他的臉龐 他的目光中透著堅強 他說他去過很多的地方 滿眼...
    一條自由自在的魚閱讀 249評論 0 0
  • 改變自己,從自己的內(nèi)心深處開始 感恩今天是個非常不錯的天氣,讓我們都能開著車出門去迎接考試。 感恩今天我們的群主李...
    生活就該甜甜蜜蜜閱讀 700評論 0 4
  • 晶瑩的琉璃童話谷,天藍(lán)色的秋千脈脈地自由飛舞,女孩靜靜地躺著,任由粉色絲帶陪風(fēng)兒私語… 琉璃谷上空,男...
    婭夢憶軒閱讀 196評論 0 0
  • 夏拉著雨就要往飛機上走,雨立馬把雪拉上一起上了飛機,A 13 A 12 A 11三個孩子坐在了一起,雪很安靜的從皮...
    運城小7閱讀 206評論 0 0

友情鏈接更多精彩內(nèi)容