h5键盘弹起遮挡输入框

350 阅读1分钟

话不多说直接上代码

方法用来将不在浏览器窗口的可见区域内的元素滚动到浏览器窗口的可见区域。 如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动。

window.addEventListener('resize', function () {
    if (
        document.activeElement.tagName === 'InputItem' ||
        document.activeElement.tagName === 'textarea'
    ) {
        window.setTimeout(function () {
            if ('scrollIntoView' in document.activeElement) {
                document.activeElement.scrollIntoView()
            } else {
                document.activeElement.scrollIntoViewIfNeeded()
            }
        }, 0)
    }
})
```
```