http://www.open-open.com/lib/view/open1449325854077.htmljavascript
一、安卓瀏覽器看背景圖片,有些設備會模糊。
用同等比例的圖片在PC機上很清楚,可是手機上很模糊,緣由是什麼呢?
通過研究,是devicePixelRatio做怪,由於手機分辨率過小,若是按照分辨率來顯示網頁,這樣字會很是小,因此蘋果當初就把iPhone 4的960640分辨率,在網頁裏只顯示了480320,這樣devicePixelRatio=2。如今android比較亂,有1.5的,有2的也有3的。
想讓圖片在手機裏顯示更爲清晰,必須使用2x的背景圖來代替img標籤(通常狀況都是用2倍)。例如一個div的寬高是100100,背景圖必須得200200,而後background-size:contain;,這樣顯示出來的圖片就比較清晰了。
代碼能夠以下:css
background:url(../images/icon/all.png) no-repeat center center; -webkit-background-size:50px 50px; background-size: 50px 50px;display:inline-block; width:100%; height:50px;html
二、圖片加載
若您遇到圖片加載很慢的問題,對這種狀況,手機開發通常用canvas方法加載:
具體的canvas API 參見:http://javascript.ruanyifeng.com/htmlapi/canvas.html
下面舉例說明一個canvas的例子:
<li><canvas></canvas></li>
js動態加載圖片和li 總共舉例17張圖片!
vartotal=17;
varzWin=$(window);
varrender=function(){
varpadding=2;
varwinWidth=zWin.width();
varpicWidth=Math.floor((winWidth-padding*3)/4);
vartmpl ='';
for(vari=1;i<=totla;i++){
varp=padding;
varimgSrc='img/'+i+'.jpg';
if(i%4==1){
p=0;
}
tmpl +='<li style="width:'+picWidth+'px;height:'+picWidth+'px;padding-left:'+p+'px;padding-top:'+padding+'px;"><canvas id="cvs_'+i+'"></canvas></li>';
varimageObj = newImage();
imageObj.index = i;
imageObj.onload = function(){
varcvs =$('#cvs_'+this.index)[0].getContext('2d');
cvs.width = this.width;
cvs.height=this.height;
cvs.drawImage(this,0,0);
}
imageObj.src=imgSrc;
}
}
render();
三、假如手機網站不用兼容IE瀏覽器,通常咱們會使用zeptojs
zeptojs內置Touch events方法,具體能夠看http://zeptojs.com/#Touch events
看了一下zeptio新版的API,已經支持IE10以上瀏覽器,對zeptojs能夠選擇使用!
四、防止手機中網頁放大和縮小html5
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">java
設置了DTD的方式是XHTML的寫法,假如咱們頁面運用的是html5,能夠不用設置DTD,直接聲明<!DOCTYPE html>。android
使用viewport使頁面禁止縮放。 一般把user-scalable設置爲0來關閉用戶對頁面視圖縮放的行爲。ios
1
|
<
meta
name
=
"viewport"
content
=
"user-scalable=0"
/>
|
可是爲了更好的兼容,咱們會使用完整的viewport設置。git
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />github
五、apple-mobile-web-app-capable
apple-mobile-web-app-capable是設置Web應用是否以全屏模式運行。
語法:
<meta name="apple-mobile-web-app-capable"content="yes">
說明:
若是content設置爲yes,Web應用會以全屏模式運行,反之,則不會。content的默認值是no,表示正常顯示。你能夠經過只讀屬性window.navigator.standalone來肯定網頁是否以全屏模式顯示。
六、format-detection
format-detection 啓動或禁用自動識別頁面中的電話號碼。
語法:
<meta name="format-detection"content="telephone=no">
說明:
默認狀況下,設備會自動識別任何多是電話號碼的字符串。設置telephone=no能夠禁用這項功能。
七、html5調用安卓或者ios的撥號功能
html5提供了自動調用撥號的標籤,只要在a標籤的href中添加tel:就能夠了。
以下:
<ahref="tel:4008106999,1034">400-810-6999 轉 1034</a>
撥打手機直接以下
<a href="tel:15677776767">點擊撥打15677776767</a>
八、html5GPS定位功能
具體請看:http://www.jb51.net/post/html5_GPS_getCurrentPosition
九、iphone及ipad下輸入框默認內陰影
Element{
-webkit-appearance:none;
}
十、active兼容處理 即 僞類 :active 失效
方法一:body添加ontouchstartweb
<body ontouchstart="">
方法二:js給 document 綁定 touchstart 或 touchend 事件
<style>
a
{
color:#000;
}
a:active
{
color:#fff;
}
</style>
<a
herf=foo >bar</a>
<script>
document.addEventListener('touchstart',function(){},false);
</script>
圓角bug
某些Android手機圓角失效
background-clip: padding-box;
消除 IE10 裏面的那個叉號
input:-ms-clear{display:none;}
九、上下拉動滾動條時卡頓、慢
body {
-webkit-overflow-scrolling: touch;
overflow-scrolling: touch;
}
Element {
-webkit-user-select:
none
;
-moz-user-select:
none
;
-khtml-user-select:
none
;
user-select:
none
;
}
element {
-webkit-touch-callout:
none
;
}
Element{
-webkit-appearance:
none
;
}
Element {
-webkit-tap-highlight-
color
:rgba(
255
,
255
,
255
,
0
)
}
設置alpha值爲0就能夠去除半透明灰色遮罩,備註:transparent的屬性值在android下無效。
後面一篇文章有詳細介紹,地址:http://www.jb51.net/post/phone_web_ysk
1五、動畫定義3D啓用硬件加速
Element {
-webkit-transform:translate
3
d(
0
,
0
,
0
)
transform: translate
3
d(
0
,
0
,
0
);
}
Element{
border-width
:
thin
;
}
html, body, form, fieldset, p, div, h
1
, h
2
, h
3
, h
4
, h
5
, h
6
{
-webkit-text-size-adjust:
100%
;
}
/設置內嵌的元素在
3
D 空間如何呈現:保留
3
D /
-webkit-transform-style: preserve
-3
d;
/ 設置進行轉換的元素的背面在面對用戶時是否可見:隱藏 /
-webkit-backface-
visibility
:
hidden
;
<
meta
name
=
"apple-mobile-web-app-status-bar-style"
content
=
"black"
/>
說明:
除非你先使用apple-mobile-web-app-capable指定全屏模式,不然這個meta標籤不會起任何做用。
若是content設置爲default,則狀態欄正常顯示。若是設置爲blank,則狀態欄會有一個黑色的背景。若是設置爲blank-translucent,則狀態欄顯示爲黑色半透明。若是設置爲default或blank,則頁面顯示在狀態欄的下方,即狀態欄佔據上方部分,頁面佔據下方部分,兩者沒有遮擋對方或被遮擋。若是設置爲blank-translucent,則頁面會充滿屏幕,其中頁面頂部會被狀態欄遮蓋住(會覆蓋頁面20px高度,而iphone4和itouch4的Retina屏幕爲40px)。默認值是default。
2二、設置緩存
<
meta
http-equiv
=
"Cache-Control"
content
=
"no-cache"
/>
手機頁面一般在第一次加載後會進行緩存,而後每次刷新會使用緩存而不是去從新向服務器發送請求。若是不但願使用緩存能夠設置no-cache。
2四、啓動畫面
<
link
rel
=
"apple-touch-startup-image"
href
=
"start.png"
/>
OS下頁面啓動加載時顯示的畫面圖片,避免加載時的白屏。
能夠經過madia來指定不一樣的大小:
<!--iPhone-->
<link href=
"apple-touch-startup-image-320x460.png"
media=
"(device-width: 320px)"
rel=
"apple-touch-startup-image"
/>
<!-- iPhone Retina -->
<link href=
"apple-touch-startup-image-640x920.png"
media=
"(device-width: 320px) and (-webkit-device-pixel-ratio: 2)"
rel=
"apple-touch-startup-image"
/>
<!-- iPhone
5
-->
<link rel=
"apple-touch-startup-image"
media=
"(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)"
href=
"apple-touch-startup-image-640x1096.png"
>
<!-- iPad
portrait
-->
<link href=
"apple-touch-startup-image-768x1004.png"
media=
"(device-width: 768px) and (orientation: portrait)"
rel=
"apple-touch-startup-image"
/>
<!-- iPad
landscape
-->
<link href=
"apple-touch-startup-image-748x1024.png"
media=
"(device-width: 768px) and (orientation: landscape)"
rel=
"apple-touch-startup-image"
/>
<!-- iPad Retina
portrait
-->
<link href=
"apple-touch-startup-image-1536x2008.png"
media=
"(device-width: 1536px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)"
rel=
"apple-touch-startup-image"
/>
<!-- iPad Retina
landscape
-->
<link href=
"apple-touch-startup-image-1496x2048.png"
media=
"(device-width: 1536px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)"
rel=
"apple-touch-startup-image"
/>
2五、瀏覽器私有及其它meta
如下屬性在項目中沒有應用過,能夠寫一個demo測試如下!
QQ瀏覽器私有
全屏模式
<
meta
name
=
"x5-fullscreen"
content
=
"true"
>
強制豎屏
<
meta
name
=
"x5-orientation"
content
=
"portrait"
>
強制橫屏
<
meta
name
=
"x5-orientation"
content
=
"landscape"
>
應用模式
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"
>
應用模式
<
meta
name
=
"browsermode"
content
=
"application"
>
其它
針對手持設備優化,主要是針對一些老的不識別viewport的瀏覽器,好比黑莓
<
meta
name
=
"HandheldFriendly"
content
=
"true"
>
微軟的老式瀏覽器
<
meta
name
=
"MobileOptimized"
content
=
"320"
>
windows phone 點擊無高光
<
meta
name
=
"msapplication-tap-highlight"
content
=
"no"
>
2六、 IOS中input鍵盤事件keyup、keydown、keypress支持不是很好
問題是這樣的,用input search作模糊搜索的時候,在鍵盤裏面輸入關鍵詞,會經過ajax後臺查詢,而後返回數據,而後再對返回的數據進行關鍵詞標紅。用input監聽鍵盤keyup事件,在安卓手機瀏覽器中是能夠的,可是在ios手機瀏覽器中變紅很慢,用輸入法輸入以後,並未馬上相應keyup事件,只有在經過刪除以後才能相應!
解決辦法:
能夠用html5的oninput事件去代替keyup
<input type=
"text"
id=
"testInput"
>
<script type=
"text/javascript"
>
document.getElementById(
'testInput'
).addEventListener(
'input'
,
function
(e){
var
value = e.target.value;
});
</script>
<input type=
"number"
oninput=
"checkTextLength(this ,10)"
>
function
checkTextLength(obj, length) {
if
(obj.value.length > length) {
obj.value = obj.value.substr(0, length);
}
問題二,是由於form提交默認作了表單驗證,step默認是1,要設置step屬性,假如保留2位小數,寫法以下:
<
input
type
=
"number"
step
=
"0.01"
/>
關於step,我在這裏作簡單的介紹,input 中type=number,通常會自動生成一個上下箭頭,點擊上箭頭默認增長一個step,點擊下箭頭默認會減小一個step。number中默認step是1。也就是step=0.01,能夠容許輸入2位小數,而且點擊上下箭頭分別增長0.01和減小0.01。
假如step和min一塊兒使用,那麼數值必須在min和max之間。
<
input
type
=
"number"
step
=
"3.1"
min
=
"1"
/>
輸入框能夠輸入哪些數字?
首先,最小值是1,那麼能夠輸入1.0,第二個是能夠輸入(1+3.1)那就是4.1,以此類推,每次點擊上下箭頭都會增長或者減小3.1,輸入其餘數字無效。這就是step的簡單介紹。
問題三,去除input默認樣式
input[type=number] {
-moz-appearance:textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance:
none
;
margin
:
0
;
}
input,
textarea {
border: 0;
-webkit-appearance: none;
}
<
input
type
=
"text"
autocapitalize
=
"off"
/>
select option {
direction: rtl;
}
-webkit-transform: rotate(-4deg) skew(10deg) translateZ(0);
transform: rotate(-4deg) skew(10deg) translateZ(0);
outline: 1px solid rgba(255,255,255,0)
3二、移動端點擊300ms延遲
300ms尚可接受,不過由於300ms產生的問題,咱們必需要解決。300ms致使用戶體驗並非很好,解決這個問題,咱們通常在移動端用tap事件來取代click事件。
推薦兩個js,一個是fastclick,一個是tap.js
關於300ms延遲,具體請看:http://thx.github.io/mobile/300ms-click-delay/
3三、移動端點透問題
案例以下:
<
div
id
=
"haorooms"
>點頭事件測試</
div
>
<
a
href
=
"www.jb51.net"
>www.jb51.net</
a
>
$(
'#haorooms'
).on(
'tap'
,
function
(){
$(
'#haorooms'
).hide();
});
咱們點擊蒙層時 div正常消失,可是當咱們在a標籤上點擊蒙層時,發現a連接被觸發,這就是所謂的點透事件。
緣由:
touchstart 早於 touchend 早於click。 亦即click的觸發是有延遲的,這個時間大概在300ms左右,也就是說咱們tap觸發以後蒙層隱藏, 此時 click尚未觸發,300ms以後因爲蒙層隱藏,咱們的click觸發到了下面的a連接上。
解決:
(1)儘可能都使用touch事件來替換click事件。例如用touchend事件(推薦)。
(2)用fastclick,https://github.com/ftlabs/fastclick
(3)用preventDefault阻止a標籤的click
(4)延遲必定的時間(300ms+)來處理事件 (不推薦)
(5)以上通常都能解決,實在不行就換成click事件。
下面介紹一下touchend事件,以下:
$(
"#haorooms"
).on(
"touchend"
,
function
(event) {
event.preventDefault();
});
input:-ms-clear{
display
:
none
;}
3五、關於 iOS 與 OS X 端字體的優化(橫豎屏會出現字體加粗不一致等)
iOS 瀏覽器橫屏時會重置字體大小,設置 text-size-adjust 爲 none 能夠解決 iOS 上的問題,但桌面版 Safari 的字體縮放功能會失效,所以最佳方案是將 text-size-adjust 爲 100% 。
-webkit-text-size-adjust:
100%
;
-ms-text-size-adjust:
100%
;
text-size-adjust:
100%
;
3六、關於 iOS 系統中,中文輸入法輸入英文時,字母之間可能會出現一個六分之一空格
能夠經過正則去掉
this
.value =
this
.value.replace(/\u2006/g,
''
);
3七、移動端 HTML5 audio autoplay 失效問題
這個不是 BUG,因爲自動播放網頁中的音頻或視頻,會給用戶帶來一些困擾或者沒必要要的流量消耗,因此蘋果系統和安卓系統一般都會禁止自動播放和使用 JS 的觸發播放,必須由用戶來觸發才能夠播放。
解決方法思路:先經過用戶 touchstart 觸碰,觸發播放並暫停(音頻開始加載,後面用 JS 再操做就沒問題了)。
解決代碼:
document.addEventListener(
'touchstart'
,
function
() {
document.getElementsByTagName(
'audio'
)[0].play();
document.getElementsByTagName(
'audio'
)[0].pause();
});
3八、移動端 HTML5 input date 不支持 placeholder 問題
這個我感受沒有什麼好的解決方案,用以下方法
<input placeholder="Date" class="textbox-n" type="text" onfocus="(this.type='date')" id="date">
有的瀏覽器可能要點擊兩遍!
3九、部分機型存在type爲search的input,自帶close按鈕樣式修改方法
有些機型的搜索input控件會自帶close按鈕(一個僞元素),而一般爲了兼容全部瀏覽器,咱們會本身實現一個,此時去掉原生close按鈕的方法爲
#Search::-webkit-search-cancel-button{
display
:
none
;
}
40、喚起select的option展開
zepto方式:$(sltElement).trrgger(
"mousedown"
);
原生js方式:
function
showDropdown(sltElement) {
var
event;
event = document.createEvent(
'MouseEvents'
);
event.initMouseEvent(
'mousedown'
,
true
,
true
, window);
sltElement.dispatchEvent(event);
};
h5頁面有個很蛋疼的問題就是,當輸入框在最底部,點擊軟鍵盤後輸入框會被遮擋。可採用以下方式解決
var oHeight = $(document).height(); //瀏覽器當前的高度 $(window).resize(function(){ if($(document).height() < oHeight){ $("#footer").css("position","static"); }else{ $("#footer").css("position","absolute"); } });
關於Web移動端Fixed佈局的解決方案,這篇文章也不錯
http://efe.baidu.com/blog/mobile-fixed-layout/
<meta content="email=no" name="format-detection" />
<meta content="telephone=no" name="format-detection" />
-webkit-touch-callout:none
html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 {-webkit-text-size-adjust:none;}
input 的placeholder會出現文本位置偏上的狀況:PC端設置line-height等於height可以對齊,而移動端仍然是偏上,解決是設置line-height:normal
CSS3中的calc變量在iOS6瀏覽器中必須加-webkit-前綴,目前的FF瀏覽器已經無需-moz-前綴。
Android瀏覽器目前仍然不支持calc,因此要在以前增長一個保守尺寸:
div { width: 95%; width: -webkit-calc(100% - 50px); width: calc(100% - 50px); }
JS判斷設備
function deviceType(){ var ua = navigator.userAgent; var agent = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; for(var i=0; i<len,len = agent.length; i++){ if(ua.indexOf(agent[i])>0){ break; } } } deviceType(); window.addEventListener('resize', function(){ deviceType(); })
JS判斷微信瀏覽器
function isWeixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=='micromessenger'){ return true; }else{ return false; } }
2.使用click會出現綁定點擊區域閃一下的狀況,解決:給該元素一個樣式以下
-webkit-tap-highlight-color: rgba(0,0,0,0);
3.ios android判斷;
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//console.log($('.input_pass').css({'font-size':'100%','letter-spacing':'19px','top':'0px'}));
} else if (/(Android)/i.test(navigator.userAgent)) {
$('.pwd_outer .input_pass').css({'fontSize':'24px','letter-spacing':'22px'});
} else {
//console.log('我是其餘');
};
4.從新定義滾動條的樣式:
webkit如今支持擁有overflow屬性的區域,列表框,下拉菜單,textarea的滾動條自定義樣式。
::-webkit-scrollbar {width: 12px;}定義滾動條的寬度;
::-webkit-scrollbar-track {border-left: 1px solid #ccc;}定義滾動條的背景
::-webkit-scrollbar-thumb {}上下箭頭
::-webkit-scrollbar-thumb:hover {
}
::-webkit-scrollbar-thumb:active{
}
5.textarea這個標籤,具備默認樣式
-webkit-appearance: none; 經過這個屬性能夠取消;
6.指定下拉刷新獲取高度的兼容性,低版本的安卓(小米)不支持touchend事件;咱們寫在touchmove事件裏面;
var scroll=document.documentElement.scrollTop||document.body.scrollTop;//滾動的距離
var win=document.documentElement.clientHeight||document.body.clientHeight;//窗口的高度
var scrollHeight=document.documentElement.scrollHeight||document.body.scrollHeight;//文檔總高度
文字兩端居中text-align:justify;text-align-last:just;
在移動端不起做用
解決辦法:使用 ;代替空格