I'm trying to make a div scroll to the bottom after adding new content (using append)
Here is the main part:
$('#textdiv').append('<p>Lorem ipsum dolor sit amet, solet nostrud concludaturque no eam. Ne quod recteque pri. Porro nulla zril mei eu. Eu nibh rebum pri, eu est maiorum menandri, ridens tamquam abhorreant te eum. Ipsum definiebas ad mel.</p>'); $('#textdiv').animate({scrollTop: $('#textdiv').height()}, 1000);
See/try it in the fiddle: http://jsfiddle.net/5ucD3/7/
Very buggy.. It does not scroll to the bottom (only maybe at first few appends). When I scroll down and add new content, it scrolls up. Sometimes it does not animate at all.
How do I get it to work always? I figure I have to somehow get the correct height, but don't know how
Use element. scrollintoview() to Scroll to Bottom of Div in JavaScript. The Element.
Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.
To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.
And if you are in browser's javascript console, it might be useful to be able to stop the scrolling, so add: var stopScroll = function() { clearInterval(scrollInterval); }; And then use stopScroll(); . @nobjta_9x_tq this will work only if the page is loaded up to the end.
I found a way that works:
$('#textdiv').animate({scrollTop: $('#textdiv').prop("scrollHeight")}, 500);
http://jsfiddle.net/5ucD3/13/
I'm late again, but here's my two cents.
Sometimes when measuring dynamic elements using only one reading can be misguiding because the content being appended might be long, or because the request ($.get, $.post, $.ajax, etc...) for content is still in the air. If the string being appended is coming from a request, then you have to use a callback method to append the response.
The best thing you can do is string it together like this, because javascript "should" resolve the functions synchronous fashion:
$("#textdiv").append('The longest string ever parsed goes here.') .animate({scrollTop: $('#textdiv').prop("scrollHeight")}, 500);
But you're right, this method tends to be buggy, especially while dealing with divs that mutate fast enough.
That's why if you want to be extra sure that the job gets done, you could use an Interval:
var scroll_to_bottom = function(element){ var tries = 0, old_height = new_height = element.height(); var intervalId = setInterval(function() { if( old_height != new_height ){ // Env loaded clearInterval(intervalId); element.animate({ scrollTop: new_height }, 'slow'); }else if(tries >= 30){ // Give up and scroll anyway clearInterval(intervalId); element.animate({ scrollTop: new_height }, 'slow'); }else{ new_height = content.height(); tries++; } }, 100); } $('#textdiv').append('The longest string ever parsed goes here.'); scroll_to_bottom($('#textdiv'));
This method cant beat a callback or a simple function line up, but it solves the problem if you don't know exactly when the append is going to end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With