手機端頁面在項目中遇到的一些問題及解決辦法


1.解決頁面使用overflow: scroll在iOS上滑動卡頓的問題?


首先你可能會給頁面的html和body增加了height: 100%, 然後就可能造成IOS上頁面滑動的卡頓問題。解決方案是:


(1) 看是否能把body和html的height: 100%去除掉。

(2) 在滾動的容器中增加:-webkit-overflow-scrolling: touch或者給body增加:body {overflow-x: hidden}。


2.ios頁面橡皮彈回效果遮擋頁面選項卡?


(1) 有時body和html的height: 100%去除掉問題可能就沒有了。

(2) 到達臨界值的時候在阻止事件默認行爲


var startY,endY;
//記錄手指觸摸的起點座標
$('body').on('touchstart',function (e) {
     startY = e.touches[0].pageY;
});
$('body').on('touchmove',function (e) {
     endY = e.touches[0].pageY;  //記錄手指觸摸的移動中的座標
     //手指下滑,頁面到達頂端不能繼續下滑
     if(endY>startY&& $(window).scrollTop()<=0){
         e.preventDefault();
     }
   //手指上滑,頁面到達底部能繼續上滑
     if(endY<startY&& $(window).scrollTop()+ 
         $(window).height()>=$('body')[0].scrollHeight){
         e.preventDefault();
     }
})

有時也會碰見彈窗出來後兩個層的橡皮筋效果出現問題,我們可以在彈出彈出時給底層頁面加上一個類名,類名禁止頁面滑動這樣下層的橡皮筋效果就會被禁止,就不會影響彈窗層。


3.IOS機型margin屬性無效問題?


(1) 設置html body的高度爲百分比時,margin-bottom在safari裏失效

(2) 直接padding代替margin


4.Ios綁定點擊事件不執行?


(1)添加樣式cursor :pointer。點擊後消除背景閃一下的css:-webkit-tap-highlight-color:transparent;


5.Ios鍵盤換行變爲搜索?


  • 首先,input 要放在 form裏面。

  • 這時 “換行” 已經變成 “前往”。

  • 如果想變成 “搜索”,input 設置 type=”search”。


6.Jq對a標籤點擊事件不生效?


出現這種情況的原因不明,有的朋友解釋:我們平時都是點擊的A標籤中的文字了。 所以要想用JS模擬點擊A標籤事件,就得先往A標籤中的文字添加能被JS捕獲的元素,然後再用JS模擬點擊該元素即可。但是我覺得不合理,雖然找不到原因但是解決辦法還是有的。


(1)document.getElementById(“abc “).click();

