移动端输入框弹起影响布局(包括fixed布局情况)

使用  window.scrollIntoViewIfNeeded

具体代码

 window.addEventListener('resize',function(){

    if(document.activeElement.tagName === "INPUT" || document.activeElement.tagName === "TEXTAREA"){

        window.setTimeout(function(){
            document.activeElement.scrollIntoViewIfNeeded();
        },20)

    }
})

 

监听resize,当是input或者textarea的时候,触发此函数

 

(PS: ①、使用 === 而不是 ==  的原因是, === 比 == 的速度要快;②、setTimeout最小执行毫秒数是20,而不是0,就算设置是0,也会有20ms的延迟。通常情况下,这段代码可满足。 详情查阅MDN)

猜你喜欢

转载自blog.csdn.net/weixin_38747509/article/details/80652058