1. 程式人生 > >移動端web開發常見問題

移動端web開發常見問題

size auto eba 無需 情況 下滑 產生 masonry tro

1、移動端如何定義字體font-family

三大手機系統的字體:

ios 系統
默認中文字體是Heiti SC
默認英文字體是Helvetica
默認數字字體是HelveticaNeue
無微軟雅黑字體

android 系統
默認中文字體是Droidsansfallback
默認英文和數字字體是Droid Sans
無微軟雅黑字體

winphone 系統
默認中文字體是Dengxian(方正等線體)
默認英文和數字字體是Segoe
無微軟雅黑字體

各個手機系統有自己的默認字體,且都不支持微軟雅黑
如無特殊需求,手機端無需定義中文字體,使用系統默認
英文字體和數字字體可使用 Helvetica ,三種系統都支持


* 移動端定義字體的代碼 */
body{font-family:Helvetica;}



2、移動端字體單位font-size選擇px還是rem

對於只需要適配手機設備,使用px即可

對於需要適配各種移動設備,使用rem,例如只需要適配iPhone和iPad等分辨率差別比較挺大的設備

rem配置參考:

html {font-size:10px}
@media screen and (min-width:480px) and (max-width:639px) {
html {
font-size: 15px
}
}
@media screen and (min-width:640px) and (max-width:719px) {
html {
font-size: 20px
}
}
@media screen and (min-width:720px) and (max-width:749px) {
html {
font-size: 22.5px
}
}
@media screen and (min-width:750px) and (max-width:799px) {
html {
font-size: 23.5px
}
}
@media screen and (min-width:800px) and (max-width:959px) {
html {
font-size: 25px
}
}
@media screen and (min-width:960px) and (max-width:1079px) {
html {
font-size: 30px
}
}
@media screen and (min-width:1080px) {
html {
font-size: 32px
}
}


3、什麽是Retina 顯示屏,帶來了什麽問題

retina:一種具備超高像素密度的液晶屏,同樣大小的屏幕上顯示的像素點由1個變為多個,如在同樣帶下的屏幕上,蘋果設備的retina顯示屏中,像素點1個變為4個
在高清顯示屏中的位圖被放大,圖片會變得模糊,因此移動端的視覺稿通常會設計為傳統PC的2倍

那麽,前端的應對方案是:
設計稿切出來的圖片長寬保證為偶數,並使用backgroud-size把圖片縮小為原來的1/2

//例如圖片寬高為:200px*200px,那麽寫法如下
.css{width:100px;height:100px;background-size:100px 100px;}

其它元素的取值為原來的1/2,例如視覺稿40px的字體,使用樣式的寫法為20px

.css{font-size:20px}



4、viewport模板

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<title>標題</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
這裏開始內容
</body>
</html>

5. 優先使用最新版本 IE 和 Chrome

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

6. 添加到主屏後的標題(IOS)

<meta name="apple-mobile-web-app-title" content="標題">


7. 啟用 WebApp 全屏模式(IOS)
當網站添加到主屏幕後再點擊進行啟動時,可隱藏地址欄(從瀏覽器跳轉或輸入鏈接進入並沒有此效果)

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



8. 百度禁止轉碼
通過百度手機打開網頁時,百度可能會對你的網頁進行轉碼,往你頁面貼上它的廣告,非常之惡心。不過我們可以通過這個meta標簽來禁止它:

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


10. 設置狀態欄的背景顏色(IOS)
設置狀態欄的背景顏色,只有在 "apple-mobile-web-app-capable" content="yes" 時生效

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

content 參數:
default :狀態欄背景是白色。
black :狀態欄背景是黑色。
black-translucent :狀態欄背景是半透明。 如果設置為 default 或 black ,網頁內容從狀態欄底部開始。 如果設置為 black-translucent ,網頁內容充滿整個屏幕,頂部會被狀態欄遮擋。


11. 移動端手機號碼識別(IOS)
在 iOS Safari (其他瀏覽器和Android均不會)上會對那些看起來像是電話號碼的數字處理為電話鏈接,比如:
7位數字,形如:1234567
帶括號及加號的數字,形如:(+86)123456789
雙連接線的數字,形如:00-00-00111
11位數字,形如:13800138000
可能還有其他類型的數字也會被識別。我們可以通過如下的meta來關閉電話號碼的自動識別:

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

開啟電話功能
<a href="tel:123456">123456</a>

開啟短信功能:
<a href="sms:123456">123456</a>


12. 移動端郵箱識別(Android)
與電話號碼的識別一樣,在安卓上會對符合郵箱格式的字符串進行識別,我們可以通過如下的meta來管別郵箱的自動識別:
<meta content="email=no" name="format-detection" />


同樣地,我們也可以通過標簽屬性來開啟長按郵箱地址彈出郵件發送的功能:
<a mailto:[email protected]">[email protected]</a>





13、ios系統中元素被觸摸時產生的半透明灰色遮罩怎麽去掉
ios用戶點擊一個鏈接,會出現一個半透明灰色遮罩, 如果想要禁用,可設置-webkit-tap-highlight-color的alpha值為0,也就是屬性值的最後一位設置為0就可以去除半透明灰色遮罩

a,button,input,textarea{-webkit-tap-highlight-color: rgba(0,0,0,0)}



