简体   繁体   中英

Scroll Top = Scroll Height not working in Chrome

This is the js that I have in my js file

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);


});

it works fine in Firefox and IE, but with Google Chrome it constantly flicks. It will scroll to the bottom on page load, but when it calls to the function refreshChat it moves back up about halfway up the div.

I also have this in my <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);
            }
        }   
        })
        });

so that it will auto load the shoutbox on page load, could this be conflicting with it? It seems logical, but I don't want users to have to wait 3 seconds for the shoutbox to initially load.

You need to convert string to int.

scrollHeight is custom attribute and i guess its dynamically added so it must string thats why you need to cast it to int.

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

try this, hope this will solve it.

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