Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.

add distance option and fix multiple sticky error #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions jquery.sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
var splice = Array.prototype.splice; // save ref to original slice()

var defaults = {
distance: 0,
topSpacing: 0,
bottomSpacing: 0,
className: 'is-sticky',
Expand All @@ -34,7 +35,7 @@
getWidthFrom: '',
widthFromWrapper: true, // works only when .getWidthFrom is empty
responsiveWidth: false,
zIndex: 'inherit'
zIndex: 'auto'
},
$window = $(window),
$document = $(document),
Expand All @@ -47,9 +48,10 @@
extra = (scrollTop > dwh) ? dwh - scrollTop : 0;

for (var i = 0, l = sticked.length; i < l; i++) {
var s = sticked[i],
elementTop = s.stickyWrapper.offset().top,
etse = elementTop - s.topSpacing - extra;
var s = sticked[i];
if(!s) continue;
var elementTop = s.stickyWrapper.offset().top;
var etse = elementTop - s.topSpacing - extra;

//update height in case of dynamic content
s.stickyWrapper.css('height', s.stickyElement.outerHeight());
Expand All @@ -68,7 +70,7 @@
s.currentTop = null;
}
}
else {
else if(scrollTop - etse > s.distance){
var newTop = documentHeight - s.stickyElement.outerHeight()
- s.topSpacing - s.bottomSpacing - scrollTop - extra;
if (newTop < 0) {
Expand All @@ -79,8 +81,7 @@
if (s.currentTop !== newTop) {
var newWidth;
if (s.getWidthFrom) {
padding = s.stickyElement.innerWidth() - s.stickyElement.width();
newWidth = $(s.getWidthFrom).width() - padding || null;
newWidth = $(s.getWidthFrom).width() || null;
} else if (s.widthFromWrapper) {
newWidth = s.stickyWrapper.width();
}
Expand Down