Code Monkey home page Code Monkey logo

stickyfill's Introduction

#position: sticky polyfill

The most accurate sticky polyfill out in the wild.

Check out the demo and use cases test page.

###What it does

  • supports top-positioned stickies,

  • works in IE9+,

  • disables in older IEs and in browsers with native position: sticky support.

  • Mimics original position: sticky behavior:

    • uses parent node as a boundary box,
    • behaves nicely with horizontal page scrolling,
    • only works on elements with specified top,
    • mimics native top and margin-bottom behavior,
    • works with table cells disabled until Firefox makes a native implementation

###What it doesn't

  • doesn't support left, right, bottom or combined stickies,
  • doesn't work in overflowed blocks,
  • doesn't parse your CSS! Launch it manually.

###Installation

Download:

Include it on your page:

<script src="path/to/stickyfill.js"></script>

Also available in Bower:

bower install Stickyfill --save

###Usage

JS:

var stickyElements = document.getElementsByClassName('sticky');

for (var i = stickyElements.length - 1; i >= 0; i--) {
    Stickyfill.add(stickyElements[i]);
}

or JS + jQuery:

$('.sticky').Stickyfill();

CSS:

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

Also worth having a clearfix:

.sticky:before,
.sticky:after {
    content: '';
    display: table;
}

###Pro tips

  • top specifies sticky's position relatively to the top edge of the viewport. It accepts negative values, too.
  • Despite common misconception, sticky's bottom limit is defined by its parent node's bottom boundary. It has nothing to do with offsetParent (closest relatively positioned parent).
  • You can push sticky's bottom limit up or down by specifying margin-bottom.
  • Any non-default value (not visible) for overflow, overflow-x, or overflow-y on any of the predecessor elements anchors the sticky to the overflow context of that predecessor. Simply speaking, scrolling the predecessor will cause the sticky to stick, scrolling the window will not. This is expected with overflow: auto and overflow: scroll, but often causes problems and confusion with overflow: hidden. Keep this in mind, folks!

Check out the test page to understand stickies better.

###API

####Stickyfill.add(HTMLNode)

Add new sticky and init(), if necessary.

####Stickyfill.remove(HTMLNode)

Remove sticky.

####Stickyfill.rebuild()

Recalc all metrics and update stickies' positions.

Call it after layout changes. Launches automatically after window resizes and device orientations changes.

####Stickyfill.pause()

Remove event listeners.

Usefull for debugging. Use Stickyfill.init() to get stuff running again.

####Stickyfill.stop()

Disable stickies.

Use Stickyfill.init() to enable them again.

####Stickyfill.kill()

Disable stickies and erase Stickyfill.stickies.

####Stickyfill.init()

Attach event listeners and start watching for stickies in Stickyfill.stickies.

####Stickyfill.stickies

Array of parametric objects for all added stickies.

###Using Stickyfill?

Be sure to drop me a link → ✉️ [email protected]. Wanna see it in action.

###License

MIT license.

stickyfill's People

Contributors

artpolikarpov avatar dzucconi avatar fdiskas avatar gpbl avatar wilddeer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.