The same old issue, .scrollTop(0) not working in Chrome & Safari

梦想与她 提交于 2019-11-27 15:12:38
cheeken

The problem is with CSS. In particular, the rules I've included below.

html, body {
    overflow-x: hidden;
    overflow-y: auto;
}

Though these rules are obviously related to scrollbars, I'm not sure why they are causing the behavior you are observing. But if you remove them, it should solve your problem.

See: http://jsfiddle.net/jNWUm/23/.

I had the same problem. If I put

$(window).scrollTop(0);

in the document ready handler, it didn't work; but if I test it in the javascript console panel of Chrome developer toolkit, it worked! The only difference was the execution time of the script. So I tried

window.setTimeout(function() {
    $(window).scrollTop(0); 
}, 0);

and it worked. Setting a delay greater than 0 is not neccesary, although that also worked. It's not jQuery's fault, because it is the same with

window.scrollTo(0, 0);  window.scroll(0, 0);

So the reason might be the browser populates the window.pageYOffset property after it renders the page and executes the js.

For people that need the overflow settings, the workaround is to use an animation like this.

    $('#element_id').stop().animate({ scrollTop: 0 }, 500);

This seems to behave better than .scrollTop(0).

I just tested the following in IE8 and Chrome and both work:

$(window).scrollTop(0)
zCoder

Also check out this answer: scrolltop broken on android - it seems to work best when overflow is set to visible and position to relative - but ymmv. You might find these useful...

function repeatMeOnMouseDown() // for smooth scrolling
{
    var $newTop = $('#element_id').position().top + ($goingUp ? -20 : 20);
    $('#element_id').animate({top: $newTop}, 20, repeatMeOnMouseDown); 
}

// these should work
$('#element_id').animate({top: -200}, 200); // scroll down
$('#element_id').animate({top: 0}, 200); // scroll back up

// DON'T DO THIS - Offsets don't work with top like they do with scrollTop 
$('#element_id').animate({top: ("-= " + 200)}, 200);

// and when you get tired of fighting to do smooth animation 
// on different browsers (some buggy!), just use css and move on
function singleClick($goingUp)
{
   var $newTop = $('#element_id').position().top + ($goingUp ? -200 : 200);
    $('#element_id').css({top: $newTop}, 20); 
}

FYI, if not in top body, i.e. in an iframe, just try window.top.scrollTo(0,0);

RAM

I had a same problem with scrolling in chrome. Reason was height:100% style in body and html. See this answer

This code was tested in chrome. http://jsfiddle.net/wmo3o5m8/1/

(function () {
    var down = false, downX, downY;
    document.onmousedown = function (e) {
        downX = e.pageX;
        downY = e.pageY;
        down = true;
    };
    document.onmouseup = function () { down = false; };
    document.onmousemove = function (e) {
        if (down) {
            window.scrollTo(
                window.scrollX + downX - e.pageX,
                window.scrollY + downY - e.pageY
            );
        }
    };
})();

This is the normal behaviour when in your css you are declaring overflow: hidden for html or body dom elements, as you can read in the jQuery API docs:

The vertical scroll position is the same as the number of pixels that are hidden from view above the scrollable area. If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.

With overflow: hidden the scroll bar is not visible hence element not scrollable, so the number will be 0 in every (I wonder) browser you will use.

$('#element-id').prop('scrollTop', 0); should also do the trick.

I had this issue on MacOS. I tried to put:

$(window).scrollTop(0);
document.body.scrollTop = document.documentElement.scrollTop = 0;

in the $(window).load and it still wasn't working.

Then I tried to use some trick:

$('body')
  .css('position', 'fixed')
  .delay(200)
  .promise()
  .done(function() {
    $('body').css('position', 'relative');
  });

and it worked fine! The position fixed kind of detaches the <body> from the <html> which effectively sets the window scroll position to zero. Then with a delay of 200 milliseconds (just to be on the safer side), I put the position: relative back.

I hope this helps. Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!