Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Duplicate

Scrolling Overflowed DIVs with JavaScript


I have an AJAX chat that outputs the messages to a div with overflow: auto so if it gets too long, the scroll bar comes up. Is there is a way to have it auto scroll to the bottom whenever a new message is added? Preferably using jQuery?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

it'd be done with the following line

$("#youChatDiv").attr({ scrollTop: $("#youChatDiv").attr("scrollHeight") });

what this does is set the scroll to the height of the own element

just call that with every new message :)

cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...