
This is a pure JavaScript version of the hc-sticky jQuery plugin which makes any element sticky on the top of the web page using pure JavaScript.
How to use it:
Install & Import.
# NPM $ npm install hc-sticky --save
import hcSticky from 'hc-sticky';
Or include the compiled JavaScript file ‘hc-sticky.js’ on the webpage.
<script src="hc-sticky.js"></script>
Initialize the library and specify the element to be stuck.
var Sticky = new hcSticky('#sidebar', {
// options here
});
All possible plugin options.
var Sticky = new hcSticky('#sidebar', {
// The distance from the top
top: 0,
// The distance from the bottom
bottom: 0,
// The distance from the bottom of the referring element
bottomEnd: 0,
// The distance from the top inside of the sticky content
innerTop: 0,
// Element at which to trigger HC-Sticky instead of top of sticky container.
// This has higher priority than innerTop option.
innerSticker: null,
// Classname of sticky element
stickyClass 'sticky',
// Classname of wrapper
wrapperClassName: 'wrapper-sticky',
// Element that represents the reference for height instead of height of the container. recalculate
stickTo: null,
// Object containing responsive breakpoints
responsive: null,
// When set to false, sticky content will not move with the page if it is bigger that Window.
followScroll: true,
// Direction of the responsive queries.
mobileFirst: true,
// Limit the rate at which the HC Sticky can fire on window resize.
resizeDebounce: 100,
// Disable the plugin
disable: false
// Callbacks
onStart: null,
onStop: null,
onBeforeResize: null,
onResize: null
});
API methods.
var Sticky = new hcSticky('#sidebar');
// updates options
Sticky.update({
// options here
});
// re-init the plugin
Sticky.reinit();
// Detaches the plugin from element
Sticky.detach();
// Attaches the plugin back to the element.
Sticky.attach();
// Destroys the plugin
Sticky.destroy();
Changelog:
v2.3.0 (03/12/2026)
- Update
v2.2.6 (08/20/2020)
- Update
v2.2.5 (08/19/2020)
- Update
v2.2.4 (08/18/2020)
- Update
v2.2.3 (09/22/2018)
- Update
The post Cross-browser Sticky Element Plugin With Pure JavaScript – HC-Sticky appeared first on CSS Script.
Discover more from RSS Feeds Cloud
Subscribe to get the latest posts sent to your email.
