« Back to GitHub Repo

Clingify

A flexible, mobile-friendly jQuery plugin for sticky page elements

Purpose

Clingify is a jQuery plugin that allows you to easily create “sticky” or “fixed” headers, navs, and other page elements. Once you scroll past the targeted element, the plugin will toggle a CSS class that gives the element a fixed position, pinning it in place on the page.

Features

There are many other sticky plugins out there, but here are a few reasons to consider Clingify:

  • No inline CSS, which means no overwriting styles with !important.
  • Everything is controlled via CSS classes. I have included some sensible default CSS, but you are free to customize the classes to work with your preferred object-oriented CSS framework.
  • Support for multiple sticky elements on the same page.
  • The plugin wraps the target element with a placeholder div set to the same height as the target. This keeps your content from shifting when the element becomes fixed and is removed from the page flow.
  • Built with responsive design in mind. The breakpoint option makes it easy to disable Clingify behavior for smart phones and tablets.
  • Callback functions for when the element is attached or detached, and when the page is resized.
  • The scroll and resize event listeners are throttled to improve performance. The amount of throttle is a configurable option.

Installation

Just initialize the script somewhere after you’ve included jQuery (1.7+) and the plugin. Don't forget the CSS file!

<html>
<head>
<link rel="stylesheet" href="/css/clingfy.css" />
</head>
<body>
...
page content
...
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.clingify.js"></script>
<script type="text/javascript">
$(function() {
    $('.some-selector').clingify();
});
</script>
</body>
</html>

Here’s the code for the Clingify effects being used for this demo.

$(function() {
    var $navBar = $('nav'),
        $navParent = $('.primary-content'),
        $sidebar = $('.some-sidebar'),
        $sidebarParent = $('.secondary-content'),   
        matchWidths = function($elem, $elemParent) {
            $elem.width($elemParent.width());
        },
        matchOffset = function($elem, $elemParent) {
            $elem.css({'padding-left' : $elemParent.offset().left});
        },
        resetOffset = function($elem) {
            $elem.css({'padding-left' : 0});
        };

    $navBar.clingify({
        extraClass : 'navClingy',
        locked : function() {
            matchOffset($navBar, $navParent);
        },
        detached : function() {
            resetOffset($navBar);
        },
        resized : function() {
            matchOffset($navBar, $navParent);
        }
    });
    $sidebar.clingify({
        breakpoint : 768,
        extraClass : 'sidebarClingy',
        locked : function() {
            matchWidths($sidebar, $sidebarParent);
        },
        resized : function() {
            matchWidths($sidebar, $sidebarParent);
        }
    });
});

A version of the above example is being used on this page to keep the width of the clingy nav aligned with the main content. (Try resizing the browser window.)

Options

Property Value Types Default Notes
Breakpoints
breakpointHeight integer 0 At screen sizes shorter than this number (of pixels), disable Clingify.
breakpointWidth integer 0 At screen sizes narrower than this number (of pixels), disable Clingify.
Performance
throttle integer 50 How often, in milliseconds, to fire scroll and resize dependent functions.
CSS Selectors
extraClass string null Adds an additional class to both the wrapper and placeholder divs.
lockedClass string js-clingify-locked The class added to the wrapper when Clingify locks an element.
placeholderClass string js-clingify-placeholder The class of the div that prevents page reflow.
(Its height is set to match the height of the target element.)
overrideClass string js-clingify-permalock If this class is present on the wrapper div, the element cannot be detached.
wrapperClass string js-clingify-wrapper The div that wraps the target element.
Callbacks
detached function null Callback that fires just before an element is detached.
locked function null Callback that fires just before an element is locked.
resized function null Callback that fires just after window is resized.
(This runs whether or not the element is locked.)
Advanced modes (new!)
scrollingElem string window Where to attach the event listener for the scroll event
(Useful for scrolling offcanvas page elements.)
fixed boolean true Switches between "fixed positioning" (classic) mode and transform mode.
Fixed positioning mode relies on position: fixed in the CSS.
Transform mode repositions the target element using TranslateY.
(For advanced use cases. Experimental.)

Some filler text to force a long, scrolling page

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui in turpis pulvinar facilisis. Ut felis.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc.

Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh.

Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices.

Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper.

Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui in turpis pulvinar facilisis. Ut felis.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc.

Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh.

Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices.

Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper.

Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci.

Sidebar content

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc.