安卓和ios鍵盤擋住輸入內容

來源:互聯網
上載者:User

標籤:計時器   fse   top   time   一個   remove   cto   efault   滾動   

<script>
let iosUser = window.navigator.userAgent.indexOf(‘iPhone‘)
let inp = document.querySelector(‘#inp‘);//input輸入框
if (iosUser != -1) {
var bfscrolltop = document.body.scrollTop;//擷取軟鍵盤喚起前瀏覽器滾動部分的高度
inp.focus(function () {//在這裡‘input.inputframe’是我的底部輸入欄的輸入框,當它擷取焦點時觸發事件
interval = setInterval(function () {//設定一個計時器,時間設定與軟鍵盤彈出所需時間相近
document.body.scrollTop = document.body.scrollHeight;//擷取焦點後將瀏覽器內所有內容高度賦給瀏覽器滾動部分高度
}, 100)
}).blur(function () {//設定輸入框失去焦點時的事件
clearInterval(interval);//清除計時器
document.body.scrollTop = bfscrolltop;//將軟鍵盤喚起前的瀏覽器滾動部分高度重新賦給改變後的高度
});
} else {
inp.onclick = function (ev) {
document.querySelector(‘body‘).style.height = ‘999px‘;
inp.style.position = ‘static‘;
setTimeout(function () {
document.body.scrollTop = document.documentElement.scrollTop = inp.getBoundingClientRect().top + pageYOffset - 300;
}, 50);
window.addEventListener(‘touchmove‘, fn, false);
}

inp.onblur = function () {
document.querySelector(‘body‘).style.height = "auto"
document.querySelector(‘body‘).removeAttribute(‘style‘)
window.removeEventListener(‘touchmove‘, fn, false)
}

//觸摸取消blur
function fn(ev) {
var _target = ev.target || ev.srcElement;
if (_target.nodeName != ‘INPUT‘) {
inp.blur();
}
ev.preventDefault()
}
}
</script>

安卓和ios鍵盤擋住輸入內容

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.