Convert Figma logo to code with AI

garand logosticky

jQuery Plugin for Sticky Objects

3,304
1,061
3,304
173

Top Related Projects

Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't

Polyfill for CSS `position: sticky`

A jQuery plugin for creating smart sticky elements

DEPRECATED: A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.

This plugin is used to fix elements on the page (top, bottom, anywhere); however, it still allows the element to continue to move left or right with the horizontal scroll.

Quick Overview

Sticky is a lightweight jQuery plugin that allows elements to stick to the top of the browser window as the user scrolls down the page. It provides a simple way to create sticky headers, navigation bars, or any other elements that need to remain visible while scrolling.

Pros

  • Easy to implement with minimal code
  • Customizable options for offset and wrapper
  • Lightweight and performant
  • Compatible with responsive designs

Cons

  • Requires jQuery as a dependency
  • Limited functionality compared to more comprehensive sticky plugins
  • May not work well with complex layouts or dynamic content
  • Lacks advanced features like scroll-based animations

Code Examples

Basic usage:

$("#sticker").sticky({topSpacing:0});

This code makes the element with the ID "sticker" stick to the top of the page when scrolling.

Using a custom wrapper:

$("#sticker").sticky({
  topSpacing: 0,
  zIndex: 100,
  stopper: "#footer"
});

This example adds a z-index and stops the sticky behavior when the element reaches the footer.

Updating sticky elements:

$("#sticker").sticky('update');

This code updates the sticky element, useful when the page content changes dynamically.

Getting Started

  1. Include jQuery and the sticky plugin in your HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="jquery.sticky.js"></script>
  1. Initialize the plugin on your desired element:
$(document).ready(function(){
  $("#header").sticky({topSpacing:0});
});
  1. Add some basic CSS to prevent content jumps:
.sticky-wrapper { height: auto !important; }

That's it! Your element should now stick to the top of the page when scrolling.

Competitor Comparisons

Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't

Pros of floatThead

  • More feature-rich, offering advanced options for table header floating
  • Better performance for large tables with many columns
  • Actively maintained with regular updates and bug fixes

Cons of floatThead

  • More complex setup and configuration compared to sticky
  • Larger file size, which may impact page load times
  • Steeper learning curve for basic implementations

Code Comparison

sticky:

$(".sticky").sticky({
  topSpacing: 0,
  zIndex: 100
});

floatThead:

$('table').floatThead({
  position: 'fixed',
  top: 50,
  scrollContainer: function($table){
    return $table.closest('.wrapper');
  }
});

Summary

floatThead is a more powerful and flexible solution for floating table headers, offering advanced features and better performance for complex tables. However, it comes with a steeper learning curve and more complex setup compared to sticky. sticky provides a simpler, more straightforward approach for basic sticky elements, not limited to table headers. The choice between the two depends on the specific requirements of the project, with floatThead being more suitable for complex table-specific needs and sticky for general-purpose sticky elements.

Polyfill for CSS `position: sticky`

Pros of Stickyfill

  • More comprehensive polyfill for CSS position: sticky
  • Supports both top and bottom sticky positioning
  • Actively maintained with recent updates

Cons of Stickyfill

  • Larger file size and potentially more complex implementation
  • May have a slight performance impact due to its comprehensive nature

Code Comparison

Sticky:

$(document).ready(function(){
  $("#stickymenu").sticky({topSpacing:0});
});

Stickyfill:

var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

Key Differences

  • Sticky is a jQuery plugin, while Stickyfill is a vanilla JavaScript solution
  • Sticky focuses on simple top-sticky functionality, while Stickyfill aims to fully replicate CSS position: sticky behavior
  • Stickyfill offers more flexibility and browser support, but may be overkill for simple use cases

Use Cases

  • Choose Sticky for straightforward top-sticky elements in jQuery projects
  • Opt for Stickyfill when needing comprehensive sticky positioning support or working without jQuery

Community and Support

  • Sticky has more GitHub stars but hasn't been updated recently
  • Stickyfill has fewer stars but is actively maintained and has more recent contributions

A jQuery plugin for creating smart sticky elements

Pros of sticky-kit

  • More feature-rich, offering additional options like bottom boundary and recalculation on window resize
  • Supports multiple sticky elements on a single page
  • Provides callbacks for various events (stick, unstick, bottom, unbottom)

Cons of sticky-kit

  • Larger file size due to additional features
  • May have a slightly steeper learning curve for basic use cases
  • Requires jQuery as a dependency

Code Comparison

sticky:

$("#sticker").sticky({topSpacing:0});

sticky-kit:

$("#sticker").stick_in_parent({
  offset_top: 0,
  bottoming: true,
  recalc_every: 50
});

Key Differences

  • sticky is more lightweight and straightforward for simple use cases
  • sticky-kit offers more customization options and advanced features
  • sticky-kit requires jQuery, while sticky can be used as a standalone script

Use Case Recommendations

  • Choose sticky for simple, lightweight sticky element implementations
  • Opt for sticky-kit when you need advanced features or multiple sticky elements on a page

Community and Maintenance

  • Both projects have been relatively inactive in recent years
  • sticky-kit has a larger user base and more open issues/pull requests
  • Consider exploring more actively maintained alternatives for long-term projects

