BUGFIX: Fix issues if the DOM element is destroyed while the interval

is ongoing (in particular this was weird for Firefox)
This commit is contained in:
Robin Ward 2014-02-28 14:37:42 -05:00
parent 7d6984a915
commit 47357136dc
1 changed files with 12 additions and 2 deletions

View File

@ -8,8 +8,12 @@
};
LockOn.prototype.elementTop = function() {
var offsetCalculator = this.options.offsetCalculator;
return $(this.selector).offset().top - (offsetCalculator ? offsetCalculator() : 0);
var offsetCalculator = this.options.offsetCalculator,
selected = $(this.selector);
if (selected && selected.offset && selected.offset()) {
return selected.offset().top - (offsetCalculator ? offsetCalculator() : 0);
}
};
LockOn.prototype.lock = function() {
@ -26,6 +30,12 @@
var top = self.elementTop(),
scrollTop = $(window).scrollTop();
if (typeof(top) === "undefined") {
$('body,html').off(scrollEvents)
clearInterval(interval);
return;
}
if ((top !== previousTop) || (scrollTop !== top)) {
$(window).scrollTop(top);
previousTop = top;