From 75f04e6cd052e69f4b1c385e7cb5a69ba3c4de5e Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 17 Apr 2016 13:50:03 +0300 Subject: [PATCH] Improve sticky scroll --- client/js/libs/jquery/stickyscroll.js | 79 ++++++++------------------- client/js/lounge.js | 15 +++-- 2 files changed, 33 insertions(+), 61 deletions(-) diff --git a/client/js/libs/jquery/stickyscroll.js b/client/js/libs/jquery/stickyscroll.js index bad45dd5..d86740b3 100644 --- a/client/js/libs/jquery/stickyscroll.js +++ b/client/js/libs/jquery/stickyscroll.js @@ -1,67 +1,34 @@ -/*! - * stickyscroll - * https://github.com/erming/stickyscroll - * v2.2.0 - */ (function($) { + $.fn.unsticky = function() { + return this.unbind(".sticky"); + }; + $.fn.sticky = function() { - if (this.size() > 1) { - return this.each(function() { - $(this).sticky(options); - }); - } - - var isBottom = false; var self = this; + var stuckToBottom = true; - this.unbind(".sticky"); - this.on("beforeAppend.sticky", function() { - isBottom = isScrollBottom.call(self); - }); + self + .on("scroll.sticky", function(e) { + stuckToBottom = self.isScrollBottom(); + }) + .on("msg.sticky", function() { + if (stuckToBottom) { + self.scrollBottom(); + } + }) + .scrollBottom(); - this.on("afterAppend.sticky", function() { - if (isBottom) { - self.scrollBottom(); - } - }); - - var overflow = this.css("overflow-y"); - if (overflow == "visible") { - overflow = "auto"; - } - this.css({ - "overflow-y": overflow - }); - - $(window).unbind(".sticky"); - $(window).on("resize.sticky", function() { - self.scrollBottom(); - }); - - this.scrollBottom(); - return this; + return self; }; $.fn.scrollBottom = function() { - return this.each(function() { - $(this).animate({scrollTop: this.scrollHeight}, 0); - }); - }; - - $.fn.isScrollBottom = isScrollBottom; - - function isScrollBottom() { - if ((this.scrollTop() + this.outerHeight() + 1) >= this.prop("scrollHeight")) { - return true; - } else { - return false; - } - }; - - var append = $.fn.append; - $.fn.append = function() { - this.trigger("beforeAppend"); - append.apply(this, arguments).trigger("afterAppend") + var el = this[0]; + this.scrollTop(el.scrollHeight); return this; }; + + $.fn.isScrollBottom = function() { + var el = this[0]; + return el.scrollHeight - el.scrollTop - el.offsetHeight <= 30; + }; })(jQuery); diff --git a/client/js/lounge.js b/client/js/lounge.js index 3106e094..a1068eb6 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -656,15 +656,20 @@ $(function() { } viewport.removeClass("lt"); - $("#windows .active").removeClass("active"); + $("#windows .active") + .removeClass("active") + .find(".chat") + .unsticky(); var chan = $(target) .addClass("active") .trigger("show") - .css("z-index", top++) - .find(".chat") - .sticky() - .end(); + .css("z-index", top++); + + var chanChat = chan.find(".chat"); + if (chanChat.length > 0) { + chanChat.sticky(); + } var title = "The Lounge"; if (chan.data("title")) {