移动端,input、textarea滚动至可视区域

1、一般情况下

在移动端,点击input框之后,会弹出输入键盘。而内容input的内容也会自动滚动到可视区域内。

2、当父元素设置了overflow属性之后

在设置了overflow属性之后,点击input框之后,input却无法滚动到可视区域内,在此情况下,我们应该怎么做呢。

3、解决方案

1、父元素不要应用overflow属性

2、手动滚动当前活动元素滚动至可视区域

通过scrollIntoViewscrollIntoViewIfNeeded这两个api可使元素滚动至可视区域

如下是我的解决方案:

const h = document.body.scrollHeight;
window.onresize = function() {
    if (document.body.scrollHeight < h) {
        setTimeout(() => {
            document.activeElement.scrollIntoView({ behavior: "smooth" });
        });
    }
};

转载于:https://www.cnblogs.com/xieqian/p/10517702.html

你可能感兴趣的:(移动端,input、textarea滚动至可视区域)