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

scrollpos-styler

v0.7.1

Published

JavaScript to add custom CSS class to a HTML element depending on scroll position

Downloads

1,719

Readme

ScrollPos-Styler

GitHub Issues GitHub Stars NPM Downloads License

Simple JavaScript to add a custom CSS class to an HTML element depending on the window's scroll position. One CSS class is added when scrolling below a certain position, and another one is added when scrolling above that position.

Background and Motivation

Bootstrap v3 has a JavaScript component named Affix. Affix can be used to modify CSS properties of an element when reaching a certain scroll position. The main use case for it is to change positioning of the element when scrolling past the element - i.e. switching from relative to fixed positioning so that the element remains visible when otherwise the user would scroll past it.

With v4 of Bootstrap, the Affix component is dropped in favor of position:sticky - which can be used to address the above use case. Many modern browsers have native support for it already, and polyfills are available for browsers which do not natively support position:sticky.

However, there are other scenarios in which Affix could be used to apply custom styles / classes to elements when reaching certain scroll positions. A common example is changing the text color and background of a fixed navigation bar upon scrolling. Initially (when the page is scrolled to the very top), the navigation bar seamlessly integrates into the page - i.e. it is flat, transparent and without any shadow. When scrolling downwards, the navigation bar 'stands out' so that it seems to be hovering above the page. The background (and text) color might change, it might drop shadow, etc.

This scenario is not addressed by position:sticky - and this is where the small ScrollPos-Styler script comes to the rescue.

Look at the demo to get a better understanding of the effect.

Differentiation and Limitations

This script is designed to modify attributes OTHER THAN an element's position. It doesn't work well when changing positioning of an element. Switching between position:relative and position:fixed is exactly what position:sticky is designed for, and this script in no viable alternative. Refer to the documentation for details, and use polyfills for older browsers.

Installation

Several options are available:

  • Download the latest release
  • Clone the repo: git clone https://github.com/acch/scrollpos-styler.git
  • Install with Bower: bower install scrollpos-styler
  • Install with npm: npm install scrollpos-styler

Usage

Simply import the scrollPosStyler.js script into your HTML page at the very end of the body element. Then, add the .sps class to the element(s) which you want to style. Define the two CSS classes .sps--abv and .sps--blw and you're all set!

The .sps--abv class will be added to your element when the window is scrolled above the defined position, and .sps--blw will be applied when it is scrolled below that position.

The default scroll position to trigger the style is 1px, meaning that as soon as the user starts scrolling the CSS class will be toggled. This can be changed by adding the data-sps-offset tag in HTML and specifying an offset or by modifying the scrollOffsetY variable in JavaScript.

You should add the .sps--abv class to the element in your HTML code already, to avoid any flickering when JavaScript is initially executed.

To style elements which were created after the page was initially loaded (i.e. using JavaScript), a public initialization function is available. Simply run ScrollPosStyler.init() to add the appropriate class based on the current scroll position. Demo2 shows you this in action.

The following options can be used in ScrollPosStyler.init():

Name | Type | Default | Description --- | --- | --- | --- scrollOffsetY | number | 1 | Default scroll position in px to trigger the style. spsClass | string | 'sps' | Classname used to determine which elements to style. classAbove | string | 'sps--abv' | Classname added to the elements when the window is scrolled above the defined position. Default is 'sps--abv'. classBelow | string | 'sps--blw' | Classname added to the elements when the window is scrolled below the defined position. Default is 'sps--blw'. offsetTag | string | 'data-sps-offset' | HTML tag used on the element to specify a scrollOffsetY other than the default.

Dependencies

None. The script does not require jQuery or other JavaScript libraries. While being designed for Bootstrap, it does not require it.

Browser support

The script uses Window.requestAnimationFrame(), as well as Element.classList. This means that it is supported by fairly modern browsers, only (IE 10+, Firefox 23+, Chrome 24+, Safari 6.1+). I don't plan on supporting older browsers, thus don't plan to implement any workarounds.

If you need to support older browsers then these links may provide valuable information:

  • http://www.html5rocks.com/en/tutorials/speed/animations/
  • http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript
  • https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Examples

The script can be used for more than just styling a static navbar. The following list provides additional usage examples for those seeking some inspiration:

Copyright and license

Copyright 2015 Achim Christ, released under the MIT license.