ScrollToFixed
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.
Top Related Projects
jQuery Plugin for Sticky Objects
A jQuery plugin for creating smart sticky elements
Polyfill for CSS `position: sticky`
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
Quick Overview
ScrollToFixed is a jQuery plugin that allows elements on a webpage to become fixed at a specific position as the user scrolls. It's particularly useful for creating sticky headers, sidebars, or other elements that need to remain visible while scrolling through content.
Pros
- Easy to implement with minimal configuration required
- Supports both top and bottom fixing of elements
- Allows for custom offset settings and z-index control
- Provides callback functions for various scroll events
Cons
- Depends on jQuery, which may not be ideal for modern web development practices
- Limited documentation and examples available
- Not actively maintained (last update was several years ago)
- May have performance issues on pages with many fixed elements
Code Examples
- Basic usage:
$('.header').scrollToFixed();
This code fixes the element with the class 'header' to the top of the page when scrolling.
- Setting a custom offset:
$('.sidebar').scrollToFixed({
marginTop: 100,
zIndex: 100
});
This example fixes the sidebar element 100 pixels from the top and sets its z-index to 100.
- Using callback functions:
$('.footer').scrollToFixed({
bottom: 0,
limit: $('.footer').offset().top,
preFixed: function() { $(this).css('color', 'red'); },
postFixed: function() { $(this).css('color', ''); }
});
This code fixes the footer to the bottom of the page, sets a limit for when it should stop being fixed, and changes its color when it becomes fixed and unfixed.
Getting Started
- Include jQuery and the ScrollToFixed script in your HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="path/to/jquery-scrolltofixed.js"></script>
- Apply ScrollToFixed to your desired element:
$(document).ready(function() {
$('.your-element').scrollToFixed();
});
- Customize options as needed:
$('.your-element').scrollToFixed({
marginTop: 50,
limit: function() {
return $('.footer').offset().top - $('.your-element').outerHeight(true);
}
});
This example sets a top margin of 50 pixels and limits the fixed positioning to stop above the footer.
Competitor Comparisons
jQuery Plugin for Sticky Objects
Pros of Sticky
- Lightweight and simple implementation
- No jQuery dependency, pure JavaScript
- Supports multiple sticky elements on a page
Cons of Sticky
- Less customization options
- Limited functionality compared to ScrollToFixed
- May not handle complex scenarios as effectively
Code Comparison
ScrollToFixed:
$('.selector').scrollToFixed({
marginTop: 10,
limit: $('.footer').offset().top
});
Sticky:
var sticky = new Sticky('.selector');
Key Differences
- ScrollToFixed relies on jQuery, while Sticky is a vanilla JavaScript solution
- ScrollToFixed offers more configuration options out of the box
- Sticky has a simpler API and implementation
- ScrollToFixed provides more advanced features like limits and callbacks
Use Cases
- ScrollToFixed: Better for complex layouts with multiple fixed elements and specific positioning requirements
- Sticky: Ideal for simple scenarios where you need basic sticky functionality without additional dependencies
Community and Maintenance
- ScrollToFixed has more stars and forks on GitHub
- Sticky has fewer open issues and appears to be more actively maintained
Performance
- Sticky may have a slight edge in performance due to its lightweight nature and lack of jQuery dependency
- ScrollToFixed's additional features may come at the cost of slightly higher resource usage
A jQuery plugin for creating smart sticky elements
Pros of sticky-kit
- More lightweight and simpler implementation
- Supports both jQuery and Zepto libraries
- Offers smoother animations and transitions
Cons of sticky-kit
- Less actively maintained (last update in 2018)
- Fewer configuration options compared to ScrollToFixed
- Limited browser compatibility for older versions
Code Comparison
ScrollToFixed:
$('.header').scrollToFixed({
marginTop: 10,
limit: $('.footer').offset().top
});
sticky-kit:
$(".sidebar").stick_in_parent({
offset_top: 20
});
Both libraries aim to create sticky elements, but ScrollToFixed offers more granular control over the fixed positioning, while sticky-kit provides a simpler API with fewer options. ScrollToFixed is more actively maintained and has a larger user base, which may lead to better support and more frequent updates. However, sticky-kit's lightweight nature and support for multiple libraries make it a good choice for simpler projects or those already using Zepto.
When choosing between the two, consider your project's specific requirements, desired level of customization, and the importance of ongoing maintenance and updates.
Polyfill for CSS `position: sticky`
Pros of Stickyfill
- Polyfills the native
position: sticky
CSS property, providing better compatibility with modern web standards - Lightweight and focused on a single functionality, potentially leading to better performance
- Automatically detects and applies the polyfill only when necessary
Cons of Stickyfill
- Limited to
position: sticky
functionality, while ScrollToFixed offers more customization options - May not work as smoothly with complex layouts or dynamic content changes
- Less actively maintained, with fewer recent updates compared to ScrollToFixed
Code Comparison
Stickyfill:
var elements = document.querySelectorAll('.sticky');
Stickyfill.add(elements);
ScrollToFixed:
$('.header').scrollToFixed({
marginTop: 10,
limit: $('.footer').offset().top
});
Summary
Stickyfill focuses on providing a polyfill for the position: sticky
CSS property, making it a lightweight and standards-compliant solution. It's ideal for projects that prioritize modern web standards and minimal JavaScript intervention.
ScrollToFixed, on the other hand, offers more customization options and flexibility in creating fixed-position elements. It's better suited for projects requiring complex scrolling behaviors or support for older browsers.
The choice between the two depends on the specific project requirements, target browser support, and desired level of customization.
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
Pros of floatThead
- More focused on table header functionality, providing better performance for large tables
- Supports both window and overflow scrolling scenarios
- Offers more customization options and events for fine-tuning behavior
Cons of floatThead
- Limited to table headers, while ScrollToFixed can work with any element
- May require more setup and configuration for complex use cases
- Potentially heavier in terms of code size and performance impact for simpler scenarios
Code Comparison
ScrollToFixed:
$('#tableHeader').scrollToFixed();
floatThead:
$('table').floatThead({
position: 'fixed',
scrollContainer: function($table){
return $table.closest('.wrapper');
}
});
ScrollToFixed provides a simpler API for basic use cases, while floatThead offers more advanced options for table-specific scenarios. ScrollToFixed can be applied to any element, making it more versatile for general fixed positioning needs. However, floatThead excels in handling table headers specifically, with better performance for large tables and more customization options.
The choice between these libraries depends on the specific requirements of your project. If you need a general-purpose solution for fixing elements during scroll, ScrollToFixed might be more suitable. For projects focused on table functionality, particularly those with large datasets or complex scrolling behaviors, floatThead could be the better option.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Demo: https://bigspotteddog.github.io/ScrollToFixed/
ScrollToFixed
This jQuery 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.
Given an option marginTop, the element will stop moving vertically upward once the vertical scroll has reached the target position; but, the element will still move horizontally as the page is scrolled left or right. Once the page has been scrolled back down past the target position, the element will be restored to its original position on the page.
This plugin has been tested in Firefox 3+, Google Chrome 10+, Safari 5+, Internet Explorer 8/9, and Opera 11.60+.
This plugin was inspired by the excellent tutorial presented by Remy Sharp, titled "Fixed Floating Elements". You will find that tutorial here.
Notices
IMPORTANT: The latest version of this plugin reverts the offset adjustment code that added the difference between the left offset and position to the left offset. For anyone that needed it, that code is now turned on by using the offsets: true option.
UPDATE: A new option 'dontCheckForPositionFixedSupport' was added to disable the check for position:fixed support. Some iOS and Android vesions now support position:fixed; we attempt to detect support and continue instantiating the plugin if supported.
UPDATE: The fixed position support detection (above) is now turned off completely.
UPDATE: The "remove" trigger was renamed as "detach" to avoid the new Google Chrome (24) native "remove" method.
UPDATE: The 'dontCheckForPositionFixedSupport' option was commented out as it did not accurately detect support.
UPDATE: A new option was added by murb, 'dontSetWidth', for box-sizing: border-box that does not set the width on the target element when it goes fixed or absolute.
Usage
Default options:
$(document).ready(function() {
$('#mydiv').scrollToFixed();
});
Margin and Limit options:
$(document).ready(function() {
$('#cart').scrollToFixed({ marginTop: 10, limit: $($('h2')[5]).offset().top });
});
Fixed Header and Fixed Footer with a Limit
// The fixed footer will go unfixed to reveal whatever is below it when scrolled
// past the limit.
$(document).ready(function() {
$('.header').scrollToFixed();
$('.footer').scrollToFixed( { bottom: 0, limit: $('.footer').offset().top } );
});
Very Full Example
$(document).ready(function() {
$('.header').scrollToFixed({
preFixed: function() { $(this).find('h1').css('color', 'blue'); },
postFixed: function() { $(this).find('h1').css('color', ''); }
});
$('.footer').scrollToFixed( {
bottom: 0,
limit: $('.footer').offset().top,
preFixed: function() { $(this).find('h1').css('color', 'blue'); },
postFixed: function() { $(this).find('h1').css('color', ''); }
});
// Order matters because our summary limit is based on the position
// of the footer. On window refresh, the summary needs to recalculate
// after the footer.
$('#summary').scrollToFixed({
marginTop: $('.header').outerHeight() + 10,
limit: function() {
var limit = $('.footer').offset().top - $('#summary').outerHeight(true) - 10;
return limit;
},
zIndex: 999,
preFixed: function() { $(this).find('.title').css('color', 'blue'); },
preAbsolute: function() { $(this).find('.title').css('color', 'red'); },
postFixed: function() { $(this).find('.title').css('color', ''); },
postAbsolute: function() { $(this).find('.title').css('color', ''); }
});
});
// returns whether or not the ScrollToFixed plugin has been applied to the element.
var b = $.isScrollToFixed('.header');
Triggers
$('.header').trigger('detach.ScrollToFixed'); // Removes scrollToFixed from the element. The
// namespace ensures remove will not be called
// on other plugins that may be listening for
// that event! NOTE: Renamed as "detach" to
// avoid the new Chrome native "remove" method.
$('.header').trigger('resize'); // Resizes the spacer in case the fixed element height changes.
// Good for size changes to the fixed element.
$(window).scroll(); // Causes the plugin to recalculate the window scoll.
// Good for layout changes that could change the fixed element's response to
// the scroll. Example: the fixed element height expands which should cause
// it to invoke its limit.
$(window).resize(); // Causes the plugin to recalculate the element offsets, then the window scroll.
// Good for layout changes that could cause the fixed element to move.
// Example: the header height increases which should cause the fixed
// element to fix at a greater vertical scroll position.
Options
- marginTop (value|function) - the number of pixels between the top of the window and the fixed element.
- limit (value|function) - the vertical scroll position at which the element will begin to scroll up the page (absolutely).
- bottom - (fix to bottom) the number of pixels between the bottom of the window and the bottom of the fixed element.
- zIndex - the z-index of the fixed element.
- spacerClass - the class to add to the spacer for styling purposes.
- preFixed - the function handler triggered just before the element goes fixed.
- fixed - the function handler triggered just after the element goes fixed.
- postFixed - the function handler triggered just after the element leaves fixed.
- preUnfixed - the function handler triggered just before the element goes unfixed.
- unfixed - the function handler triggered just after the element goes unfixed.
- postUnfixed - the function handler triggered just after the element leaves unfixed.
- preAbsolute - the function handler triggered just before the element goes absolute.
- postAbsolute - the function handler triggered just after the element leaves absolute.
- offsets - (true|false|not present) some websites have needed an adjustment to the left position of the element due to something in their layout. This option turns this adjustment on.
- minWidth (number) - the minimum width the window must be to "fix" the target element. Turns off the functionaility when the window width is less than specified.
- maxWidth (number) - the maximum width the window must be to "fix" the target element. Turns off the functionaility when the window width is more than specified.
- dontCheckForPositionFixedSupport - (true|false|not present) some devices do not support position fixed; we check to see if it does. This option turns off that check if set to true.
- dontSetWidth - (true|false|not set) box sizing that does not set the width on the target element when it goes fixed or absolute.
- removeOffsets - (true|false|not set) recalculate top offset and delete left offset when the element goes absolute.
Demos
- http://jsfiddle.net/y3qV5/434/ - floating cart summary with a limit.
- http://jsfiddle.net/k2R3G/81/ - fixed header; allows content to flow under it.
- http://jsfiddle.net/ZczEt/167/ - very full example: fixed header, footer and floating summary, with events.
- http://jsfiddle.net/y3qV5/435/ - 2 cart summaries that scroll up and stop at different intervals.
- http://jsfiddle.net/y3qV5/769/ - Another multi-cart example using floats, with section stops.
- http://jsfiddle.net/ZZYpG/68/ - Endlessly scrolling date markers.
- http://jsfiddle.net/y3qV5/1730/ - Yahoo's new sidebar.
Top Related Projects
jQuery Plugin for Sticky Objects
A jQuery plugin for creating smart sticky elements
Polyfill for CSS `position: sticky`
Fixed <thead>. Doesn't need any custom css/html. Does what position:sticky can't
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot