Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dyrhoi .
3,173 PointsjQuery - AJAX, scroll to bottom of a div problems
Hey guys,
I have this problem,
I'm creating a chat, which updates every 2 secounds, via AJAX.
Now I want to scroll to the bottom, every time it loads, great that works.
Now problem is, if they scroll up to check old messages, it will scroll them down, annoying right? Now I tried coming up with a solution, which imo should work. But I'm stupid so. Anyone can help me?:
js function loadChat() {
$.ajax({
url: 'ajax/load_chat.php',
success: function(data) {
$('#chatbox').html(data);
//console.log($('#chatbox')[0].scrollHeight - $('#chatbox').innerHeight());
//console.log($('#chatbox').scrollTop());
if($('#chatbox')[0].scrollHeight - $('#chatbox').innerHeight() + 50 < $('#chatbox').scrollTop()) {
$("#chatbox").stop().animate({ scrollTop: $('#chatbox')[0].scrollHeight}, 200);
}
},
});
}
This code makes it scroll down no matter what, but my if statement should stop that... right?
Pastebin since Treehouse syntax wont work http://pastebin.com/AGcdtSmP