DEPRECATED: A position: sticky polyfill that works with filamentgroup/fixed-fixed for a safer position:fixed fallback.

Pros of fixed-sticky

  • More robust and feature-rich, supporting both top and bottom positioning
  • Better cross-browser compatibility, including older versions of Internet Explorer
  • Actively maintained with recent updates and bug fixes

Cons of fixed-sticky

  • Larger file size and potentially more complex implementation
  • Requires additional CSS for proper styling and positioning
  • May have a steeper learning curve for beginners

Code Comparison

sticky:

$(".sticky").sticky({
  topSpacing: 0,
  zIndex: 100
});

fixed-sticky:

<div class="fixedsticky">
  <div class="fixedsticky-top">Top content</div>
  <div class="fixedsticky-bottom">Bottom content</div>
</div>
$(function() {
  FixedSticky.tests.sticky = false;
  $(".fixedsticky").fixedsticky();
});

Summary

While sticky provides a simpler implementation for basic sticky positioning, fixed-sticky offers more advanced features and better browser support. fixed-sticky is more suitable for complex layouts and projects requiring broader compatibility, while sticky may be preferable for simpler use cases or when minimizing file size is a priority.

This plugin is used to fix elements on the page (top, bottom, anywhere); however, it still allows the element to continue to move left or right with the horizontal scroll.

Pros of ScrollToFixed

  • More customizable with options for offsets, limit, and bottom spacing
  • Supports both horizontal and vertical scrolling
  • Provides callback functions for various events (e.g., preFixed, postFixed)

Cons of ScrollToFixed

  • Larger file size (11KB vs 4KB for Sticky)
  • More complex implementation, potentially harder to set up
  • Less actively maintained (last update in 2017 vs 2022 for Sticky)

Code Comparison

ScrollToFixed:

$('.selector').scrollToFixed({
    marginTop: 10,
    limit: $('.footer').offset().top - $('.selector').outerHeight(true) - 10,
    zIndex: 999
});

Sticky:

var sticky = new Sticky('.selector', {
    topSpacing: 10,
    bottomSpacing: 10,
    className: 'is-sticky'
});

Both libraries aim to create fixed-position elements on scroll, but ScrollToFixed offers more advanced features and customization options. Sticky, on the other hand, provides a simpler, more lightweight solution with a more recent update history. The choice between the two depends on the specific requirements of the project, such as the need for advanced features versus a simpler implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Sticky

Sticky is a jQuery plugin that gives you the ability to make any element on your page always stay visible.

Sticky in brief

This is how it works:

  • When the target element is about to be hidden, the plugin will add the class className to it (and to a wrapper added as its parent), set it to position: fixed and calculate its new top, based on the element's height, the page height and the topSpacing and bottomSpacing options.
  • That's it. In some cases you might need to set a fixed width to your element when it is "sticked". But by default (widthFromWrapper == true) sticky updates elements's width to the wrapper's width. Check the example-*.html files for some examples.

Usage

  • Include jQuery & Sticky.
  • Call Sticky.
<script src="jquery.js"></script>
<script src="jquery.sticky.js"></script>
<script>
  $(document).ready(function(){
    $("#sticker").sticky({topSpacing:0});
  });
</script>
  • Edit your css to position the elements (check the examples in example-*.html).

  • To unstick an object

<script>
  $("#sticker").unstick();
</script>

Options

  • topSpacing: (default: 0) Pixels between the page top and the element's top.
  • bottomSpacing: (default: 0) Pixels between the page bottom and the element's bottom.
  • className: (default: 'is-sticky') CSS class added to the element's wrapper when "sticked".
  • wrapperClassName: (default: 'sticky-wrapper') CSS class added to the wrapper.
  • center: (default: false) Boolean determining whether the sticky element should be horizontally centered in the page.
  • getWidthFrom: (default: '') Selector of element referenced to set fixed width of "sticky" element.
  • widthFromWrapper: (default: true) Boolean determining whether width of the "sticky" element should be updated to match the wrapper's width. Wrapper is a placeholder for "sticky" element while it is fixed (out of static elements flow), and its width depends on the context and CSS rules. Works only as long getWidthForm isn't set.
  • responsiveWidth: (default: false) Boolean determining whether widths will be recalculated on window resize (using getWidthfrom).
  • zIndex: (default: inherit) controls z-index of the sticked element.

Methods

  • sticky(options): Initializer. options is optional.
  • sticky('update'): Recalculates the element's position.

Events

  • sticky-start: When the element becomes sticky.
  • sticky-end: When the element returns to its original location
  • sticky-update: When the element is sticked but position must be updated for constraints reasons
  • sticky-bottom-reached: When the element reached the bottom space limit
  • sticky-bottom-unreached: When the element unreached the bottom space limit

To subscribe to events use jquery:

<script>
  $('#sticker').on('sticky-start', function() { console.log("Started"); });
  $('#sticker').on('sticky-end', function() { console.log("Ended"); });
  $('#sticker').on('sticky-update', function() { console.log("Update"); });
  $('#sticker').on('sticky-bottom-reached', function() { console.log("Bottom reached"); });
  $('#sticker').on('sticky-bottom-unreached', function() { console.log("Bottom unreached"); });
</script>

NPM DownloadsLast 30 Days