H5項目常見問題及注意事項
- H5頁面窗口自動調整到設備寬度,并禁止用戶縮放頁面
<meta name=
"viewport" content=
"width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
var phoneWidth =
parseInt(window.screen.width);
var phoneScale = phoneWidth/
640;
var ua = navigator.userAgent;
if (
/Android (\d+\.\d+)/.test(ua)){
var version =
parseFloat(
RegExp.$
1);
if(version>
2.3){document.write(
'<meta name="viewport" content="width=640, minimum-scale = '+phoneScale+
', maximum-scale = '+phoneScale+
', target-densitydpi=device-dpi">');}
else{document.write(
'<meta name="viewport" content="width=640, target-densitydpi=device-dpi">');}
}
else {document.write(
'<meta name="viewport" content="width=640, user-scalable=no, target-densitydpi=device-dpi">');
}
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection"content="telephone=no, email=no" />
<meta name="keywords" content="your tags" />
<meta name="description" content="150 words" />
<meta name="robots" content="index,follow" />
<meta http-equiv="refresh" content="0;url=" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="true">
<meta name="MobileOptimized" content="320">
<meta name="screen-orientation" content="portrait">
<meta name="x5-orientation" content="portrait">
<meta name="full-screen" content="yes">
<meta name="x5-fullscreen" content="true">
<meta name="browsermode" content="application">
<meta name="x5-page-mode" content="app">
<meta name="msapplication-tap-highlight" content="no"><meta name="author" content="author name" />
<meta name="google" content="index,follow" />
<meta name="googlebot" content="index,follow" />
<meta name="verify" content="index,follow" />
常見問題:
@ --------------------------------------中文字體的英文名稱
@ 宋體 SimSun
@ 黑體 SimHei
@ 微信雅黑 Microsoft Yahei
@ 微軟正黑體 Microsoft JhengHei
@ 新宋體 NSimSun
@ 新細明體 MingLiU
@ 細明體 MingLiU
@ 標楷體 DFKai-SB
@ 仿宋 FangSong
@ 楷體 KaiTi
@ 仿宋_GB2312 FangSong_GB2312
@ 楷體_GB2312 KaiTi_GB2312
@
@ 說明:中文字體多數使用宋體、雅黑,英文用Helveticabody { font-family: Microsoft Yahei,SimSun,Helvetica; }
// 一、打電話
<
a href=
"tel:0755-10086">打電話給:
0755-
10086</
a>
<
a href=
"sms:10086">發短信給:
10086</
a>
<
a href=
"mailto:863139978@qq.com">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?cc=zhangqian0406@yeah.net">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?cc=zhangqian0406@yeah.net&bcc=384900096@qq.com">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com;384900096@qq.com">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?subject=郵件主題">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?body=郵件主題內容%0A騰訊誠信%0A期待您的到來">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?body=http://www.baidu.com">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com?body=<img src='images/1.jpg' />">點擊我發郵件</
a>
<
a href=
"mailto:863139978@qq.com;384900096@qq.com?cc=zhangqian0406@yeah.net&bcc=993233461@qq.com&subject=[郵件主題]&body=騰訊誠邀您參與%0A%0Ahttp://www.baidu.com%0A%0A<img src='images/1.jpg' />">點擊我發郵件</
a>
- 移動端touch事件(區分webkit和winphone)
touchstart——當手指觸碰屏幕時候發生。不管當前有多少只手指
touchmove——當手指在屏幕上滑動時連續觸發。通常我們再滑屏頁面,會調用event的preventDefault()可以阻止默認情況的發生:阻止頁面滾動
touchend——當手指離開屏幕時觸發
touchcancel——系統停止跟蹤觸摸時候會觸發。例如在觸摸過程中突然頁面alert()一個提示框,此時會觸發該事件,這個事件比較少用
touches:屏幕上所有手指的信息
targetTouches:手指在目標區域的手指信息
changedTouches:最近一次觸發該事件的手指信息
touchend時,touches與targetTouches信息會被刪除,changedTouches保存的最后一次的信息,最好用于計算手指信息
clientX、clientY在顯示區的坐標
target:當前元素
ontouchstart
> ontouchmove
> ontouchend
> onclick
MSPointerDown——當手指觸碰屏幕時候發生。不管當前有多少只手指
MSPointerMove——當手指在屏幕上滑動時連續觸發。通常我們再滑屏頁面,會調用css的html{
-ms-touch-action:
none;}可以阻止默認情況的發生:阻止頁面滾動
MSPointerUp——當手指離開屏幕時觸發
- 移動端click屏幕產生200-300ms的延時響應
說明:移動設備上的web網頁是有
300ms延遲的,玩玩會造成按鈕點擊延遲甚至是點擊失效。以下是歷史原因,來源一個公司內一個同事的分享:
2007年蘋果發布首款iphone上IOS系統搭載的safari為了將適用于PC端上大屏幕的網頁能比較好的展示在手機端上,使用了雙擊縮放(double tap
to zoom)的方案,比如你在手機上用瀏覽器打開一個PC上的網頁,你可能在看到頁面內容雖然可以撐滿整個屏幕,但是字體、圖片都很小看不清,此時可以快速雙擊屏幕上的某一部分,你就能看清該部分放大后的內容,再次雙擊后能回到原始狀態。雙擊縮放是指用手指在屏幕上快速點擊兩次,iOS 自帶的 Safari 瀏覽器會將網頁縮放至原始比例。原因就出在瀏覽器需要如何判斷快速點擊上,當用戶在屏幕上單擊某一個元素時候,例如跳轉鏈接<
a href=
"#"></
a>,此處瀏覽器會先捕獲該次單擊,但瀏覽器不能決定用戶是單純要點擊鏈接還是要雙擊該部分區域進行縮放操作,所以,捕獲第一次單擊后,瀏覽器會先Hold一段時間t,如果在t時間區間里用戶未進行下一次點擊,則瀏覽器會做單擊跳轉鏈接的處理,如果t時間里用戶進行了第二次單擊操作,則瀏覽器會禁止跳轉,轉而進行對該部分區域頁面的縮放操作。那么這個時間區間t有多少呢?在IOS safari下,大概為
300毫秒。這就是延遲的由來。造成的后果用戶純粹單擊頁面,頁面需要過一段時間才響應,給用戶慢體驗感覺,對于web開發者來說是,頁面js捕獲click事件的回調函數處理,需要
300ms后才生效,也就間接導致影響其他業務邏輯的處理。
fastclick可以解決在手機上點擊事件的
300ms延遲
zepto的touch模塊,tap事件也是為了解決在click的延遲問題
說明:retina屏是一種具備超高像素密度的液晶屏,同樣大小的屏幕上顯示的像素點由
1個變為多個,如在同樣帶下的屏幕上,蘋果設備的retina顯示屏中,像素點
1個變為
4個。
在高清顯示屏中的位圖被放大,圖片會變得模糊,因此移動端的視覺稿通常會設計為傳統PC的
2倍。
那么,前端的應對方案是:設計稿切出來的圖片長寬保證為偶數,并使用backgroud
-size把圖片縮小為原來的
1/
2
.css{width:
100px;height:
100px;background
-size:
100px
100px;}
.css{font
-size:
20px}
image
-set,webkit私有屬性,也是CSS4的屬性,為解決Rentina屏幕下的圖像而生。
.css {background: url(images/bg
.jpg) no
-repeat center;background:
-webkit-image-set(url(images/bg
.jpg)
1x, url(images/bg
-2x
.jpg)
2x);
}
a,button,input,textarea {
-webkit-tap-highlight-color: rgba(
0,
0,
0,
0);
-webkit-user-modify:read
-write-plaintext-only;
}
* {
-webkit-tap-highlight-color: rgba(
0,
0,
0,
0); }
<meta name
="msapplication-tap-highlight" content
="no">
input,select { -webkit-appearance:none; appearance: none; }
select::-ms-expand { display:none; }
input[
type=radio]::-ms-
check,
input[
type=checkbox]::-ms-
check { display:none; }
input[
type=text]::-ms-clear,
input[
type=tel]::-ms-clear,
input[
type=number]::-ms-clear { display:none; }
- 移動端字體單位font-size選擇px還是rem
html {
font-size: 62.5%; }
body {
font-size:12px; font-size:1.2rem; }
::-webkit-scrollba
::-webkit-scrollbar-thumb
::-webkit-scrollbar-
track
::-webkit-scrollbar-
button
::-webkit-scrollbar-
track-piece
::-webkit-scrollbar-corner
::-webkit-resizer
a,img { -webkit-touch-callout: none }
html,body {-webkit-user-
select:none; user-
select: none; }
::-webkit-input-placeholder {
color: #
999; }
:-moz-placeholder {
color: #
999; }
::-moz-placeholder {
color: #
999; }
:-ms-input-placeholder {
color: #
999; }
input:focus::-webkit-input-placeholder{
color:#
999; }
input::-webkit-input-speech-
button {display: none}
html { -ms-touch-action:none; }
- 取消input在ios下,輸入的時候英文首字母的默認大寫
<input autocapitalize="off" autocorrect="off" />
<input
type=
"file" accept=
"images/*" />
<input
type=
"file" accept=
"video/*" />
function orientInit(){var orientChk = document.documentElement.clientWidth > document.documentElement.clientHeight?
'landscape':
'portrait';
if(orientChk ==
'lapdscape'){}
else{}
}orientInit();
window.addEventListener(
'onorientationchange' in window?
'orientationchange':
'resize',
function(){setTimeout(orientInit,
100);
},
false)
@media all
and (orientation:portrait){ }
@media all
and (orientation:landscape){ }
- audio元素和video元素在ios和andriod中無法自動播放
<audio src=
"music/bg.mp3" autoplay loop controls>你的瀏覽器還不支持哦</audio>
<audio controls=
"controls"> <source src=
"music/bg.ogg" type=
"audio/ogg"></source><source src=
"music/bg.mp3" type=
"audio/mpeg"></source>優先播放音樂bg.ogg,不支持在播放bg.mp3
</audio>
$(window).one(
'touchstart',
function(){music.play();
})
document.addEventListener(
"WeixinJSBridgeReady",
function () {music.play();
},
false);
if(window.DeviceMotionEvent){document.addEventListener(
'devicemotion', deviceMotionHandler,
false)
}
var speed =
30;
var x = y = z = lastX = lastY = lastZ =
0;
function deviceMotionHandler(eventData){var acceleration = event.accelerationIncludingGravity;x = acceleration.x;y = acceleration.y; z = acceleration.z;
if(
Math.abs(x-lastX)>speed ||
Math.abs(y-lastY)>speed ||
Math.abs(z-lastZ)>speed ){yaoAfter();}lastX = x;lastY = y;lastZ = z;
}
function yaoAfter(){
}
- 微信瀏覽器用戶調整字體大小后頁面矬了,怎么阻止用戶調整
if (
typeof(WeixinJSBridge) ==
"undefined") {document.addEventListener(
"WeixinJSBridgeReady",
function (e) {setTimeout(
function(){WeixinJSBridge.invoke(
'setFontSizeCallback', {
'fontSize':
0},
function(res){alert(
JSON.stringify(res));})},
0)});
}
else{ setTimeout(
function(){WeixinJSBridge.invoke(
'setFontSizeCallback', {
'fontSize':
0},
function(res){alert(
JSON.stringify(res));})},
0)
}
body { -webkit-text-size-adjust:
100%!important; }
<div id=
"wrapper"><ul><li></li>.....</ul>
</div>
<script src=
"iscroll.js"></script>
<script>
var myscroll;
function loaded(){myscroll=
new iScroll(
"wrapper");}window.addEventListener(
"DOMContentLoaded",loaded,
false);
</script>
var ua = navigator.userAgent.indexOf(
'Android');
if(ua>-
1){$(
'.ipt').on(
'focus',
function(){$(
'.css').css({
'visibility':
'hidden'})}).on(
'blur',
function(){$(
'.css').css({
'visibility':
'visible'})})
}
<video x-webkit-airplay="true" webkit-playsinline="true" preload="auto" autoplay src="http://"></video>
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();
})
function isWeixin(){var ua = navigator.userAgent.toLowerCase();
if(ua.match(
/MicroMessenger/i)==
'micromessenger'){
return true;}
else{
return false;}
}
-webkit-transform: translate(-
50%,-
50%) scale(-
0.5,
1)
.css {-webkit-transform-style: preserve-3d;-webkit-backface-visibility: hidden;-webkit-perspective: 1000;
}
.css {-webkit-
transform: translate3d(0,0,0);-moz-
transform: translate3d(0,0,0);-ms-
transform: translate3d(0,0,0);transform: translate3d(0,0,0);
}
var autoScale =
function(){var ratio =
320/
504, winW = document.getElement.clientWidth,winH = document.getElement.clientHeight,ratio2 = winW/winH,scale;
if(ratio<ratio2){scale = (winH/
504).toString().substring(
0,
6);}
else{scale = (winW/
320).toString().substring(
0,
6); }
var cssText =
'-webkit-transform: scale('+scale+
');-webkit-transform-origin: top; opacity:1;' $(
'.wrap').attr(
'style', cssText);
}
setTimeout(
function(){if(document.documentElement.clientWidth/document.documentElement.clientHeight !==
320/
504){autoScale();}
else{$(
'.page').css({
'opacity':
1});}
},
300)
window.addEventListener(
'onorientationchange' in window?
'orientationchange':
'resize', autoScale,
false){detectOrientatioin();
}
function detectOrientatioin(){if(window.orientation==
180 || window.orientation==
0){}
if(window.orientation==
90 || window.orientation==-
90){}
}
常用的移動端框架
zepto.js
- 官網
- 中文網
- 瀏覽器檢測
- tap事件
總結
以上是生活随笔為你收集整理的H5项目常见问题及注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。