14、部分android系統中元素被點擊時產生的邊框怎麽去掉
android用戶點擊一個鏈接,會出現一個邊框或者半透明灰色遮罩, 不同生產商定義出來額效果不一樣,可設置-webkit-tap-highlight-color的alpha值為0去除部分機器自帶的效果

a,button,input,textarea{
-webkit-tap-highlight-color: rgba(0,0,0,0)
-webkit-user-modify:read-write-plaintext-only;
}

-webkit-user-modify有個副作用,就是輸入法不再能夠輸入多個字符
另外,有些機型去除不了,如小米2
對於按鈕類還有個辦法,不使用a或者input標簽,直接用div標簽

15、winphone系統a、input標簽被點擊時產生的半透明灰色背景怎麽去掉

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

16、webkit表單元素的默認外觀怎麽重置
.css{-webkit-appearance:none;}

17、webkit表單輸入框placeholder的顏色值能改變麽

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


18、webkit表單輸入框placeholder的文字能換行麽
ios可以,android不行~

19. 關閉iOS鍵盤首字母自動大寫
在iOS中,默認情況下鍵盤是開啟首字母大寫的功能的,如果啟用這個功能,可以這樣:
<input type="text" autocapitalize="off" />

20. 關閉iOS輸入自動修正
和英文輸入默認自動首字母大寫那樣,IOS還做了一個功能,默認輸入法會開啟自動修正輸入內容,這樣的話,用戶經常要操作兩次。如果不希望開啟此功能,我們可以通過input標簽屬性來關閉掉:
<input type="text" autocorrect="off" />

21. 禁止文本縮放
當移動設備橫豎屏切換時,文本的大小會重新計算,進行相應的縮放,當我們不需要這種情況時,可以選擇禁止:
html {
   -webkit-text-size-adjust: 100%;
}

需要註意的是,PC端的該屬性已經被移除,該屬性在移動端要生效,必須設置 `meta viewport’。


22. 移動端如何清除輸入框內陰影
在iOS上,輸入框默認有內部陰影,但無法使用 box-shadow 來清除,如果不需要陰影,可以這樣關閉:

input,
textarea {
  border: 0;
  -webkit-appearance: none;
}
23.屏幕旋轉的事件和樣式

window.orientation,取值:正負90表示橫屏模式、0和180表現為豎屏模式;

window.onorientationchange = function(){
switch(window.orientation){
case -90:
case 90:
alert("橫屏:" + window.orientation);
case 0:
case 180:
alert("豎屏:" + window.orientation);
break;
}
}

樣式
//豎屏時使用的樣式
@media all and (orientation:portrait) {
.css{}
}

//橫屏時使用的樣式
@media all and (orientation:landscape) {
.css{}
}

24.audio元素和video元素在ios和andriod中無法自動播放
應對方案:觸屏即播
$(‘html‘).one(‘touchstart‘,function(){
audio.play()
})

25.搖一搖功能
HTML5 deviceMotion:封裝了運動傳感器數據的事件,可以獲取手機運動狀態下的運動加速度等數據。

26.手機拍照和上傳圖片

<input type="file">的accept 屬性

<!-- 選擇照片 -->
<input type=file accept="image/*">

<!-- 選擇視頻 -->
<input type=file accept="video/*">

使用總結:
ios 有拍照、錄像、選取本地圖片功能
部分android只有選取本地圖片功能
winphone不支持
input控件默認外觀醜陋

27. 消除transition閃屏
.css{

-webkit-transform-style: preserve-3d;

-webkit-backface-visibility: hidden;
}

開啟硬件加速
解決頁面閃白
保證動畫流暢
.css {
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}

設計高性能CSS3動畫的幾個要素
盡可能地使用合成屬性transform和opacity來設計CSS3動畫,
不使用position的left和top來定位
利用translate3D開啟GPU加速

28. android 上去掉語音輸入按鈕
input::-webkit-input-speech-button {display: none}

29.模擬按鈕hover效果
移動端觸摸按鈕的效果,可明示用戶有些事情正要發生,是一個比較好體驗,但是移動設備中並沒有鼠標指針,使用css的hover並不能滿足我們的需求,還好國外有個激活css的active效果,代碼如下,

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<style type="text/css">

a{-webkit-tap-highlight-color: rgba(0,0,0,0);}
.btn-blue{display:block;height:42px;line-height:42px;text-align:center;border-radius:4px;font-size:18px;color:#FFFFFF;background-color: #4185F3;}
.btn-blue:active{background-color: #357AE8;}

</style>
</head>
<body>

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

<script type="text/javascript">
document.addEventListener("touchstart", function(){}, true)
</script>
</body>
</html>
框架
1. 移動端基礎框架
zepto.js 語法與jquery幾乎一樣,會jquery基本會zepto~
iscroll.js 解決頁面不支持彈性滾動,不支持fixed引起的問題~ 實現下拉刷新,滑屏,縮放等功能~
underscore.js 該庫提供了一整套函數式編程的實用功能,但是沒有擴展任何JavaScript內置對象。
fastclick 加快移動端點擊響應時間
animate.css CSS3動畫效果庫
Normalize.css Normalize.css是一種現代的、CSS reset為HTML5準備的優質替代方案

2. 滑屏框架
適合上下滑屏、左右滑屏等滑屏切換頁面的效果
slip.js
iSlider.js
fullpage.js
swiper

3.瀑布流框架
masonry

移動端web開發常見問題