Eli Grey

RetargetMouseScroll

Yesterday I made a simple JavaScript micro-library named RetargetMouseScroll that provides a simple API to retarget/redirect mouse scroll events. I named it this because RedirectMouseScroll sounded weird. At first glance it seems pretty useless but there are some cool things you can do with the library which are listed in the examples section of this post.

Demo

The test suite functions as a great demo.

Usage

RetargetMouseScroll(
    element         :Node,    // default: document
    targetWindow    :Window,  // default: window
    preventDefault  :Boolean, // default: true
    scrollMultiplier:Number   // default: 1.0
)

RetargetMouseScroll returns an object containing a restore method. Calling the method restores the default scrolling.

Examples

  • RetargetMouseScroll(myElement, myFrame) – Per-element mouse scroll retargetting to a frame
  • RetargetMouseScroll(document, window, true, 0.5) – Slow down scrolling
  • RetargetMouseScroll(document, window, true, -1) – Invert scrolling
  • RetargetMouseScroll(document, window, true, 2) – Speed up scrolling

More advanced example using a popup:

var win = window.open("/", "example", "w=" + (screen.availWidth / 3.5)
                                      + ",h=" + (screen.availHeight / 3.5)
                                      + ",scrollbars=yes,resize=yes");
win.addEventListener("DOMContentLoaded", function() {
    var retargetting = RetargetMouseScroll(document, win);
    win.onunload = function () {
        retargetting.restore();
    };
}, false);

In the previous example, all mouse scrolling on the main document is retargetted to the popup until it is closed.

Leave a Reply