discourse/vendor/assets/javascripts/lock-on.js
Robin Ward 40f86829f7 Post Cloaking:
* We now use a new custom view, {{cloaked-collection}} to display posts in a topic.

* Posts are removed and inserted (cloaked/uncloaked) into the DOM dynamically based on whether they
  are visible in the current browser viewport.

* There's been a lot of refactoring to ensure the relationship between the post views and the topic
  controller is sane.

* Lots of fixes involving jumping to a post, including a new LockOn component to that tries to stay
  focused on an element even if stuff is loading before it in the DOM that would normally push it
  down.
2013-11-27 12:53:32 -05:00

55 lines
1.5 KiB
JavaScript

(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() {
var offsetCalculator = this.options.offsetCalculator;
return $(this.selector).offset().top - (offsetCalculator ? offsetCalculator() : 0);
};
LockOn.prototype.lock = function() {
var self = this,
previousTop = this.elementTop(),
startedAt = new Date().getTime()
i = 0;
$(window).scrollTop(previousTop);
var interval = setInterval(function() {
i = i + 1;
var top = self.elementTop(),
scrollTop = $(window).scrollTop();
if ((top !== previousTop) || (scrollTop !== top)) {
$(window).scrollTop(top);
previousTop = top;
}
// We commit suicide after 1s just to clean up
var nowTime = new Date().getTime();
if (nowTime - startedAt > 1000) {
$('body,html').off(scrollEvents)
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);
}
})
};
exports.LockOn = LockOn;
})(window);