(2)$(“#abc “)[0].click();


7.有時因爲服務器或者別的原因導致頁面上的圖片沒有找到?


這是我們想需要用一個本地的圖片代替沒有找的的圖片


<script type="text/javascript"> 
function nofind(){
var img=event.srcElement;
img.src="images/logoError.png";
img.οnerrοr=null; 控制不要一直跳動
}
</script>
<img src="images/logo.png" onerror="nofind();" />


8.transform屬性影響position:fixed?


(1)規範中有規定:如果元素的transform值不爲none,則該元素會生成包含塊和層疊上下文。CSS Transforms Module Level 1不只在手機上,電腦上也一樣。除了fixed元素會受影響之外,z-index(層疊上下文)值也會受影響。絕對定位元素等和包含塊有關的屬性都會受到影響。當然如果transform元素的display值爲inline時又會有所不同。最簡單的解決方法就是transform元素內部不能有absolute、fixed元素.


9.ios對position: fixed不太友好,有時我們需要加點處理?


在安卓上面,點擊頁面底部的輸入框,軟鍵盤彈出,頁面移動上移。

而ios上面,點擊頁面底部輸入框,軟鍵盤彈出,輸入框看不到了。。。查資料說什麼的都有,iscroll,jquery-moblie,absolute,fixe,static都非常複雜,要改很多。。。

讓他彈出時讓滾動條在最低部


var u = navigator.userAgent, app = navigator.appVersion;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
if (isiOS) {
   $(<span class="" style="color: rgb(166, 226, 46);word-wrap: inherit !important;word-break: inherit !important;">'textarea'</span>).focus(<span class="" style="word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="color: rgb(249, 38, 114);font-weight: bold;word-wrap: inherit !important;word-break: inherit !important;">function</span> (<span class="" style="color: rgb(255, 152, 35);word-wrap: inherit !important;word-break: inherit !important;"></span>) </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span class="" style="color: rgb(166, 226, 46);word-wrap: inherit !important;word-break: inherit !important;">window</span>.setTimeout(<span class="" style="color: rgb(166, 226, 46);word-wrap: inherit !important;word-break: inherit !important;">'scrollBottom()'</span>, <span class="" style="word-wrap: inherit !important;word-break: inherit !important;">500</span>);<br> &nbsp; &nbsp;});<br>}<br><span class="" style="word-wrap: inherit !important;word-break: inherit !important;"><span class="" style="color: rgb(249, 38, 114);font-weight: bold;word-wrap: inherit !important;word-break: inherit !important;">function</span> <span class="" style="color: rgb(166, 226, 46);font-weight: bold;word-wrap: inherit !important;word-break: inherit !important;">scrollBottom</span>(<span class="" style="color: rgb(255, 152, 35);word-wrap: inherit !important;word-break: inherit !important;"></span>) </span>{<br> &nbsp; &nbsp;<span class="" style="color: rgb(166, 226, 46);word-wrap: inherit !important;word-break: inherit !important;">window</span>.scrollTo(<span class="" style="word-wrap: inherit !important;word-break: inherit !important;">0</span>, $('body').height());
}


10.jq validate插件驗證問題?


(1)所以的input必須有name不然會出錯


11.有時手機會出現斷網的情況,我沒可能會對斷網的情況做一些處理?


(1)navigator.onLine可判斷是否是脫機狀態.


12.判斷對象的長度?


(1)用Object.keys,Object.keys方法返回的是一個數組,數組裏面裝的是對象的屬性。


var person = {
   "name" : "zhangshan",
   "sex" : "man",
   "age" : "50",
   "height" : "180",
   "phone" : "1xxxxxxxxxx",
   "email" : "[email protected]"
};
var arr = Object.keys(person);
console.log(arr.length);


(2)Object.getOwnPropertyNames(obj).length


13.上一題我們用到了Object.keys與Object.getOwnPropertyNames他們的區別?


Object.keys定義:返回一個對象可枚舉屬性的字符串數組;

Object.getOwnPropertyNames定義:返回一個對象可枚舉、不可枚舉屬性的名稱;

屬性的可枚舉性、不可枚舉性:定義:可枚舉屬性是指那些內部 “可枚舉” 標誌設置爲 true 的屬性,對於通過直接的賦值和屬性初始化的屬性,該標識值默認爲即爲 true,對於通過 Object.defineProperty 等定義的屬性,該標識值默認爲 false。


var obj = { "prop1": "v1" };
Object.defineProperty(obj, "prop2", { value: "v2", enumerable: false });
console.log(Object.keys(obj).length);           //output:1
console.log(Object.getOwnPropertyNames(obj).length);    //output:2
console.log(Object.keys(obj));           //output:Array[1] => [0: "prop1"]
console.log(Object.getOwnPropertyNames(obj));    //output:Array[2] => [0: "prop1", 1: "prop2"]


這裏寫圖片描述


綜合實例:


var obj = { "prop1": "v1" };
Object.defineProperty(obj, "prop2", { value: "v2", enumerable: false});
console.log(obj.hasOwnProperty("prop1")); //output: true
console.log(obj.hasOwnProperty("prop2")); //output: true
console.log(obj.propertyIsEnumerable("prop1")); //output: true
console.log(obj.propertyIsEnumerable("prop2")); //output: false
console.log('prop1' in obj);    //output: true
console.log('prop2' in obj);    //output: true
for (var item in obj) {
   console.log(item);
}
//output:prop1
for (var item in Object.getOwnPropertyNames(obj)) {
   console.log(Object.getOwnPropertyNames(obj)[item]);
}
//ouput:[prop1,prop2]


14.移動開發不同手機彈出數字鍵盤問題?


(1)type=”tel”

iOS和Android的鍵盤表現都差不多

(2)type=”number”

優點是Android下實現的一個真正的數字鍵盤

缺點一:iOS下不是九宮格鍵盤,輸入不方便

缺點二:舊版Android(包括微信所用的X5內核)在輸入框後面會有超級雞肋的小尾巴,好在Android 4.4.4以後給去掉了。

不過對於缺點二,我們可以用webkit私有的僞元素給fix掉:


input[type=number]::-webkit-inner-spin-button,  
input[type=number]::-webkit-outer-spin-button {
       -webkit-appearance: none;
       appearance: none;
       margin: 0;
}


(3)pattern屬性


pattern用於驗證表單輸入的內容,通常HTML5的type屬性,比如email、tel、number、data類、url等,已經自帶了簡單的數據格式驗證功能了,加上pattern後,前端部分的驗證更加簡單高效了。

顯而易見,pattern的屬性值要用正則表達式。

實例 簡單的數字驗證

數字的驗證有兩個:

<input type=”number” pattern=”d”> 

<input type=”number” pattern=”[0-9]*”>


15.input[number]類型輸入非數字字符


js獲取的值是空;比如-12,+123等


16.Javascript:history.go()和history.back()的用法與區別?


簡單的說就是:go(-1):返回上一頁,原頁面表單中的內容會丟失;back():返回上一頁,原頁表表單中的內容會保留。history.go(-1):後退+刷新history.back():後退

之所以注意到這個區別,是因爲不同的瀏覽器後退行爲也是有區別的,而區別就跟javascript:history.go()和history.back()的區別類似。

Chrome和ff瀏覽器後退頁面,會刷新後退的頁面,若有數據請求也會提交數據申請。類似於history.go(-1);

而safari(包括桌面版和ipad版)的後退按鈕則不會刷新頁面,也不會提交數據申請。類似於javascript:history.back();


17.Meta基礎知識:


<meta name="viewport"content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
// width    設置viewport寬度,爲一個正整數,或字符串‘device-width’
// height   設置viewport高度,一般設置了寬度,會自動解析出高度,可以不用設置
// initial-scale    默認縮放比例,爲一個數字,可以帶小數
// minimum-scale    允許用戶最小縮放比例,爲一個數字,可以帶小數
// maximum-scale    允許用戶最大縮放比例,爲一個數字,可以帶小數
// user-scalable    是否允許手動縮放
空白頁基本meta標籤
<!-- 設置縮放 -->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
<!-- 可隱藏地址欄,僅針對IOS的Safari(注:IOS7.0版本以後,safari上已看不到效果) -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<!-- 僅針對IOS的Safari頂端狀態條的樣式(可選default/black/black-translucent ) -->
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<!-- IOS中禁用將數字識別爲電話號碼/忽略Android平臺中對郵箱地址的識別 -->
<meta name="format-detection"content="telephone=no, email=no" />
其他meta標籤
<!-- 啓用360瀏覽器的極速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 針對手持設備優化,主要是針對一些老的不識別viewport的瀏覽器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微軟的老式瀏覽器 -->
<meta name="MobileOptimized" content="320">
<!-- uc強制豎屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ強制豎屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC強制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ強制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC應用模式 -->
<meta name="browsermode" content="application">
<!-- QQ應用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 點擊無高光 -->
<meta name="msapplication-tap-highlight" content="no">


18.移動端如何定義字體font-family?


@ ————————————–中文字體的英文名稱

@ 宋體 SimSun

@ 黑體 SimHei

@ 微信雅黑 Microsoft Yahei

@ 微軟正黑體 Microsoft JhengHei

@ 新宋體 NSimSun

@ 新細明體 MingLiU

@ 細明體 MingLiU

@ 標楷體 DFKai-SB

@ 仿宋 FangSong

@ 楷體 KaiTi

@ 仿宋_GB2312 FangSong_GB2312

@ 楷體_GB2312 KaiTi_GB2312 

@

@ 說明:中文字體多數使用宋體、雅黑,英文用Helvetica


body { font-family: Microsoft Yahei,SimSun,Helvetica; }


19.打電話發短信寫郵件怎麼實現?


// 一、打電話
<a href="tel:0755-10086">打電話給:0755-10086</a>
//  二、發短信,winphone系統無效
<a href="sms:10086">發短信給: 10086</a>
// 三、寫郵件
<a href="mailto:[email protected]">點擊我發郵件</a>
//2.收件地址後添加?cc=開頭,可添加抄送地址(Android存在兼容問題)
<a href="mailto:[email protected][email protected]">點擊我發郵件</a>
//3.跟着抄送地址後,寫上&bcc=,可添加密件抄送地址(Android存在兼容問題)
<a href="mailto:[email protected][email protected]&[email protected]">點擊我發郵件</a>
//4.包含多個收件人、抄送、密件抄送人,用分號(;)隔開多個郵件人的地址
<a href="mailto:[email protected];[email protected]">點擊我發郵件</a>
//5.包含主題,用?subject=
<a href="mailto:[email protected]?subject=郵件主題">點擊我發郵件</a>
//6.包含內容,用?body=;如內容包含文本,使用%0A給文本換行
<a href="mailto:[email protected]?body=郵件主題內容%0A騰訊誠信%0A期待您的到來">點擊我發郵件</a>
//7.內容包含鏈接,含http(s)://等的文本自動轉化爲鏈接
<a href="mailto:[email protected]?body=http://www.baidu.com">點擊我發郵件</a>
//8.內容包含圖片(PC不支持)
<a href="mailto:[email protected]?body=<img src='images/1.jpg' />">點擊我發郵件</a>
//9.完整示例
<a href="mailto:[email protected];[email protected][email protected]&[email protected]&subject=[郵件主題]&body=騰訊誠邀您參與%0A%0Ahttp://www.baidu.com%0A%0A<img src='images/1.jpg' />">點擊我發郵件</a>


20.移動端touch事件(區分webkit和winphone)?


// 以下支持webkit

touchstart——當手指觸碰屏幕時候發生。不管當前有多少隻手指

touchmove——當手指在屏幕上滑動時連續觸發。通常我們再滑屏頁面,會調用event的preventDefault()可以阻止默認情況的發生:阻止頁面滾動

touchend——當手指離開屏幕時觸發

touchcancel——系統停止跟蹤觸摸時候會觸發。例如在觸摸過程中突然頁面alert()一個提示框,此時會觸發該事件,這個事件比較少用


//TouchEvent說明:

touches:屏幕上所有手指的信息

targetTouches:手指在目標區域的手指信息

changedTouches:最近一次觸發該事件的手指信息

touchend時,touches與targetTouches信息會被刪除,changedTouches保存的最後一次的信息,最好用於計算手指信息


//參數信息(changedTouches[0])

clientX、clientY在顯示區的座標

target:當前元素


//事件響應順序

ontouchstart > ontouchmove > ontouchend > onclick


21.點擊元素產生背景或邊框怎麼去掉


//ios用戶點擊一個鏈接,會出現一個半透明灰色遮罩, 如果想要禁用,可設置-webkit-tap-highlight-color的alpha值爲0去除灰色半透明遮罩;
//android用戶點擊一個鏈接,會出現一個邊框或者半透明灰色遮罩, 不同生產商定義出來額效果不一樣,可設置-webkit-tap-highlight-color的alpha值爲0去除部分機器自帶的效果;
//winphone系統,點擊標籤產生的灰色半透明背景,能通過設置<meta name="msapplication-tap-highlight" content="no">去掉;
//特殊說明:有些機型去除不了,如小米2。對於按鈕類還有個辦法,不使用a或者input標籤,直接用div標籤
a,button,input,textarea {
   -webkit-tap-highlight-color: rgba(0,0,0,0);
   -webkit-user-modify:read-write-plaintext-only; //-webkit-user-modify有個副作用,就是輸入法不再能夠輸入多個字符
}  
// 也可以
* { -webkit-tap-highlight-color: rgba(0,0,0,0); }
//winphone下
<meta name="msapplication-tap-highlight" content="no">


22.美化表單元素


//一、使用appearance改變webkit瀏覽器的默認外觀

input,select { -webkit-appearance:none; appearance: none; }


//二、winphone下,使用僞元素改變表單元素默認外觀

//1.禁用select默認箭頭,::-ms-expand修改表單控件下拉箭頭,設置隱藏並使用背景圖片來修飾

select::-ms-expand { display:none; }


//2.禁用radio和checkbox默認樣式,::-ms-check修改表單複選框或單選框默認圖標,設置隱藏並使用背景圖片來修飾

input[type=radio]::-ms-check,

input[type=checkbox]::-ms-check { display:none; }


//3.禁用pc端表單輸入框默認清除按鈕,::-ms-clear修改清除按鈕,設置隱藏並使用背景圖片來修飾

input[type=text]::-ms-clear,

input[type=tel]::-ms-clear,

input[type=number]::-ms-clear { display:none; }


23.移動端字體單位font-size選擇px還是rem?


// 如需適配多種移動設備,建議使用rem。以下爲參考值:

html { font-size: 62.5%; } //10*16 = 62.5%

//設置12px字體 這裏注意在rem前要加上對應的px值,解決不支持rem的瀏覽器的兼容問題,做到優雅降級

body { font-size:12px; font-size:1.2rem; }


24.input標籤添加上disable屬性在ios端字體顏色不兼容的問題?


input[disabled],input:disabled,input.disabled{
 color: #3e3e3e;  
 -webkit-text-fill-color: #3e3e3e;  
 -webkit-opacity:1;  
 opacity: 1;  
}


25.IOS 的光標大小問題


IE:不管該行有沒有文字,光標高度與font-size一致。 

FF:該行有文字時,光標高度與font-size一致。該行無文字時,光標高度與input的height一致。 

Chrome:該行無文字時,光標高度與line-height一致;該行有文字時,光標高度從input頂部到文字底部(這兩種情況都是在有設定line-height的時候),如果沒有line-height,則是與font-size一致。

IOS中情況和Chrome 相似。


設置字體大小和行高一致,然後通過 padding 撐開大小

只給IE瀏覽器設置 line-height

-ms-line-height:40px;



發佈了238 篇原創文章 · 獲贊 269 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章