jQuery.stickOnScroll

A jQuery plugin for making elements fixed on the top of the page when scrolling and scrolling that item up when it a specified footer element gets "close".

Demo

This page is using the plugin to stick the help topic headers (to the right) to the top of the page as you scroll down.

See the following standalone demo pages:
Example 01 - Simple usage.
Example 02 - Several items fixed on both window and fixed height elements.

View the Project on GitHub purtuga/jquery.stickOnScroll

jQuery.stickOnScroll

Usage

Given the following html:

<p>some text here</p>
<div class="section-title-container">
    <h2 style="width: 100%;">Section 1</h2>
</div>
<p>Some text here</p>
<p>Some text here</p>
<p>Some text here</p>
<p>Some text here</p>
<p>Some text here</p>
<p>Some text here</p>
<p id="section1footer">Some text here</p>

And the following javascript code:

var ele = $("div.section-title-container");
ele.css("height", ele.outerHeight())
    .find("h2")
        .stickOnScroll({
            footerElement:  $("#section1footer"),
            bootomOffset:   20,
            setParentOnStick: true
        });
});

Will make the h2 heading "Section 1" stick to the top of the page (css position: fixed) and will watch for when the footer element (the paragraph with id = section1footer) comes within 20 pixels of the bottom of the h2 element at which point, the header is scrolled up with the normal flow of the page.

In the example above the height of the div.section-title-container is set prior to calling stickOnScroll(). This was just for demonstrations purposes and could have been achieved via other methods (ie. css). The reason for setting the height of the element around the header is that once the header is fixed on the page, it is removed from the normal flow of the document. By setting the surrounding element's height, the document structure should be maintained when the header is moved to position: position.

NOTE Since this is such a common need, a new input option, setParentOnStick, has been added, wich will automatically set the height of the parent element when set to true.

This plugin will manipulate the following css properties on the element that will be fixed:

Note: Internet Explorer Behaviour When used on a viewport that is not the Window object, the fixed element will be animated (gradual slide down) in Internet Explorer in order to avoid the flashing/flickering affect.

Options

Events

The following events are triggered by this plugin:

Examples

Example 1:

The following example will apply stickOnScroll to a header element inside a scrolling area. Note the css properties set on the viewport element (id=info).

<div id="info" style="height: 200px; overflow: auto; position: relative;">
    <div id="info_body" style="height: 2000px;">
        <div style="height: 2em;">
            <div id="header" style="background: green;">Page Header</div>
        </div>
        Long area with content.
    </div>
</div>

Code:

$("div.header").stickOnScroll({
    viewport: $("div.info"),
    setParentOnStick: true
});

License

Release under the terms of the MIT License.

Change Log

Version 1.3, July 9, 2013

Version 1.2, Jun. 29, 2013

Version 1.1, 10/27/2012


Copyright 2012 Paul Tavares. All rights reserved.

Author

Paul Tavares @purtuga

Follow me at Twitter @paul_tavares