Sticky scroll to bottom

This commit is contained in:
Mattias Erming 2014-03-06 10:53:02 -08:00
parent 7f3656e458
commit b45a3e09fd
2 changed files with 22 additions and 6 deletions

View File

@ -115,14 +115,15 @@ h2 {
padding: 0px 8px; padding: 0px 8px;
} }
#chat .messages { #chat .messages {
bottom: 26px;
left: 0; left: 0;
overflow-y: auto; overflow-y: auto;
padding: 6px 0; padding: 6px 0;
position: absolute; position: absolute;
right: 160px; right: 160px;
top: 36px; top: 36px;
word-wrap: break-word;
z-index: 0; z-index: 0;
bottom: 26px;
} }
#chat .messages .message { #chat .messages .message {
padding: 0 8px; padding: 0 8px;

View File

@ -20,9 +20,6 @@ $(function() {
var View = {}; var View = {};
View.refresh = function(event) { View.refresh = function(event) {
if (event.data == undefined || event.data == []) {
return;
}
chat.html(""); chat.html("");
event.data.forEach(function(network) { event.data.forEach(function(network) {
chat.append(Mustache.render(channels, network, { chat.append(Mustache.render(channels, network, {
@ -30,6 +27,7 @@ $(function() {
messages: messages messages: messages
})); }));
}); });
chat.find(".messages").scrollToBottom();
sidebar.html( sidebar.html(
Mustache.render(networks, { Mustache.render(networks, {
networks: event.data networks: event.data
@ -44,19 +42,23 @@ $(function() {
case "user": case "user":
target = ".users"; target = ".users";
render = Mustache.render( render = Mustache.render(
$("#users").html(), {users: event.data} users, {users: event.data}
); );
break; break;
case "message": case "message":
target = ".messages"; target = ".messages";
render = Mustache.render( render = Mustache.render(
$("#messages").html(), {messages: event.data} messages, {messages: event.data}
); );
break; break;
} }
if (target != "") { if (target != "") {
target = $("[data-id='" + event.target + "'] " + target); target = $("[data-id='" + event.target + "'] " + target);
var keepAtBottom = target.isScrollBottom();
target.append(render); target.append(render);
if (keepAtBottom) {
target.scrollToBottom();
}
} }
}; };
@ -86,9 +88,22 @@ $(function() {
}); });
}); });
(function() { (function() {
var highest = 1; var highest = 1;
$.fn.bringToTop = function() { $.fn.bringToTop = function() {
this.css('z-index', highest++); this.css('z-index', highest++);
}; };
$.fn.scrollToBottom = function() {
this.scrollTop(this.prop("scrollHeight"));
};
$.fn.isScrollBottom = function() {
var height = this.outerHeight();
var scroll = this.scrollTop();
if ((scroll + height + 5) >= this.prop("scrollHeight")) {
return true;
}
};
})(); })();