1.0.1 • Published 9 years ago
js-sticky v1.0.1
js-sticky
Light javascript utility to "sticky" elements to the top or bottom of the viewport.
Dependencies
- jQuery, post version 1.7
Installation
npm install js-sticky
or just include the js file in your project.
Then in the file:
<script src="js-sticky.js"></script>Usage
On your page, define these ids:
- a
#stickyor#sticky-bottomelement (former is stickied to the top of the viewport, latter is stickied to the bottom of the viewport) - a
#sticky-triggerelement (the point at which the sticky behavior should trigger--if it's just a position in the page, can just make an empty div like<div id="sticky-trigger"></div>) - if your
#stickyelement takes up space in the DOM (i.e. isposition: relativeorstaticand NOT floating), wrap#stickyin a#sticky-contdiv
For example:
page stuff blah blah
<div id="sticky-trigger"></div>
<div id="sticky-cont">
<div id="sticky">
sticky element here!
</div>
</div>Behavior: On scrolling past sticky-trigger, sticky div will be stuck. On scrolling back up past sticky-trigger, sticky will act as normal.
Nothing is needed to initialize the sticky behavior; the jQuery takes care of that. Default trigger is document ready, or you can overwrite with your own event in the script. The script is pretty thoroughly commented.
Examples
- an example of a sticky element anchored to the top of the page: here (on left)
- an example of a sticky element anchored by the bottom: here (scroll to the comments section) (on right)
Current limitations (future to-dos)
- must manually define a
sticky-contcontainer - can only have 1
stickyandsticky-bottomelement per page
License
Reporting issues
Please report issues through Github, or feel free to open a PR with a fix. Thanks!