jQuery scrollTop() does not work in scrolling DIV on mobile browsers, alternatives?

前端 未结 13 972
傲寒
傲寒 2020-12-08 19:52

I am trying to scroll to a specific location in a scrolling DIV. Right now I am using a pixel offset with the jQuery scrollTop() function which works great on desktop brows

13条回答
  •  一整个雨季
    2020-12-08 20:19

    I found the answer here http://blog.jonathanargentiero.com/jquery-scrolltop-not-working-on-mobile-devices-iphone-ipad-android-phones/

    Mobile phones doesn't understand $('html,body') so u can do the following for mobile

    if(navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {           
        window.scrollTo(0)
    } else {
        // default `$('html,body')` code for scrolling
    }
    

    OR

    simply use $('body') instead of $('html, body').

提交回复
热议问题