简体   繁体   中英

Scrolling with jQuery Without using animate()

I'm building a messenger site and want the chat to automatically scroll to the bottom of the chat feed (latest message) when the page is refreshed. I am very new to jQuery and after a bit of searching I have got it to work using animate() . This is great when the page first loads too jarring when reloading after a form submit for example.

Is there any way to auto-scroll to the bottom of a container with out animating it?

// When page loads
$(document).ready(function(){
    // Load chat. ALSO the chat has to be hard coded in here.
    $('#MessagesContainer').load('/php/chat_ref.php');

    // Scroll to the bottom of the page. ALSO ".height()+500" was a bit of a bodge.
    $("#MessagesContainer").animate({ scrollTop:$('#MessagesContainer').height()+500}, 100);
    return false;
});

// Every 2 seconds.
var auto_refresh = setInterval(function(){
    // Reload the chat.
    $('#MessagesContainer').load('/php/chat_ref.php'); 
    return false;
}, 2000);

如何使用scrollTop代替动画?

$("#MessagesContainer").scrollTop($("#MessagesContainer").height());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM