Zebra Pin
A lightweight jQuery plugin for pinning elements to the page or to container elements
Zebra_Pin is a lightweight (2.5KB minified, ~800 bytes gzipped) and adaptive (things work as expected when the browser window is resized) jQuery plugin for pinning elements to the page or to a container element, so that pinned elements remain visible when they are about to be scrolled out of view. This type of elements are also referred to as fixed position elements or sticky elements.
Use it to create sticky sidebars, sticky navigation, sticky headers and footers, or anything else you feel the need to make it stick to the page while the user scrolls.
You can have "hard" pinned elements - elements are pinned to their initial position and stay there, elements that become pinned when they are about to be scrolled out of view, as well as pinned elements that can move only inside their parent element's boundaries.
Pinned elements are added a user-defined CSS class so you can adjust their looks when pinned. Additionally, custom events are fired when elements become pinned/unpinned giving you even more power for customizing the result.
Works in pretty much any browser - Firefox, Chrome, Safari, Edge, Opera and Internet Explorer 7+
Features
- elements can be pinned inside a container element, not just to the page
- pinned elements are added a user-defined CSS class so you can adjust their looks when pinned
- custom events are fired when elements become pinned/unpinned giving you even more power for customizing the result
- it is really small – it weights 2.5KB minified (~800 bytes gzipped) offering a very good ratio of features per used bytes
- it's cross-browser – works in every major browser and IE7+
Demo
See the demos
Requirements
Zebra Pin has no dependencies other than jQuery 1.7+
Installation
Zebra Pin is available as a npm package. To install it use:
npm install zebra_pin --save
Zebra Pin is also available as a Bower package. To install it use:
bower install zebra_pin --save
How to use
First, load the latest version of jQuery from a CDN and provide a fallback to a local source, like:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>window.jQuery || document.write('<script src="path/to/jquery-3.3.1.js"><\/script>')</script>
Load the Zebra Pin jQuery plugin:
<script src="path/to/zebra_pin.min.js"></script>
Alternatively, you can load Zebra Pin from JSDelivr CDN like this:
<script
src="https://cdn.jsdelivr.net/npm/zebra_pin@latest/dist/zebra_pin.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/zebra_pin@2.0.0/dist/zebra_pin.min.js"></script>
Now, within the DOM-ready event, pin elements to page or to a container:
$(document).ready(function() {
new $.Zebra_Pin($('#element'));
new $.Zebra_Pin($('#element'), {
top_spacing: 10
});
new $.Zebra_Pin($('#element'), {
contained: true
});
new $.Zebra_Pin($('#element'), {
hard: true
});
});
Configuration options
Properties
Property | Type | Default | Description |
---|
class_name | string | "Zebra_Pin" | CSS class to be added to the element when it becomes pinned |
contain | boolean | false |
Specifies whether the pinned element should be restricted to its parent element's boundaries or not.
The container element must have its position set to something other than the default static
|
hard | boolean | false |
Specifies whether the element should be "hard" pinned (the element is pinned to its position from the
beginning), or become pinned only when it is about to go out of view.
|
top_spacing | integer | 0 |
Distance, in pixels, from the browser window's top (or the container element's top, when the element is contained to its parent element's boundaries) from which the element should become pinned.
This only works if the hard property is set to false .
|
bottom_spacing | integer | 0 |
Distance, in pixels, from the containing parent element's bottom which the pinned element must not exceed.
This only works if the hard property is set to false and the contain property is set to true
|
z_index | integer | 1000 |
The value of zIndex CSS property to be set for pinned elements
|
Events
Event | Description |
---|
onPin |
Callback function to be executed when an element becomes pinned
The callback function receives 3 arguments:
- the vertical position, relative to the document, where the event occurred
- a reference to the pinned element
- the index of the element - if the plugin was attached to multiple elements (0 based)
|
onUnpin |
Callback function to be executed when an element becomes unpinned (reverts to its original state)
The callback function receives 3 arguments:
- the vertical position, relative to the document, where the event occurred
- a reference to the unpinned element
- the index of the element - if the plugin was attached to multiple elements (0 based)
|
Methods
update()
Updates the pinned elements' positions in accordance with the scrolled amount and with the pinned elements' container elements (if any).
Useful if a pinned element's parent changes height.
var zp = new $.Zebra_Pin($('#element'), {
contain: true
});
zp.update();
Support the development of this project
Cross browser/device testing is done with