jQuery scrollTop not working in Chrome but working in Firefox

后端 未结 15 1782
再見小時候
再見小時候 2020-11-29 02:54

I have used a scrollTop function in jQuery for navigating to top, but strangely \'the smooth animated scroll\' stopped working in Safari and Chrome (scrolling w

15条回答
  •  借酒劲吻你
    2020-11-29 03:44

    Scroll body and check if it worked:

    function getScrollableRoot() {
        var body = document.body;
        var prev = body.scrollTop;
        body.scrollTop++;
        if (body.scrollTop === prev) {
            return document.documentElement;
        } else {
            body.scrollTop--;
            return body;
        }
    }
    
    
    $(getScrollableRoot()).animate({ scrollTop: 0 }, "slow");
    

    This is more efficient than $("html, body").animate because only one animation used, not two. Thus, only one callback fires, not two.

提交回复
热议问题