jQuery scrollTop not working in Chrome but working in Firefox

后端 未结 15 1818
再見小時候
再見小時候 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:49

    // if we are not already in top then see if browser needs html or body as selector
    var obj = $('html').scrollTop() !== 0 ? 'html' : 'body';
    
    // then proper delegate using on, with following line:
    $(obj).animate({ scrollTop: 0 }, "slow");
    

    BUT, best approach is to scroll an id into your viewport using just native api (since you scroll to top anyway this can be just your outer div):

    document.getElementById('wrapperIDhere').scrollIntoView(true);
    

提交回复
热议问题