npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sticky-states

v1.0.8

Published

Makes elements sticky to the viewport (position fixed) based on the scroll position.

Downloads

867

Readme

Sticky States

npm version DragsterJS gzip size

Makes elements sticky to the viewport (position fixed) based on the scroll position.

Installation

Setting up is pretty straight-forward. Download the js and css files from dist folder and include them in your HTML:

<link rel='stylesheet' id='sticky-states'  href='dist/sticky-states.min.css' type='text/css' media='all' />
<script type="text/javascript" src="path/to/dist/sticky-states.min.js"></script>

NPM

Sticky States is also available on NPM:

$ npm install sticky-states

Initialization

Once the Sticky States script is loaded all functions will be available through the global variable window.StickyStates, however to enable the components you need to call the function init:

Call the function StickyStates.init( options ); passing the options parameter as an object.

Options Available

The options parameter accept any of the available options from the default settings by passing the new values as an object. You can simply ommit the options you don't want to change the default values of.

These are the currently accepted options with their default values, if in doubt check the source code:

	var _defaults = {
		elementSelector: '[data-sticky-states]',
		innerElementSelector: '[data-sticky-states-inner]',

		isEndPositionClass: 'is-end-position',
		isStickyClass: 'is-sticky',
		isStickyTopClass: 'is-sticky--top',
		isStickyBottomClass: 'is-sticky--bottom',
		isActivatedClass: 'is-activated',

		positionAttribute: 'data-sticky-position',
		thresholdAttribute: 'data-sticky-threshold',
		stickyRelativeToAttribute: 'data-sticky-relative-to',
		staticAtEndAttribute: 'data-sticky-static-at-end',
		containerAttribute: 'data-sticky-container',

		position: 'top', // Accepted values: `top`, `bottom`
		threshold: 0,
	};

For example, if your application already has the markup defined in many places and you want to change the selector used for the sticky element and its content element, initialize the component with the options below:

var options = {
	elementSelector: '[data-sticky-states], .also-has-sticky-states',
	innerElementSelector: '[data-sticky-states-inner], .also-sticky-content',
}
StickyStates.init( options );

Everything else will use the default values.

Basic Usage

1. Recommended HTML elements structure

The sticky-states component requires the following HTML elements structure:

<div data-sticky-states>
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

IMPORTANT: note that the main sticky element, which is marked with the data attribute data-sticky-states, is used to keep the space and position of the sticky inner element while it is sticky, avoiding layout shifts. The element that actually gets sticky is the inner element, marked with the data attribute data-sticky-states-inner.

You can define the boundaries of the sticky element using the data attribute data-sticky-container, the default value is the parent element of the main sticky element. The boundaries of the container element are used define when the sticky element will stop being sticky or when it should move back to its normal state and position.

The attribute data-sticky-container accepts any valid CSS selector, and will use the first element that matches the selector.

While looking for the container element, the script will try to find a match higher in the DOM hiearchy of the sticky element itself (using the query function stickyElement.closest()). If a match is not found, the script will try to find a matching element in the entire document (using the query function window.querySelector()).

For example, to set a sticky element to stay sticky while the content of a section is visible in the viewport you could use the following markup:

<div data-sticky-states data-sticky-container="#section-id">
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

<section id="section-id">
	<!-- SECTION CONTENT -->
</section>	

Note that in this example the sticky element is not inside hierarchy of the container element <section> used to define the boundaries.

2. Setting the position of the sticky element (top or bottom)

Use the data attribute data-sticky-position at the main sticky element to set the position of the sticky element, if not specified at the element it will default to the top position, or the default position defined at the time of initialization of the StickyStates components.

Sticky at the top (default):

<div data-sticky-states>
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

or

<div data-sticky-states data-sticky-position="top">
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

Sticky at the bottom;

<div data-sticky-states data-sticky-position="bottom">
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

3. Setting the sticky element to stay at its original position, works only for sticky elements positioned at the bottom

<div data-sticky-states data-sticky-position="bottom" data-sticky-threshold="0" data-sticky-static-at-end>
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

4. Setting the sticky element threshold position relative to another sticky element

Single CSS selector:

<div data-sticky-states data-sticky-states data-sticky-relative-to="#relative-sticky">
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

Multiple CSS selector for different viewport width breakpoints:

<div data-sticky-states data-sticky-states data-sticky-relative-to='{ "xs": { "breakpointInitial": 0, "breakpointFinal": 1023, "selector": ".site-header" }, "sm": { "breakpointInitial": 1024, "breakpointFinal": 100000, "selector": ".col-full-nav" } }'>
	<div data-sticky-states-inner>
		<!-- CONTENT -->
	</div>
</div>

Contributing to Development

This isn't a large project by any means, but you are definitely welcome to contribute.

Development environment

Clone the repo and run npm install:

$ cd path/to/sticky-states
$ npm install

Run the build command:

$ gulp build

Build on file save:

$ gulp
$ gulp watch

License

Licensed under MIT.