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

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - AJAX chat, auto scroll window

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...