iOS Safari – How to disable overscroll but allow scrollable divs to scroll normally?

后端 未结 14 1580
南方客
南方客 2020-11-28 17:50

I\'m working on an iPad-based web app, and need to prevent overscrolling so that it seems less like a web page. I\'m currently using this to freeze the viewport and disable

14条回答
  •  情深已故
    2020-11-28 18:04

    Using Tyler Dodge's excellent answer kept lagging on my iPad, so I added some throttling code, now it's quite smooth. There is some minimal skipping sometimes while scrolling.

    // Uses document because document will be topmost level in bubbling
    $(document).on('touchmove',function(e){
      e.preventDefault();
    });
    
    var scrolling = false;
    
    // Uses body because jquery on events are called off of the element they are
    // added to, so bubbling would not work if we used document instead.
    $('body').on('touchstart','.scrollable',function(e) {
    
        // Only execute the below code once at a time
        if (!scrolling) {
            scrolling = true;   
            if (e.currentTarget.scrollTop === 0) {
              e.currentTarget.scrollTop = 1;
            } else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
              e.currentTarget.scrollTop -= 1;
            }
            scrolling = false;
        }
    });
    
    // Prevents preventDefault from being called on document if it sees a scrollable div
    $('body').on('touchmove','.scrollable',function(e) {
      e.stopPropagation();
    });
    

    Also, adding the following CSS fixes some rendering glitches (source):

    .scrollable {
        overflow: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }
    .scrollable * {
        -webkit-transform: translate3d(0,0,0);
    }
    

提交回复
热议问题