1、一般情況下
在移動端,點擊input框之后,會彈出輸入鍵盤。而內容input的內容也會自動滾動到可視區域內。
2、當父元素設置了overflow屬性之后
在設置了overflow屬性之后,點擊input框之后,input卻無法滾動到可視區域內,在此情況下,我們應該怎么做呢。
3、解決方案
1、父元素不要應用overflow屬性
2、手動滾動當前活動元素滾動至可視區域
通過scrollIntoView
或scrollIntoViewIfNeeded
這兩個api可使元素滾動至可視區域
如下是我的解決方案:
const h = document.body.scrollHeight;
window.onresize = function() {
if (document.body.scrollHeight < h) {
setTimeout(() => {
document.activeElement.scrollIntoView({ behavior: "smooth" });
});
}
};