javascript scroll event for iPhone/iPad?

后端 未结 5 1735
不知归路
不知归路 2020-11-22 15:38

I can\'t seem to capture the scroll event on an iPad. None of these work, what I am doing wrong?

window.onscroll=myFunction;

document.onscroll=myFunction;

         


        
5条回答
  •  面向向阳花
    2020-11-22 16:17

    Sorry for adding another answer to an old post but I usually get a scroll event very well by using this code (it works at least on 6.1)

    element.addEventListener('scroll', function() {
        console.log(this.scrollTop);
    });
    
    // This is the magic, this gives me "live" scroll events
    element.addEventListener('gesturechange', function() {});
    

    And that works for me. Only thing it doesn't do is give a scroll event for the deceleration of the scroll (Once the deceleration is complete you get a final scroll event, do as you will with it.) but if you disable inertia with css by doing this

    -webkit-overflow-scrolling: none;
    

    You don't get inertia on your elements, for the body though you might have to do the classic

    document.addEventListener('touchmove', function(e) {e.preventDefault();}, true);
    

提交回复
热议问题