Disable scrolling on input focus in javascript

I was wondering if javascript can stop scrolling a page when a user types in or clicks on <input type="text"></input>

+4
source share
3 answers

So, I figured out how to do this, and I will leave it as a post for anyone who needs to solve this problem. (NOTE: this solution has only been tested on safari and Firefox)

for

<input id="text" type="text" />

function

document.getElementById('text').onkeydown = new function(event){return false}

, , , . , , , some, , onkeydown, false true .

+2

. : focus, -.

, , , .

$(function() {
    var oldScroll = window.onscroll;
    $(document).on('focus', 'input', function(e) {
        window.onscroll = function () { 
            window.scroll(0,0); 
        } ;
        setTimeout(function() {
            window.onscroll = oldScroll;
        }, 100);
    });

});

http://jsfiddle.net/N4da4/8/

+1

You can set the scrollTop property to x every n milliseconds. The problem with this approach, I would think that the page will look jerky (technical term) when the user tries to scroll the page.

In addition, you violate the user's expectations so that they can scroll the page at any time, which I would recommend, as this could confuse them at best and at worst annoy them.

0
source

All Articles