简体   繁体   English

使用jQuery滚动而不使用animate()

[英]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. 我正在建立一个Messenger网站,并希望当刷新页面时聊天自动滚动到聊天源(最新消息)的底部。 I am very new to jQuery and after a bit of searching I have got it to work using animate() . 我对jQuery非常陌生,经过一番搜索后,我使用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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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