滚动顶部=滚动高度不适用于Chrome

时间:2013-01-17 03:56:54

标签: javascript jquery ajax

这是我在js文件中的js

function refreshChat()
    {
        //speed up by selecting the div only once
        var shoutbox = $("#shoutbox");

        //get the height of the scroll (if any)
        var oldScrollH = shoutbox.attr("scrollHeight") - 20;

        //the ajax request
        $.ajax({
        url: 'shoutbox/update.php',
        //disable cache
        cache: false,
        success: function(html) {
            //update the shoutbox
            shoutbox.html(html);
            //get the heigth of the scroll after the update
            var newScrollH = shoutbox.attr("scrollHeight") - 20;
            if(newScrollH > oldScrollH)
            {
                //*move* the scroll down using an animation :)
                shoutbox.animate({scrollTop: newScrollH}, 1);
            }
        }   
        });
    }
    //set the refreshChat function to run every *refreshSeconds*
    setInterval(refreshChat, refreshSeconds);


});

它在Firefox和IE中运行良好,但谷歌Chrome浏览器不断轻弹。它将在页面加载时滚动到底部,但是当它调用函数refreshChat时,它会向上移动到div的一半左右。

我的<head>

也有这个
$(document).ready(function(){
        //speed up by selecting the div only once
        var shoutbox = $("#shoutbox");

        //get the height of the scroll (if any)
        var oldScrollH = shoutbox.attr("scrollHeight");

        //the ajax request
        $.ajax({
        url: 'shoutbox/update.php',
        //disable cache
        cache: false,
        success: function(html) {
            //update the shoutbox
            shoutbox.html(html);
            //get the heigth of the scroll after the update
            var newScrollH = shoutbox.attr("scrollHeight");
            if(newScrollH > oldScrollH)
            {
                //*move* the scroll down using an animation :)
                shoutbox.animate({scrollTop: newScrollH}, 1);
            }
        }   
        })
        });

这样它会在页面加载时自动加载shoutbox,这可能与它有冲突吗?这似乎是合乎逻辑的,但我不希望用户必须等待3秒才能使shoutbox初始加载。

1 个答案:

答案 0 :(得分:1)

您需要将字符串转换为int。

scrollHeight 是自定义属性,我猜它是动态添加的,因此它必须将这就是为什么需要将其强制转换为int。

parseInt(shoutbox.attr("scrollHeight"));

试试这个,希望这会解决它。