- Overview
- Documents
Sticky kit provides an easy way to attach elements to the page when the user scrolls such that the element is always visible.
Browser Support
Sticky Kit works with all modern browsers, and IE7+.
Note: only floated columns work in IE7.
Source: leafo.net
1. INCLUDE JS FILES
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script src="$root/src/jquery.sticky-kit.js"></script>
2. HTML
<h1>My Site</h1> <div class="content" data-sticky_parent> <div class="sidebar" data-sticky_column> This is a sticky column </div> <div class="main" data-sticky_column> This is the main column <p class="sub"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus id leo et aliquam. Proin consectetur ligula vel neque cursus laoreet. Nullam dignissim, augue at consectetur pellentesque, metus ipsum interdum sapien, quis ornare quam enim vel ipsum. </p> <p class="sub"> In congue nunc vitae magna tempor ultrices. Cras ultricies posuere elit. Nullam ultrices purus ante, at mattis leo placerat ac. Nunc faucibus ligula nec lorem sodales venenatis. Curabitur nec est condimentum, blandit tellus nec, semper arcu. Nullam in porta ipsum, non consectetur mi. Sed pharetra sapien nisl. Aliquam ac lectus sed elit vehicula scelerisque ut vel sem. Ut ut semper nisl. </span> <p class="sub"> Curabitur rhoncus, arcu at placerat volutpat, felis elit sollicitudin ante, sed tempus justo nibh sed massa. Integer vestibulum non ante ornare eleifend. In vel mollis dolor. </p> </div> </div> <div class="footer"> My very tall footer! </div>
3. JAVASCRIPT
$("#sidebar").stick_in_parent();
4. OPTIONS
$("#sticky_item").stick_in_parent(options);
options is an optional hash of options to configure how Sticky Kit works. The following options are accepted:
- parent — The element will be the parent of the sticky item. The dimensions of the parent control when the sticky element bottoms out. Defaults to the closest parent of the sticky element. Can be a selector.
- inner_scrolling — Boolean to enable or disable the ability of the sticky element to scroll independently of the scrollbar when it’s taller than the viewport. Defaults to true for enabled.
- sticky_class — The name of the CSS class to apply to elements when they have become stuck. Defaults to "is_stuck".
- offset_top — offsets the initial sticking position by of number of pixels, can be either negative or positive
5. EVENTS
Various events are triggered from a sticky element when its state changes. They are:
- sticky_kit:stick — Triggered when element becomes stuck.
- sticky_kit:unstick — Triggered when element becomes unstuck. (Note: an element is still considered stuck when it has bottomed out)
- sticky_kit:bottom — Triggered when element bottoms out.
- sticky_kit:unbottom — Triggered when element is no longer bottomed out.
For example, if we want to log when an element sticks and unsticks we might do:
$("#sticky_item").stick_in_parent() .on("sticky_kit:stick", function(e) { console.log("has stuck!", e.target); }) .on("sticky_kit:unstick", function(e) { console.log("has unstuck!", e.target); });
Sticky Kit listens to one event on document.body.
- sticky_kit:recalc — trigger this event to cause all sticky elements to be recalculated. More information below.
If you want to remove sticky kit from an element after applying it you can send that element a sticky_kit:detach event.
Sticky Kit also listens to an event on the sticky elements:
- sticky_kit:detach — remove sticky kit and restore element to original position
For example:
$("#sticky_item").trigger("sticky_kit:detach");