在javascript中禁用输入焦点上的滚动 [英] disable scrolling on input focus in javascript

查看:59
本文介绍了在javascript中禁用输入焦点上的滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当用户输入或点击< input type =text>< / input>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