2016-12-18 15:53:28 +00:00
|
|
|
import jQuery from "jquery";
|
|
|
|
|
2014-07-05 00:31:20 +00:00
|
|
|
(function($) {
|
2016-04-17 10:50:03 +00:00
|
|
|
$.fn.unsticky = function() {
|
2016-05-25 07:41:41 +00:00
|
|
|
return this.trigger("unstick.sticky").unbind(".sticky");
|
2016-04-17 10:50:03 +00:00
|
|
|
};
|
2014-09-30 21:44:29 +00:00
|
|
|
|
2016-04-17 10:50:03 +00:00
|
|
|
$.fn.sticky = function() {
|
2014-07-05 00:31:20 +00:00
|
|
|
var self = this;
|
2016-04-17 10:50:03 +00:00
|
|
|
var stuckToBottom = true;
|
2016-05-25 07:41:41 +00:00
|
|
|
var lastStick = 0;
|
2016-04-17 10:50:03 +00:00
|
|
|
|
2016-05-25 07:41:41 +00:00
|
|
|
var keepToBottom = function() {
|
|
|
|
if (stuckToBottom) {
|
|
|
|
self.scrollBottom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(window).on("resize.sticky", keepToBottom);
|
2016-04-17 10:50:03 +00:00
|
|
|
self
|
2016-05-25 07:41:41 +00:00
|
|
|
.on("unstick.sticky", function() {
|
|
|
|
$(window).off("resize.sticky", keepToBottom);
|
2016-04-17 10:50:03 +00:00
|
|
|
})
|
2016-05-25 07:41:41 +00:00
|
|
|
.on("scroll.sticky", function() {
|
|
|
|
// When resizing, sometimes the browser sends a bunch of extra scroll events due to content
|
|
|
|
// reflow, so if we resized within 250ms we can assume it's one of those. The order of said
|
|
|
|
// events is not predictable, and scroll can happen last, so not setting stuckToBottom is
|
|
|
|
// not enough, we have to force the scroll still.
|
|
|
|
if (stuckToBottom && Date.now() - lastStick < 250) {
|
2016-04-17 10:50:03 +00:00
|
|
|
self.scrollBottom();
|
2016-05-25 07:41:41 +00:00
|
|
|
} else {
|
|
|
|
stuckToBottom = self.isScrollBottom();
|
2016-04-17 10:50:03 +00:00
|
|
|
}
|
|
|
|
})
|
2016-05-25 07:41:41 +00:00
|
|
|
.on("scrollBottom.sticky", function() {
|
|
|
|
stuckToBottom = true;
|
|
|
|
lastStick = Date.now();
|
|
|
|
this.scrollTop = this.scrollHeight;
|
|
|
|
})
|
2017-06-26 09:01:55 +00:00
|
|
|
.on("keepToBottom.sticky", keepToBottom)
|
2016-04-17 10:50:03 +00:00
|
|
|
.scrollBottom();
|
|
|
|
|
|
|
|
return self;
|
2014-07-05 00:31:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.scrollBottom = function() {
|
2016-05-25 07:41:41 +00:00
|
|
|
this.trigger("scrollBottom.sticky");
|
2016-04-17 10:50:03 +00:00
|
|
|
return this;
|
2014-07-05 00:31:20 +00:00
|
|
|
};
|
|
|
|
|
2016-04-17 10:50:03 +00:00
|
|
|
$.fn.isScrollBottom = function() {
|
|
|
|
var el = this[0];
|
|
|
|
return el.scrollHeight - el.scrollTop - el.offsetHeight <= 30;
|
2014-07-05 00:31:20 +00:00
|
|
|
};
|
|
|
|
})(jQuery);
|