How to prevent document scrolling but allow scrolling inside div elements on websites for iOS and Android?

后端 未结 11 1540
南笙
南笙 2020-12-04 10:43

I created a website with jQueryMobile for iOS and Android.

I don\'t want the document itself to scroll. Instead, just an area (a

element) sh
11条回答
  •  醉话见心
    2020-12-04 11:14

    Here is a solution that uses jQuery for the events.

    var stuff = {};
    $('#scroller').on('touchstart',stuff,function(e){
      e.data.max = this.scrollHeight - this.offsetHeight;
      e.data.y = e.originalEvent.pageY;
    }).on('touchmove',stuff,function(e){
      var dy = e.data.y - e.originalEvent.pageY;
      // if scrolling up and at the top, or down and at the bottom
      if((dy < 0 && this.scrollTop < 1)||(dy > 0 && this.scrollTop >= e.data.max)){
        e.preventDefault();
      };
    });
    

提交回复
热议问题