advscrollevent

jQuery plug-in providing advanced scroll events. These might, for example, trigger an event, whenever a window or frame content has been scrolled up or down by at least a certain amount of pixels, optionally only after a change of the scroll direction.

Project home page

What is this?

This software module contains a jQuery plug-in building on jQuery’s native scroll event: By applying jQuery’s .scroll() method to selected scrollable content (typically, but not limited to, $(window)), you may register an event handler wich gets called whenever the content is scrolled. With jQuery’s methods scrollTop resp. scrollLeft the event handler may then determine the acutal position of the viewport.

For many purposes, those native functions are ‘a bit low-level’, and this plug-in builds upon them in order to define ‘higher-level’ events such as: User has scrolled upwards for at least 20px for the first time after scrolling downwards. The plug-in provides a couple of options (like the 20px threshold in the previous example) which, among others, help prevent accidentally triggering events only due un impreciseness (such as minimal scrolling movements caused by a user just laying his finger onto a touch screen with a little, unintended movement).

Examples

This project contains one demo page named demo.html, which contains some demonstrations. As one possible use-case, it shows how tool bars or panels may be hidden (e.g. on a mobile site layout) when the user scrolls down, probably reading the text of the page, whereas the panels are shown again, whenever the user scrolls upwards or reaches the bottom of the page, assuming that in both cases he doesn’t read the main content any more and should be provided with more means of interaction.

Open the demo page in a browser to try out the example configuration, see the html source (including embedded javascript) to see how the configuration was done.

Feel encouraged to make copies of the demo file play around with the options to see what happens.

JavaScripts

Usage

Basics

Options

As already established above, the advScroll() function takes exactly one argument, which has to be a JavaScript object defining options via its properties. The following option properties are defined:

Options for event handler function

Other options

If you want to configure the oncePerDirection or directionChangeDelayMillis settings separately for both scrolling directions, you may use the following options:

Overwriting default options

The descriptions for the options above contain default values wich apply, if you don’t add that option key to the argument object you pass to the plug-in function.

Any defaults (except the undefined ones) are taken from the object $.fn.advScroll.defaults.

It is possible for you to override these defaults by simply modifying said object before first calling the plug-in function.

License: BSD 2-clause

Copyright (c) 2016, Immo Schulz-Gerlach, www.isg-software.de
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.