0

这是我的 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 中运行良好,但在 Google 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);
            }
        }   
        })
        });

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

4

1 回答 1

1

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

scrollHeight是自定义属性,我猜它是动态添加的,所以它必须是字符串,这就是你需要将它转换为 int 的原因。

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

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

于 2013-01-17T04:24:01.807 回答