Added sticky jQuery plugin

This commit is contained in:
Mattias Erming 2014-03-09 19:39:25 +01:00
parent bd963fbd8c
commit a783a71789

View File

@ -31,7 +31,7 @@ $(function() {
}) })
); );
chat.find(".messages").scrollToBottom(); chat.find(".messages").sticky();
chat.find(".window") chat.find(".window")
// Sort windows by `data-id` value. // Sort windows by `data-id` value.
.sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); }) .sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); })
@ -59,8 +59,7 @@ $(function() {
}); });
}); });
(function($) {
(function() {
var highest = 1; var highest = 1;
$.fn.bringToTop = function() { $.fn.bringToTop = function() {
return this return this
@ -68,16 +67,46 @@ $(function() {
.find("input") .find("input")
.focus(); .focus();
}; };
})(jQuery);
$.fn.scrollToBottom = function() { // Sticky plugin
return this.scrollTop(this.prop("scrollHeight")); // https://github.com/erming/sticky
(function($) {
var append = $.fn.append;
$.fn.append = function() {
return append.apply(this, arguments).trigger("append");
}; };
$.fn.isScrollBottom = function() { $.fn.sticky = function() {
var height = this.outerHeight(); var self = this;
var scroll = this.scrollTop(); if (self.size() > 1) {
if ((scroll + height + 5) >= this.prop("scrollHeight")) { return self.each(function() {
$(this).sticky();
});
}
var sticky = false;
self.on("scroll", function() {
sticky = self.isScrollAtBottom();
});
self.trigger("scroll");
self.on("append", function() {
if (sticky) {
self.scrollToBottom();
}
});
return this;
};
$.fn.scrollToBottom = function() {
this.scrollTop(this.prop("scrollHeight"));
};
$.fn.isScrollAtBottom = function() {
if ((this.scrollTop() + this.outerHeight()) >= this.prop("scrollHeight")) {
return true; return true;
} }
}; };
})(); })(jQuery);