2015-03-04 23:01:17 -05:00
|
|
|
// Dear traveller, you are entering a zone where we are at war with the browser
|
|
|
|
// the browser is insisting on positioning scrollTop per the location it was in
|
|
|
|
// the past, we are insisting on it being where we want it to be
|
2015-03-04 23:09:31 -05:00
|
|
|
// The hack is just to keep trying over and over to position the scrollbar (up to 1 minute)
|
2015-03-04 23:01:17 -05:00
|
|
|
//
|
|
|
|
// The root cause is that a "refresh" on a topic page will almost never be at the
|
|
|
|
// same position it was in the past, the URL points to the post at the top of the
|
|
|
|
// page, so a refresh will try to bring that post into view causing drift
|
|
|
|
//
|
|
|
|
// Additionally if you loaded multiple batches of posts, on refresh they will not
|
|
|
|
// be loaded.
|
|
|
|
//
|
|
|
|
// This hack leads to a slight jerky experience, however other workarounds are more
|
|
|
|
// complex, the 2 options we have are
|
|
|
|
//
|
|
|
|
// 1. onbeforeunload ensure we are scrolled to the right spot
|
|
|
|
// 2. give up on the scrollbar and implement it ourselves (something that will happen)
|
|
|
|
|
2013-11-20 16:33:36 -05:00
|
|
|
(function (exports) {
|
|
|
|
|
|
|
|
var scrollEvents = "scroll.lock-on touchmove.lock-on mousedown.lock-on wheel.lock-on DOMMouseScroll.lock-on mousewheel.lock-on keyup.lock-on";
|
|
|
|
|
|
|
|
var LockOn = function(selector, options) {
|
|
|
|
this.selector = selector;
|
|
|
|
this.options = options || {};
|
|
|
|
};
|
|
|
|
|
|
|
|
LockOn.prototype.elementTop = function() {
|
2014-02-28 14:37:42 -05:00
|
|
|
var offsetCalculator = this.options.offsetCalculator,
|
|
|
|
selected = $(this.selector);
|
|
|
|
|
|
|
|
if (selected && selected.offset && selected.offset()) {
|
|
|
|
return selected.offset().top - (offsetCalculator ? offsetCalculator() : 0);
|
|
|
|
}
|
2013-11-20 16:33:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
LockOn.prototype.lock = function() {
|
|
|
|
var self = this,
|
|
|
|
previousTop = this.elementTop(),
|
2015-03-04 23:01:17 -05:00
|
|
|
startedAt = new Date().getTime(),
|
2013-11-20 16:33:36 -05:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
$(window).scrollTop(previousTop);
|
|
|
|
|
2015-03-04 23:01:17 -05:00
|
|
|
var within = function(threshold,x,y) {
|
|
|
|
return Math.abs(x-y) < threshold;
|
|
|
|
};
|
|
|
|
|
2013-11-20 16:33:36 -05:00
|
|
|
var interval = setInterval(function() {
|
|
|
|
i = i + 1;
|
|
|
|
|
|
|
|
var top = self.elementTop(),
|
|
|
|
scrollTop = $(window).scrollTop();
|
|
|
|
|
2014-02-28 14:37:42 -05:00
|
|
|
if (typeof(top) === "undefined") {
|
2015-03-04 23:01:17 -05:00
|
|
|
$('body,html').off(scrollEvents);
|
2014-02-28 14:37:42 -05:00
|
|
|
clearInterval(interval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:01:17 -05:00
|
|
|
if (!within(4, top, previousTop) || !within(4, scrollTop, top)) {
|
2013-11-20 16:33:36 -05:00
|
|
|
$(window).scrollTop(top);
|
2015-03-04 23:01:17 -05:00
|
|
|
// animating = true;
|
|
|
|
// $('html,body').animate({scrollTop: parseInt(top,10)+'px'}, 200, 'swing', function(){
|
|
|
|
// animating = false;
|
|
|
|
// });
|
2013-11-20 16:33:36 -05:00
|
|
|
previousTop = top;
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:01:17 -05:00
|
|
|
// We commit suicide after 3s just to clean up
|
2013-11-20 16:33:36 -05:00
|
|
|
var nowTime = new Date().getTime();
|
|
|
|
if (nowTime - startedAt > 1000) {
|
2015-03-04 23:01:17 -05:00
|
|
|
$('body,html').off(scrollEvents);
|
2013-11-20 16:33:36 -05:00
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 50);
|
|
|
|
|
|
|
|
$('body,html').off(scrollEvents).on(scrollEvents, function(e){
|
|
|
|
if ( e.which > 0 || e.type === "mousedown" || e.type === "mousewheel" || e.type === "touchmove") {
|
|
|
|
$('body,html').off(scrollEvents);
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
2015-03-04 23:01:17 -05:00
|
|
|
});
|
2013-11-20 16:33:36 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.LockOn = LockOn;
|
|
|
|
|
|
|
|
})(window);
|