1. 程式人生 > >IOS11下fixed中input光標錯位問題

IOS11下fixed中input光標錯位問題

title mov () border out osi lock rap color

項目遇到了這個問題,故作了個臨時解決方案,暫時沒有想到更好的方法,查閱了網上的方案,也沒有找到完美的解決方案。

方案思路:

①彈窗打開時,阻止 body 滾動,禁用 touchmove ,同時記錄當前 body 的滾動高度 startScrollTop

②彈窗關閉時,恢復 body 滾動,同時給當前滾動條賦值 startScrollTop ,恢復到彈窗前的高度

③關閉輸入鍵盤時,同樣恢復到彈窗前的高度

實現的代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <
title>ios11光標錯位問題</title> <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script> <style type="text/css"> .wrap{ height
: 4000px; } button{ margin-bottom: 1000px; display: block; } .fixed{ position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center;
align-items: center; z-index: 2; box-sizing: border-box; background: rgba(0,0,0,0.2); } .mask{ position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255,0,0, 0.1); z-index: 1; } input{ padding: 10px; } .abs{ width: 200px; padding: 10px; position: absolute; z-index: 99; border: 3px solid #000; background: #fff; } </style> </head> <body> <h2>頂部</h2> <div class="wrap"> <button type="button" name="button" onclick="showDialog()">show dialog</button> <button type="button" name="button" onclick="showDialog()">show dialog</button> <button type="button" name="button" onclick="showDialog()">show dialog</button> </div> <div class="fixed" id="dialog" style="display: none;"> <div class="mask" onclick="hideDialog()"></div> <div class="abs"> <div><input type="text" onfocus="getDialogScrollTop()" onblur="setDialogScrollTop()" name="" value=""></div> <div><input type="text" onfocus="getDialogScrollTop()" onblur="setDialogScrollTop()" name="" value=""></div> <div><input type="text" onfocus="getDialogScrollTop()" onblur="setDialogScrollTop()" name="" value=""></div> </div> </div> <h2>底部</h2> <script> var startScrollTop = 0; function showDialog() { document.getElementById(dialog).style.display = "flex"; disabledScroll() getDialogScrollTop(); setDialogScrollTop(); } function hideDialog() { document.getElementById(dialog).style.display = "none"; enabledScroll() } // 取消默認事件 function stopPreventDefault(e) { e.preventDefault(); e.stopPropagation(); } // 添加 touchmove 事件 禁用滾動條滾動 function disabledScroll() { document.addEventListener("touchmove", stopPreventDefault, false); } // 取消 touchmove 事件 還原滾動條滾動,同時還原小鍵盤打開前的滾動高度 function enabledScroll() { document.removeEventListener("touchmove", stopPreventDefault); setTimeout(setDialogScrollTop, 200) } // 獲取當前滾動高度並賦值到 startScrollTop function getDialogScrollTop() { startScrollTop = document.body.scrollTop; } // 還原滾動高度 function setDialogScrollTop() { document.querySelectorAll(.fixed).forEach(function(ths) { ths.style.top = startScrollTop + px; }); document.body.scrollTop = startScrollTop } </script> </body> </html>

IOS11下fixed中input光標錯位問題