Browse Source

Sticky now handles bottom edge conditions better

pull/1785/merge
jlukic 9 years ago
parent
commit
29c4f82125
1 changed files with 62 additions and 47 deletions
  1. 109
      src/definitions/modules/sticky.js

109
src/definitions/modules/sticky.js

@ -30,7 +30,9 @@ $.fn.sticky = function(parameters) {
$allModules $allModules
.each(function() { .each(function() {
var var
settings = $.extend(true, {}, $.fn.sticky.settings, parameters),
settings = ( $.isPlainObject(parameters) )
? $.extend(true, {}, $.fn.sticky.settings, parameters)
: $.extend({}, $.fn.sticky.settings),
className = settings.className, className = settings.className,
namespace = settings.namespace, namespace = settings.namespace,
@ -62,35 +64,14 @@ $.fn.sticky = function(parameters) {
module = { module = {
initialize: function() { initialize: function() {
if(settings.context) {
$context = $(settings.context);
}
else {
$context = $container;
}
if($context.length === 0) {
module.error(error.invalidContext, settings.context, $module);
return;
}
module.determineContext();
module.verbose('Initializing sticky', settings, $container); module.verbose('Initializing sticky', settings, $container);
module.save.positions();
// error conditions
if( module.is.hidden() ) {
module.error(error.visible, $module);
}
if(module.cache.element.height > module.cache.context.height) {
module.reset();
module.error(error.elementSize, $module);
return;
}
module.save.positions();
module.checkErrors();
module.bind.events();
$window
.on('resize' + eventNamespace, module.event.resize)
;
$scroll
.on('scroll' + eventNamespace, module.event.scroll)
;
if(settings.observeChanges) { if(settings.observeChanges) {
module.observeChanges(); module.observeChanges();
} }
@ -111,15 +92,9 @@ $.fn.sticky = function(parameters) {
if(observer) { if(observer) {
observer.disconnect(); observer.disconnect();
} }
$window
.off('resize' + eventNamespace, module.event.resize)
;
$scroll
.off('scroll' + eventNamespace, module.event.scroll)
;
$module
.removeData(moduleNamespace)
;
$window.off('resize' + eventNamespace, module.event.resize);
$scroll.off('scroll' + eventNamespace, module.event.scroll);
$module.removeData(moduleNamespace);
}, },
observeChanges: function() { observeChanges: function() {
@ -146,6 +121,37 @@ $.fn.sticky = function(parameters) {
} }
}, },
determineContext: function() {
if(settings.context) {
$context = $(settings.context);
}
else {
$context = $container;
}
if($context.length === 0) {
module.error(error.invalidContext, settings.context, $module);
return;
}
},
checkErrors: function() {
if( module.is.hidden() ) {
module.error(error.visible, $module);
}
if(module.cache.element.height > module.cache.context.height) {
module.reset();
module.error(error.elementSize, $module);
return;
}
},
bind: {
events: function() {
$window.on('resize' + eventNamespace, module.event.resize);
$scroll.on('scroll' + eventNamespace, module.event.scroll);
}
},
event: { event: {
resize: function() { resize: function() {
requestAnimationFrame(function() { requestAnimationFrame(function() {
@ -177,9 +183,7 @@ $.fn.sticky = function(parameters) {
$element = $('<div/>'), $element = $('<div/>'),
element = $element.get() element = $element.get()
; ;
$element
.addClass(className.supported)
;
$element.addClass(className.supported);
return($element.css('position').match('sticky')); return($element.css('position').match('sticky'));
} }
}, },
@ -203,8 +207,9 @@ $.fn.sticky = function(parameters) {
height : $module.outerHeight() height : $module.outerHeight()
}, },
context = { context = {
offset: $context.offset(),
height: $context.outerHeight()
offset : $context.offset(),
height : $context.outerHeight(),
bottomPadding : parseInt($context.css('padding-bottom'), 10)
} }
; ;
module.cache = { module.cache = {
@ -221,9 +226,10 @@ $.fn.sticky = function(parameters) {
bottom : element.offset.top + element.height bottom : element.offset.top + element.height
}, },
context: { context: {
top : context.offset.top,
height : context.height,
bottom : context.offset.top + context.height
top : context.offset.top,
height : context.height,
bottomPadding : context.bottomPadding,
bottom : context.offset.top + context.height - context.bottomPadding
} }
}; };
module.set.containerSize(); module.set.containerSize();
@ -309,7 +315,9 @@ $.fn.sticky = function(parameters) {
else { else {
module.debug('Settings container size', module.cache.context.height); module.debug('Settings container size', module.cache.context.height);
if( Math.abs($container.height() - module.cache.context.height) > 5) { if( Math.abs($container.height() - module.cache.context.height) > 5) {
$container.height(module.cache.context.height);
$container.css({
height: module.cache.context.height
});
} }
} }
}, },
@ -389,7 +397,12 @@ $.fn.sticky = function(parameters) {
if(elementVisible) { if(elementVisible) {
if( module.is.initialPosition() ) { if( module.is.initialPosition() ) {
if(scroll.top >= element.top) {
if(scroll.top >= context.bottom) {
console.log(scroll.top, context.bottom);
module.debug('Element bottom of container');
module.bindBottom();
}
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();
} }
@ -470,7 +483,7 @@ $.fn.sticky = function(parameters) {
$module $module
.css('left' , '') .css('left' , '')
.css('top' , '') .css('top' , '')
.css('bottom' , '')
.css('bottom' , module.cache.context.bottomPadding)
.removeClass(className.fixed) .removeClass(className.fixed)
.removeClass(className.top) .removeClass(className.top)
.addClass(className.bound) .addClass(className.bound)
@ -491,6 +504,7 @@ $.fn.sticky = function(parameters) {
module.set.offset(); module.set.offset();
$module $module
.css('left', module.cache.element.left) .css('left', module.cache.element.left)
.css('bottom' , '')
.removeClass(className.bound) .removeClass(className.bound)
.removeClass(className.bottom) .removeClass(className.bottom)
.addClass(className.fixed) .addClass(className.fixed)
@ -504,6 +518,7 @@ $.fn.sticky = function(parameters) {
module.set.offset(); module.set.offset();
$module $module
.css('left', module.cache.element.left) .css('left', module.cache.element.left)
.css('bottom' , '')
.removeClass(className.bound) .removeClass(className.bound)
.removeClass(className.top) .removeClass(className.top)
.addClass(className.fixed) .addClass(className.fixed)

Loading…
Cancel
Save