sticky-js

repository·master·Indexed 20 days ago

https://github.com/rgalus/sticky-js

A lightweight, vanilla JavaScript library (v1.3.0) for creating responsive sticky elements. It allows elements to stick to the viewport or a specific parent container using the Sticky class, supporting configuration via global options or data attributes such as data-margin-top and data-sticky-for. Includes methods for updating positions and destroying instances to prevent memory leaks.

Tokens
1.9K
Snippets
8
Records
11
Agent score
23%

What's inside sticky-js

  1. Initialize sticky-js

    master

    To use sticky-js, include the script in your HTML and initialize a new Sticky instance by passing a CSS selector for the element you want to make sticky. You can also pass a global options object.

    <!-- 1. Include the script -->
    <script src="sticky.min.js"></script>
    
    <!-- 2. The element to make sticky -->
    <div class="selector">Sticky element</div>
    
    <script>
      // 3. Initialize
      var sticky = new Sticky('.selector');
    </script>
  2. How Sticky.js handles element wrapping

    master

    When the wrap option is set to true (or the data-sticky-wrap attribute is present), sticky-js wraps the target element in a placeholder element. This is used to maintain the layout and prevent the page from jumping when the element switches to position: fixed.

    If wrapping is enabled, the library applies display: block and sets the width and height of the parent (the wrapper) to match the original dimensions of the sticky element.

  3. Configure sticky-js options

    master

    You can configure sticky behavior using either a global options object in the constructor or via data-* attributes on the HTML elements.

    OptionTypeDefaultDescription
    data-sticky-wrapbooleanfalseWhen true, the sticky element is wrapped in a <span> with the element's dimensions to prevent content jumping.
    data-margin-topnumber0The margin between the top of the viewport and the sticky element when scrolled.
    data-sticky-fornumber0A breakpoint (viewport width). Sticky is activated when the viewport is larger than this value and destroyed when smaller.
    data-sticky-classstringnullThe CSS class added to the element when it becomes stuck.
  4. Use sticky-js with multiple elements and containers

    master

    You can manage multiple sticky elements by using the data-sticky-container attribute on a parent element. This allows elements to be sticky relative to that specific container rather than the entire page. You can also configure individual element behavior using data-* attributes.

    <div class="row" data-sticky-container>
      <div class="medium-2 columns">
        <img src="http://placehold.it/250x250" class="sticky" data-margin-top="20" data-sticky-for="1023" data-sticky-class="is-sticky">
      </div>
      <div class="medium-8 columns">
        <h1>Sticky-js</h1>
        <p>Lorem ipsum.....</p>
      </div>
      <div class="medium-2 columns">
        <img src="http://placehold.it/250x250" class="sticky" data-margin-top="20" data-sticky-for="1023" data-sticky-class="is-sticky">
      </div>
    </div>
    
    <script src="sticky.min.js"></script>
    <script>
      var sticky = new Sticky('.sticky');
    </script>
  5. Sticky instance methods: update() and destroy()

    master

    The Sticky instance provides methods to manage the lifecycle and state of the sticky element:

    • update(): Recalculates the sticky position. Call this if the parent container (marked with data-sticky-container) changes its height.
    • destroy(): Removes the sticky behavior from the element.
    var sticky = new Sticky('.sticky');
    
    // Call update when parent container height changes
    sticky.update();
    
    // Remove sticky behavior
    sticky.destroy();
  6. Configure Sticky.js global options

    master

    When instantiating the Sticky class, you can pass an options object to define global behavior. These options can be overridden on a per-element basis using data-* attributes.

    const options = {
      wrap: false,             // Whether to wrap the element in a placeholder
      wrapWith: '<span class="wrapper"></span>', // The HTML to use for wrapping
      marginTop: 0,            // Default top margin when sticky
      marginBottom: 0,         // Default bottom margin for boundary detection
      stickyFor: 0,            // Minimum viewport width required for stickiness
      stickyClass: null,       // CSS class added to the element when active
      stickyContainer: 'body'  // Selector for the container the element is stuck within
    };
    
    new Sticky('.selector', options);
  7. Browser compatibility for sticky-js

    master

    The library uses ECMAScript 5 features. Supported browsers include:

    • IE 9+
    • Chrome 23+
    • Firefox 21+
    • Safari 6+
    • Opera 15+

    If you need to support older browsers, you must provide an ECMAScript 5 polyfill.

  8. Initialize Sticky.js with the Sticky class

    master

    To use sticky-js, instantiate the Sticky class by providing a CSS selector for the elements you want to make sticky and an optional configuration object. The library will automatically find these elements once the page is fully loaded and manage their sticky behavior based on scroll and resize events.

    // Basic usage
    const sticky = new Sticky('.my-sticky-element');
    
    // Usage with configuration
    const sticky = new Sticky('.my-sticky-element', {
      wrap: true,
      marginTop: 20,
      stickyClass: 'is-sticky'
    });
  9. Manage Sticky instances with destroy()

    master

    To prevent memory leaks and remove all event listeners (scroll, resize, and load) associated with the sticky elements, call the .destroy() method on your Sticky instance.

    const sticky = new Sticky('.my-element');
    
    // Later, when the elements are no longer needed or the component unmounts
    sticky.destroy();
  10. Override Sticky.js options with data attributes

    master

    You can customize the behavior of individual elements directly in your HTML using data-* attributes. These values take precedence over the global options passed to the Sticky constructor.

    <!-- Individual element configuration -->
    <div 
      class="my-element" 
      data-margin-top="10" 
      data-margin-bottom="20" 
      data-sticky-for="768" 
      data-sticky-class="active-sticky" 
      data-sticky-wrap
      data-sticky-wrap-with="<div class="wrapper"></div>"
    >
      Sticky Content
    </div>