• 0

Auto scrolling a DIV


Question

I'm trying to autoscroll my div so that it's always scrolled all the way to the bottom. The DIV is working correctly, but I'm not sure how to setup the JQUERY statement to do it.

Here's what I'm trying to do:


$(document).ready(function() {

var pathname = window.location.pathname;

$("#messages").load(pathname + '/loadMessages');

$("div#messages").scrollTop() = $("div#messages").scrollHeight;

$("chatInput").submit(function() {
$.post(pathname + '/postMessages', $('#chatInput').serialize(), function(data) {
$("#messages").append('<div>' + data + '</div>');
})
return false;
});

});
[/CODE]

I figure I probably need a .load event to make it start at the bottom and then a .submit for new posts, but I need help. Thanks in advance!

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I pretty much got it now with:


$("#messages").prop({
scrollTop: $("#messages").prop('scrollHeight')
});
[/CODE]

It's still not perfect, it only scrolls to the second to last post, but I'll get it.

Link to comment
Share on other sites

  • 0

function init() {

var div = document.getElementById("myDiv");

// increase the scroll position by 10 px every 10th of a second
setInterval(function() {
// make sure it's not at the bottom
if (div.scrollTop < div.scrollHeight - div.clientHeight)
div.scrollTop += 10; // move down
}, 100); // 100 milliseconds

}[/CODE]

Above would do fine for autoscrolling elements, why do you want use jquery when something can be done in just simple js?

And jquery has no built in autoscroll :/ so if you want to do it with jquery you should use a plugin which is more code then just using plain js >.>

Link to comment
Share on other sites

This topic is now closed to further replies.