Fork me on GitHub

jQuery
UI
Scrollable

Overview

A jQuery UI widget that enables monitoring, querying, or changing the scroll position of an element relative to a scrolling container. Read about the journey to get here on my blog post - benknowscode.com/2013/07/detect-dom-element-scrolled-with-jquery

Features

Download

Either download the Minified or the Full source and add it after the normal jQuery UI scripts.

Usage

A scrolling element is either the window or an element with the overflow style set to auto or scroll

  $('#myelement').scrollable()
      

This will add myelement to the list of elements being monitored for their scroll position. If the element scrolls into the visible viewport defined by its container, then an in event is triggered. When it scrolls out, an out event is triggered. Each event is triggered only one time when an element moves in and out of view.

Options

direction - Determines if monitoring/positioning will occur for either both, vertical only, or horizontal only. Defaults to both.

  $('#myelement').scrollable({ direction: 'vertical' });
      

offset - Adjust the logical size of the container such that the detection compares to a different sized box than the physical one displayed in the browser. Accepts pixels and percentages. Accepts left, top, right, and bottom to set each side individually or vertical/y and horizonal/x which will split the value evenly between the top/bottom and left/right, respectively. Any missing values will default to zero. Positive value shrink the box while negative expand it.

  // Adjust the bounds of the top/left side of the detection zone
        $('#myelement').scrollable({ offset: { left: '40%', top: '40%' } });

        // Adjust the bounds of the left/right only (20% for each) and leave top/bottom alone
        $('#myelement').scrollable({ offset: { x: '40%' } });
      

See the example to see how it works.

Methods

position - returns an object hash describing the state of the element relative to the scrolling container's visible viewport:

goto - Scrolls the element into the visible viewport. Uses jQuery.animate to smooth the transistion. Takes an optional config object with the following options:

See the example to experiment with the positioning options.

Events

in - triggered when the element scrolls into the defined viewport.

  $('#myelement').scrollable({
           in: function ( e, ui ) {

              ...

           }
        });
      

or

  $('#myelement').on('scrollin', function ( e, ui ) {

              ...

        });
      

e is the original scroll event.

The ui object contains the following keys:

out - triggered when the element scrolls out of the defined viewport. Same parameters and usage as in.

Demos

This page serves as one of the demos. The navigation bar on the left will track along as you scroll down this page. Each topic has a header which are being tracked by Scrollable. As they enter the bottom of the window, the in event fires which handles updating the navigation bar. Click each section on the navigation menu on the left will animate the section into view using the goto method.