總結(jié)幾個(gè)移動(dòng)端H5軟鍵盤(pán)的大坑
1. 部分機(jī)型軟鍵盤(pán)彈起擋住原來(lái)的視圖
解決方法:可以通過(guò)監(jiān)聽(tīng)移動(dòng)端軟鍵盤(pán)彈起。
Element.scrollIntoView() 方法讓當(dāng)前的元素滾動(dòng)到瀏覽器窗口的可視區(qū)域內(nèi)。參數(shù)如下:
- true,表示元素的頂部與當(dāng)前區(qū)域的可見(jiàn)部分的頂部對(duì)齊
- false,表示元素的底部與當(dāng)前區(qū)域的可見(jiàn)部分的尾部對(duì)齊
Element.scrollIntoViewIfNeeded()方法也是用來(lái)將不在瀏覽器窗口的可見(jiàn)區(qū)域內(nèi)的元素滾動(dòng)到瀏覽器窗口的可見(jiàn)區(qū)域。但如果該元素已經(jīng)在瀏覽器窗口的可見(jiàn)區(qū)域內(nèi),則不會(huì)發(fā)生滾動(dòng)。此方法是標(biāo)準(zhǔn)的Element.scrollIntoView()方法的專有變體。
- window.addEventListener('resize', function() {
- if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') {
- window.setTimeout(function() {
- if ('scrollIntoView' in document.activeElement) {
- document.activeElement.scrollIntoView(false)
- } else {
- document.activeElement.scrollIntoViewIfNeeded(false)
- }
- }, 0)
- }
- })
2. ios 鍵盤(pán)收起時(shí)頁(yè)面沒(méi)有回落,底部會(huì)留白
部分蘋(píng)果手機(jī)填寫(xiě)表單的時(shí)候的,輸入內(nèi)容后關(guān)閉軟鍵盤(pán),底部會(huì)留一塊空白。這種情況可以通過(guò)監(jiān)聽(tīng)鍵盤(pán)回落時(shí)間滾動(dòng)到原來(lái)的位置。
- window.addEventListener('focusout', function() {
- window.scrollTo(0, 0)
- })
- //input輸入框彈起軟鍵盤(pán)的解決方案。
- var bfscrolltop = document.body.scrollTop
- $('input')
- .focus(function() {
- documentdocument.body.scrollTop = document.body.scrollHeight
- //console.log(document.body.scrollTop);
- })
- .blur(function() {
- document.body.scrollTop = bfscrolltop
- //console.log(document.body.scrollTop);
- })
3. onkeyUp 和 onKeydown 兼容性問(wèn)題
部分 ios 機(jī)型 中 input 鍵盤(pán)事件 keyup、keydown、等支持不是很好, 用 input 監(jiān)聽(tīng)鍵盤(pán) keyup 事件,在安卓手機(jī)瀏覽器中沒(méi)有問(wèn)題,但是在 ios 手機(jī)瀏覽器中用輸入法輸入之后,并未立刻相應(yīng) keyup 事件:
- onkeypress 用戶按下并放開(kāi)任何字母數(shù)字鍵時(shí)發(fā)生。系統(tǒng)按鈕(箭頭鍵和功能鍵)無(wú)法得到識(shí)別。
- onkeyup 用戶放開(kāi)任何先前按下的鍵盤(pán)鍵時(shí)發(fā)生。
- onkeydown 用戶按下任何鍵盤(pán)鍵(包括系統(tǒng)按鈕,如箭頭鍵和功能鍵)時(shí)發(fā)生。
4. ios12 輸入框難以點(diǎn)擊獲取焦點(diǎn),彈不出軟鍵盤(pán)
定位找到問(wèn)題是 fastclick.js 對(duì) ios12 的兼容性,可在 fastclick.js 源碼或者 main.js 做以下修改:
- FastClick.prototype.focus = function(targetElement) {
- var length
- if (deviceIsIOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
- length = targetElement.value.length
- targetElement.setSelectionRange(length, length)
- targetElement.focus()
- } else {
- targetElement.focus()
- }
- }
5. fastclick 導(dǎo)致下拉框焦點(diǎn)沖突
移動(dòng)端使用 fastclick 之后,在 ios 環(huán)境下,有幾個(gè)連續(xù)的下拉框 第一個(gè) select 框突然填充了第二個(gè)下拉框的內(nèi)容。
根本原因是 Fastclick 導(dǎo)致 ios 下多個(gè) select ,點(diǎn)擊某一個(gè),焦點(diǎn)不停變換的 bug。修改源碼,在 onTouchStart 事件內(nèi)判斷設(shè)備是否為 ios,再判斷當(dāng)前 nodeName 是否為 select,如果是 return false 去阻止 fastClick 執(zhí)行其他事件。
- //line 391行
- FastClick.prototype.onTouchStart = function(event) {
- //在其方法中添加判斷 符合ios select的時(shí)候 不返回事件
- if (deviceIsIOS && this.targetElement == 'select') this.targetElement = null
- event.preventDefault()
- }
- //line521 或者講源碼中 有關(guān)touchEnd判斷非ios或者非select的事件注釋,
- if (!deviceIsIOS || targetTagName !== 'select') {
- this.targetElement = null
- event.preventDefault()
- }
6. ios 下 fixed 失效的原因
軟鍵盤(pán)喚起后,頁(yè)面的 fixed 元素將失效,變成了 absolute,所以當(dāng)頁(yè)面超過(guò)一屏且滾動(dòng)時(shí),失效的 fixed 元素就會(huì)跟隨滾動(dòng)了。不僅限于 type=text 的輸入框,凡是軟鍵盤(pán)(比如時(shí)間日期選擇、select 選擇等等)被喚起,都會(huì)遇到同樣地問(wèn)題。
解決方法: 不讓頁(yè)面滾動(dòng),而是讓主體部分自己滾動(dòng),主體部分高度設(shè)為 100%,overflow:scroll
- <body>
- <div class='warper'>
- <div class='main'></div>
- <div>
- <div class="fix-bottom"></div>
- </body>
- .warper {
- position: absolute;
- width: 100%;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- overflow-y: scroll;
- -webkit-overflow-scrolling: touch; /* 解決ios滑動(dòng)不流暢問(wèn)題 */
- }
- .fix-bottom {
- position: fixed;
- bottom: 0;
- width: 100%;
- }
7. ios 鍵盤(pán)換行變?yōu)樗阉?/strong>
- input type="search"
- input 外面套 form,必須要有 action,action="javascript:return true"
- 表單提交阻止默認(rèn)提交事件
- <form action="javascript:return true" @submit.prevent="formSubmit">
- <input type="search" placeholder="請(qǐng)輸入訴求名稱" id="search" />
- </form>