简体   繁体   English

向下滚动动画js

[英]Scroll down animation js

I built a kind of chat in JS and then I wanted that when I got new message the chat automatically scrolled down (with animation...). 我在JS中建立了一种聊天方式,然后我希望当我收到新消息时,聊天内容自动向下滚动(带有动画...)。 Everything worked beautifully, but after the animation stopped the user couldn't scroll by himself; 一切正常,但是动画停止后,用户无法自己滚动。 the chat automatically scrolled to the end. 聊天会自动滚动到结尾。

So this is the code : 所以这是代码:

<!-- language:lang-js -->

var height = 1;
window.setInterval(function() {
    var elem = document.getElementById('chat');
    elem.scrollTop = height;
    if (elem.scrollheight < height) {
        clearInterval(this);
    }
    height += 2;
}, 50);

the clearInterval function expects a number. clearInterval函数需要一个数字。 Using that should make it work correctly. 使用它应该使其正常工作。 You also have many syntax errors. 您也有许多语法错误。

var intervalReference = window.setInterval(function() {
    var elem = document.getElementById('chat');
    elem.scrollTop = height;
    if (elem.scrollHeight < height) {
        clearInterval(intervalReference);
    }
    height += 2;
}, 50);

you should make a var holding the interval like this : 您应该使var保持这样的间隔:

var height = 1;
var interval = window.setInterval( animate, 50 );

function animate() {
    var elem = document.getElementById('chat');
    elem.scrollTop = height;
    if (elem.scrollHeight < height) {
        clearInterval( interval );
    }
    height += 2;
}

this should work fine 这应该工作正常

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

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