在javascript中禁用输入焦点上的滚动 [英] disable scrolling on input focus in javascript
问题描述
我想知道当用户输入或点击< input type =text>< / input> $时,是否可以停止使用javascript滚动页面c $ c>
I was wondering if it was possible to stop the page from scrolling using javascript when a user type or clicks on a <input type="text"></input>
推荐答案
所以我想出了如何做到这一点,我将把这作为其他任何人的记录需要解决这个问题。 (注意:此解决方案仅在safari和Firefox上测试过)
So I figured out how to accomplish this, and I will leave this as a record for anyone else who needs to solve this problem. (NOTE : this solution has only been tested on safari and Firefox)
for:
<input id="text" type="text" />
功能
document.getElementById('text').onkeydown = new function(event){return false}
不会导致窗口移动滚动,以便用户在键入字段时可以看到输入字段。如果像我这样想要这对某些字母发生,而对其他人来说则不然,只需编辑onkeydown函数的内容,以便它对某些键码返回false,对其他键码则返回true。
Will not cause the window to shift the scroll so that a user can see the input field when he types into the field. If like me you want this to happen for some letters but not for others simply edit the contents of the onkeydown function so that it returns false for certain keycodes and true for others.
这篇关于在javascript中禁用输入焦点上的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!