A JQuery plugin to make your elements stick to the top when scrolling a webpage. It takes the initial position of any element and gives the element a fixed position when scrolling past it, ensuring the element always stays in view.
Perfect for sidebars.
bower install jquery-sticktotop
$('aside').stickToTop(options);
Option | Description |
---|---|
scrollParent: | Element in which scrolling is monitored. default: DOM window |
offset: | Scroll offset to apply fixed positioning (sticking).
Basically the gap left at all times between the scrollParent and the target element. default: {top: 0, left: 0} |
bottomBound: | Scroll value relative to the bottom which to stop the element
from sticking (absolute positioning). Useful if you have a large footer and dont want your sidebar crashing into it. default: false (no bottom bound) |
minWindowHeight: | The minimum height of the parent/window in which stickToTop will be active. If less than the minimum height stickToTop will have no effect but will become active as soon as the parent/window height is greater than minWindowHeight. default: false (no min height) |
minWindowWidth: | The minimum width of the parent/window in which stickToTop will be active. If less than the minimum width stickToTop will have no effect but will become active as soon as the parent/window width is greater than minWindowWidth. Useful for disabling stickToTop for mobile and tablet viewports.
|
preserveLayout: | Preserves layout of sticky elements by adding a div which
occupies the original flow.
|
onStick: | Callback for when the element becomes sticky. default: null |
onDetach: | Callback for when the element becomes detached. Also fires when the bottomBound is reached. default: null |
Note: bottomBound now includes the height of the sticky element in the calculation to make bottomBound more intuitive (issue #1)
You can detach stickToTop from your element by using the unstickToTop() function
var $aside = $('aside');
// Attach
$aside.stickToTop();
...
// Detach
$aside.get(0).unstickToTop();
The "normal" use-case for this plugin demostrated applying the plugin to a side and menu bar.
Using this plugin to position many elements (in this case a canvas with a tetris block painted inside of it) and stick them to their appropriate places while scrolling to form a heart.
Visit this blog post for a full explanation