Prevent Scrolling

by rodneyrehm

HTML

<div></div>

CSS

div { height: 10000px; }

JavaScript

(function($, document, undefined){
/*
   scroll-sensitive keys:
         9:  Tab (A11Y much?)
        32: Space
        33: PageUp
        34: PageDown
        35: End
        36: Home
        37: Left
        38: Up
        39: Right
        40: Down
 */
var $doc = $(document),
    scrollTop = $.fn.scrollTop;

$.fn.scrollTop = function(offset) {
    if (offset === "currentposition") {
        offset = this.data('currentscrollposition');
    } else {
        this.data('currentscrollposition', offset);
    }
    
    return scrollTop.call(this, offset);
};

// what about touchstart?

$doc.scrollTop(500)
    .on("DOMMouseScroll mousewheel", function(e) {
        // prevent scrolling by mousewheel
        e.preventDefault();
        e.stopImmediatePropagation();
    }).on("keydown", function(e){
        // prevent scrolling by keyboard
        // TODO: check if focus on <input|textarea|select>
        if (e.keyCode === 9 || e.keyCode >= 32 && e.keyCode <= 40) {
          e.preventDefault();
          e.stopImmediatePropagation();
        }
    }).on("scroll", function() {
        // prevent scrolling by anything else
        console.log("scrolled!");
        $doc.scrollTop("currentposition");
    }).click(function(){ $doc.scrollTop(1000); });



})(jQuery, document);