Browse Source

#2366, fixes when sticky content is larger than context height. Fixes margin bottom not clearing when pushing: true

pull/2369/head
jlukic 9 years ago
parent
commit
629eeff892
1 changed files with 61 additions and 14 deletions
  1. 75
      src/definitions/modules/sticky.js

75
src/definitions/modules/sticky.js

@ -239,6 +239,9 @@ $.fn.sticky = function(parameters) {
offset : $context.offset(), offset : $context.offset(),
height : $context.outerHeight(), height : $context.outerHeight(),
bottomPadding : parseInt($context.css('padding-bottom'), 10) bottomPadding : parseInt($context.css('padding-bottom'), 10)
},
container = {
height: $container.outerHeight()
} }
; ;
module.cache = { module.cache = {
@ -333,7 +336,9 @@ $.fn.sticky = function(parameters) {
set: { set: {
offset: function() { offset: function() {
module.verbose('Setting offset on element', settings.offset); module.verbose('Setting offset on element', settings.offset);
$module.css('margin-top', settings.offset);
$module
.css('margin-top', settings.offset)
;
}, },
containerSize: function() { containerSize: function() {
var var
@ -345,7 +350,8 @@ $.fn.sticky = function(parameters) {
module.determineContainer(); module.determineContainer();
} }
else { else {
if( Math.abs($container.height() - module.cache.context.height) > 5) {
console.log($container.outerHeight(), module.cache.context.height, $module);
if( Math.abs($container.outerHeight() - module.cache.context.height) > settings.jitter) {
module.debug('Context has padding, specifying exact height for container', module.cache.context.height); module.debug('Context has padding, specifying exact height for container', module.cache.context.height);
$container.css({ $container.css({
height: module.cache.context.height height: module.cache.context.height
@ -353,6 +359,14 @@ $.fn.sticky = function(parameters) {
} }
} }
}, },
minimumSize: function() {
var
element = module.cache.element
;
$container
.css('min-height', element.height)
;
},
scroll: function(scroll) { scroll: function(scroll) {
module.debug('Setting scroll on element', scroll); module.debug('Setting scroll on element', scroll);
if(module.elementScroll == scroll) { if(module.elementScroll == scroll) {
@ -432,11 +446,11 @@ $.fn.sticky = function(parameters) {
if(elementVisible) { if(elementVisible) {
if( module.is.initialPosition() ) { if( module.is.initialPosition() ) {
if(scroll.top >= context.bottom) {
if(scroll.top > context.bottom) {
module.debug('Element bottom of container'); module.debug('Element bottom of container');
module.bindBottom(); module.bindBottom();
} }
else if(scroll.top >= element.top) {
else if(scroll.top > element.top) {
module.debug('Element passed, fixing element to page'); module.debug('Element passed, fixing element to page');
module.fixTop(); module.fixTop();
} }
@ -504,9 +518,11 @@ $.fn.sticky = function(parameters) {
module.debug('Binding element to top of parent container'); module.debug('Binding element to top of parent container');
module.remove.offset(); module.remove.offset();
$module $module
.css('left' , '')
.css('top' , '')
.css('margin-bottom' , '')
.css({
left : '',
top : '',
marginBottom : ''
})
.removeClass(className.fixed) .removeClass(className.fixed)
.removeClass(className.bottom) .removeClass(className.bottom)
.addClass(className.bound) .addClass(className.bound)
@ -519,9 +535,11 @@ $.fn.sticky = function(parameters) {
module.debug('Binding element to bottom of parent container'); module.debug('Binding element to bottom of parent container');
module.remove.offset(); module.remove.offset();
$module $module
.css('left' , '')
.css('top' , '')
.css('margin-bottom' , module.cache.context.bottomPadding)
.css({
left : '',
top : '',
marginBottom : module.cache.context.bottomPadding
})
.removeClass(className.fixed) .removeClass(className.fixed)
.removeClass(className.top) .removeClass(className.top)
.addClass(className.bound) .addClass(className.bound)
@ -539,10 +557,14 @@ $.fn.sticky = function(parameters) {
fixTop: function() { fixTop: function() {
module.debug('Fixing element to top of page'); module.debug('Fixing element to top of page');
module.set.minimumSize();
module.set.offset(); module.set.offset();
$module $module
.css('left', module.cache.element.left)
.css('bottom' , '')
.css({
left : module.cache.element.left,
bottom : '',
marginBottom : ''
})
.removeClass(className.bound) .removeClass(className.bound)
.removeClass(className.bottom) .removeClass(className.bottom)
.addClass(className.fixed) .addClass(className.fixed)
@ -553,10 +575,14 @@ $.fn.sticky = function(parameters) {
fixBottom: function() { fixBottom: function() {
module.debug('Sticking element to bottom of page'); module.debug('Sticking element to bottom of page');
module.set.minimumSize();
module.set.offset(); module.set.offset();
$module $module
.css('left', module.cache.element.left)
.css('bottom' , '')
.css({
left : module.cache.element.left,
bottom : '',
marginBottom : ''
})
.removeClass(className.bound) .removeClass(className.bound)
.removeClass(className.top) .removeClass(className.top)
.addClass(className.fixed) .addClass(className.fixed)
@ -792,20 +818,41 @@ $.fn.sticky.settings = {
verbose : true, verbose : true,
performance : true, performance : true,
// whether to stick in the opposite direction on scroll up
pushing : false, pushing : false,
context : false, context : false,
// Context to watch scroll events
scrollContext : window, scrollContext : window,
// Offset to adjust scroll
offset : 0, offset : 0,
// Offset to adjust scroll when attached to bottom of screen
bottomOffset : 0, bottomOffset : 0,
jitter : 5, // will only set container height if difference between context and container is larger than this number
// Whether to automatically observe changes with Mutation Observers
observeChanges : false, observeChanges : false,
// Called when position is recalculated
onReposition : function(){}, onReposition : function(){},
// Called on each scroll
onScroll : function(){}, onScroll : function(){},
// Called when element is stuck to viewport
onStick : function(){}, onStick : function(){},
// Called when element is unstuck from viewport
onUnstick : function(){}, onUnstick : function(){},
// Called when element reaches top of context
onTop : function(){}, onTop : function(){},
// Called when element reaches bottom of context
onBottom : function(){}, onBottom : function(){},
error : { error : {

Loading…
Cancel
Save