JS focus preventScroll参数使用实例页面
回到相关文章 »效果:
提示:浏览器可视区域的下面有两个输入框。
代码:
HTML代码:
<button id="button1">点击我获取焦点会定位</button>
<button id="button2">点击我获取焦点但不会定位</button>
<p class="remind">提示:浏览器可视区域的下面有两个输入框。</p>
<div style="margin-top: calc(100vh - 180px);">
<input id="input1" size="10">
<input id="input2" size="10">
</div>
JS代码:
button1.onclick = function () {
input1.focus();
};
button2.onclick = function () {
input2.focus({
preventScroll: true
});
};