• Stars
    star
    679
  • Rank 66,532 (Top 2 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

StickyState is a high performant module making native position:sticky statefull and polyfilling the missing sticky browser feature

#StickyState

StickyState adds state to position:sticky elements and also polyfills the missing native sticky feature.

Dependency free, pure Javascript for IE9+.

Today's browsers do not all support the position:sticky feature (which by the way is being used (polyfilled) on pretty much every site you visit) - moreover the native supported feature itself comes without a readable state. Something like a:hover => div:sticky to add different styles to the element in its sticky state - or to read the state if needed in JavaScript.

Unlike almost all polyfills you can find in the wild, StickyState is highly performant. The calculations are reduced to a minimum by persisting several attributes.

In some cases you also need to know in which direction the user scrolls - for example if you want to hide a sticky header when the user scrolls up. if you set the scrollClass property of the options StickyState will add your choosen classNames to the element when it is sticky and scrolling.

As a standalone Library its 6.75kb gzipped.

Warning concerning Chromes implementation of native position:sticky

it looks like chromes implementaton of position:sticky is different to all other implementations out there. don't know if thats a bug - but bottom is currently not recognized by chrome. there will be a fix for this soon in sticky-state

Dependencies

none!

Browser support

IE >= 9, *


Install

npm install sticky-state

Demo

all you can eat

https://rawgit.com/soenkekluth/sticky-state/master/examples/index.html

headroom style

https://rawgit.com/soenkekluth/sticky-state/master/examples/headroom.html

simple

https://rawgit.com/soenkekluth/sticky-state/master/examples/simple.html


css

Your css should contain the following lines: (you can specify the classNames in js) https://github.com/soenkekluth/sticky-state/blob/master/dist/sticky-state.css

.sticky {
  position: -webkit-sticky;
  position: sticky;
}

.sticky.sticky-fixed.is-sticky {
  position: fixed;
  -webkit-backface-visibility: hidden;
  -moz-backface-visibility: hidden;
  backface-visibility: hidden;
}

.sticky.sticky-fixed.is-sticky:not([style*="margin-top"]) {
  margin-top: 0 !important;
}
.sticky.sticky-fixed.is-sticky:not([style*="margin-bottom"]) {
  margin-bottom: 0 !important;
}

.sticky.sticky-fixed.is-absolute{
  position: absolute;
}

js

var StickyState = require('sticky-state');
new StickyState(document.querySelectorAll('.sticky'));
//  all elements with class .sticky will have sticky state:

options

var StickyState = require('sticky-state');

// the props you can set (except scrollClass this shows the default options):



var stickyOptions = {
  disabled: false, // disable or enable the sticky feature initially
  className: 'sticky', // the core class which should be equal to the css. see above.
  stateClassName: 'is-sticky',  // the state class, when the element is actually sticky
  fixedClass: 'sticky-fixed',  // the fallback class that uses position:fixed to make the element sticky 
  wrapperClass: 'sticky-wrap', // the fallback (polyfilled) version needs a placeholder that uses the space of the actual sticky element when its position:fixed
  wrapFixedSticky: true,  // by default the sticky element gets wrapped by the placeholder. if you set it to false it will be inserted right before it.
  absoluteClass: 'is-absolute',  // the polyfilled sticky element needs to be position:absolut in some cases.
  
  // scrollclass will add a class to the sticky element that is depending on the scroll direction when the element is sticky.
  // when the scrolling stops the class will be the value of "none" unless you set "persist" to true.
  
  scrollClass: {
    down: null,
    up: null,
    none: null,
    persist: false
  }
};

// instantiate with options
var stickyElements = new StickyState(document.querySelectorAll('.sticky'), stickyOptions);

api / events

var StickyState = require('sticky-state');
new StickyState(document.querySelectorAll('.sticky'))
  .on('sticky:on', function(e){console.log('sticky:on', e.target);})
  .on('sticky:off', function(e){console.log('sticky:off' ,e.target);});

React Component

https://github.com/soenkekluth/react-sticky-state

More Repositories

1

react-sticky-state

ReactStickyState makes native position:sticky statefull and polyfills the missing sticky browser feature
JavaScript
96
star
2

hyperapp-example

hyperapp example
JavaScript
14
star
3

connect-ssi

SSI (Server Side Includes) Middleware for connect
JavaScript
14
star
4

fastscroll

Performant Custom Scroll Events
JavaScript
14
star
5

browsersync-ssi

SSI middleware for browser-sync
JavaScript
12
star
6

device.js

client side device / useragent detection for easier adaptive development / design
JavaScript
8
star
7

docker-backup

backup docker container with its volumes
JavaScript
7
star
8

nextframe

Promise for requestAnimationFrame plus nextFrames, sequence, delay, throttleFrames, waitFrames
JavaScript
6
star
9

seti-monokai-sublime

Seti Monokai Color Scheme for Sublime Text
5
star
10

react-state-promise

get a Promise from setState instead of using the idiot callback from facebook!
JavaScript
5
star
11

pmvc-react

PMVC (PureMVC) - React Example
JavaScript
4
star
12

scroll-events

performant custom scroll events and custom scroll propertys
JavaScript
4
star
13

ready-state

document.readyState Promise
JavaScript
4
star
14

scrollemitter

enhanced scroll events like scroll: start, progress, stop, min, max
JavaScript
4
star
15

react-css-animate

React CSSAnimate
JavaScript
3
star
16

lazytube

Lazy Loading Youtube Players
JavaScript
3
star
17

yarn-run

Run locally-installed node module executables with yarn.
JavaScript
3
star
18

browser-sync-connect

connect to browser sync without copying the script snippets to the html source
JavaScript
3
star
19

pmvc

pmvc (PureMVC) JavaScript MultiCore Framework
JavaScript
3
star
20

generator-wunder

a generator for yeoman
JavaScript
2
star
21

babel-base-setup

A starting point for new Projects using Babel
JavaScript
2
star
22

run-proxy

run-proxy calls npm scripts either with npm or yarn (if used)
JavaScript
2
star
23

EventDispatcher

JavaScript Event Dispatcher
JavaScript
2
star
24

object-state-store

observable stateful object store
JavaScript
2
star
25

react-puremvc

JavaScript
2
star
26

delegatejs

performant object scope function [event] delegation like function.bind or jquery.proxy
JavaScript
2
star
27

parallux

Parallux - high performant parallax engine
HTML
2
star
28

bablyfill

instantly run or transpile your es6+ code without needing to install anything else but bablyfill
JavaScript
2
star
29

scrollfeatures

performant custom scroll events and custom scroll propertys
JavaScript
1
star
30

gulpfile

my Gulpfile
JavaScript
1
star
31

ypx

npx for yarn / alias for yarn run
Shell
1
star
32

lazyview

check if a dom component is inside the viewport and call a function and or add a class
HTML
1
star
33

fs-onchange

Use glob patterns to watch files and folders to run a command when anything is added, changed or deleted.
JavaScript
1
star
34

node-delegate

performant object scope function [event] delegation like function.bind or jquery.proxy
JavaScript
1
star
35

key-value-store-state

observable stateful key value object store
JavaScript
1
star
36

eslint-config-kluth

base eslint config using prettier
JavaScript
1
star
37

object-store-state

observable stateful object store
JavaScript
1
star
38

ceaser-easing

sass variables for CSS3 easing equations
CSS
1
star
39

browser-sync-ejs

ejs middleware for connect / browsersync
JavaScript
1
star
40

easy-glob

simple and easy to use glob with instant results using fast-glob as backend and is faster than node-glob.
JavaScript
1
star
41

eslint-configs

ESLint config using Prettier and sort-class-members for code formatting and unification. jest for testing.
JavaScript
1
star
42

run-script-proxy

execute scripts inside package.json with npm or yarn with a simple run command
JavaScript
1
star
43

minibundle

simple effective and fast build modules for esnext es5 and browser
JavaScript
1
star
44

prs

alias for `new Promise`
HTML
1
star
45

animal

extreme small animation library
JavaScript
1
star
46

microdispatch

minimal and performant event dispatcher / emitter
JavaScript
1
star
47

configs

config files for projects
HTML
1
star
48

js-module-template

my default template for fast coding a js module
JavaScript
1
star
49

my-global-modules

my global node modules
1
star
50

babel-preset-es2015-tree-shaking

Babel preset for all es2015 plugins except es2015-modules-commonjs to enable tree-shaking in webpack
JavaScript
1
star