Create Container-Relative Sticky Elements with jQuery

File Size: 8.07 KB
Views Total: 83
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Create Container-Relative Sticky Elements with jQuery

Relative Sticky Header is a tiny jQuery plugin that provides a simple solution for making any elements sticky within their parent container.

The plugin fixes key limitations of CSS position:sticky and traditional jQuery/JavaScript fixed-position solutions. It keeps sticky elements within their parent containers, preserves horizontal scrolling, and maintains layout integrity. The relative positioning approach prevents content jumps and layout breaks common with fixed positioning.

How It Works:

  1. The plugin selects the parent container and the sticky element based on the provided selector.
  2. If the sticky element is not found, it logs a warning and stops further execution.
  3. The plugin adds a CSS class to the sticky element for styling.
  4. On window scroll, the plugin calculates the scroll position and adjusts the sticky element's position accordingly.
  5. If the scroll position surpasses the original position of the sticky element, it becomes "sticky." The top property is updated dynamically based on the scrolling distance while ensuring it does not exceed the parent's height.
  6. When the scroll position returns to the original state, the element reverts to its default position.

How To Use It:

1. Download the plugin from jQueryScript or install it via npm: 

# NPM
$ npm install jquery-sticky-header-relative

2. Load the Relative Sticky Header jQuery plugin after you've included the latest jQuery library:

<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/dist/jquery.stickyheadersrelative.min.js"></script>

3. Call the stickyHeadersRelative method on the parent container and pass the selector for the element you want to make sticky while scrolling:

<div class="container">
  <h1>Sticky Heading</h1>
</div>
$(document).ready(function () {
  $('.container').stickyHeadersRelative('h1');
});

4. Customize plugin options by passing an options object as the second parameter:

  • stickyClass: This option allows you to specify the CSS class that will be added to the element when it becomes sticky. This class can be used to apply specific styles to the sticky element. For instance, you might use it to add a shadow, change the background color, or adjust padding. 
  • backgroundColor: This option sets the background color of the element when it's sticky. This is useful for visually distinguishing the sticky element from the rest of the content. The default value is 'white'. You can use any valid CSS color value here, including named colors (like 'red', 'blue'), hexadecimal values (like '#FF0000'), RGB values (like 'rgb(255, 0, 0)'), or HSL values.
  • zIndex: This option controls the stacking order of the sticky element. A higher zIndex value ensures the sticky element appears above other elements on the page. This is crucial for preventing the sticky element from being obscured by other content. The default value is 1000. You should choose a value that's high enough to ensure the desired stacking order.
  • additionalStyles: This option provides a way to add any other CSS styles to the sticky element. This is an object where the keys are CSS properties and the values are the corresponding CSS values. 
$('.container').stickyHeadersRelative('h1', {
  stickyClass: 'sticky-element',
  backgroundColor: 'white',
  zIndex: 1000,
  additionalStyles: {}
});

See Also:


This awesome jQuery plugin is developed by ssv445. For more Advanced Usages, please check the demo page or visit the official website.