api

1
2
3
button.focus({ 
preventScroll: true
});
1
2
3
4
5
6
7
<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>
1
2
3
4
5
6
7
8
button1.onclick = function () {
input1.focus();
};
button2.onclick = function () {
input2.focus({
preventScroll: true
});
};

demo

猛戳查看

兼容情况

64+ ✔ 68+ ✔ 51+ ✔