How to disable rubber band in iOS web apps?

前端 未结 8 1591
夕颜
夕颜 2020-12-01 00:50

This:

$(\'body\').on(\'touchmove\', function(e) { e.preventDefault(); });

Works, but will disable scrolling throughout the whole page, whic

相关标签:
8条回答
  • 2020-12-01 01:49

    I wrote, in my opinion, the best solution for this problem. It will disable scrolling in general unless the element has y scrolling.

    /********************************************************************************
     * Disable rubber band (c)2013 - Mark van Wijnen | www.CrystalMinds.nl
     ********************************************************************************/
    $(function(){
        var scrollY = 0;
    
        $(document).on('touchstart', function( e ){
            scrollY = e.originalEvent.touches.item(0).clientY;
        });
    
        $(document).on('touchmove', function( e ){
            var scrollPos       = e.target.scrollTop;
            var scrollDelta     = scrollY - e.originalEvent.touches.item(0).clientY;
            var scrollBottom    = scrollPos + $(e.target).height();
            scrollY             = e.originalEvent.touches.item(0).clientY;
    
            if ( $(e.target).css( 'overflow-y' ) != 'scroll' || ( scrollDelta < 0 && scrollPos == 0 ) || ( scrollDelta > 0 && scrollBottom == e.target.scrollHeight ) ) 
                e.preventDefault();
        });
    });
    
    0 讨论(0)
  • 2020-12-01 01:52

    Finally I mixed some methods and these codes are the working version. But you must include the hammer.js

    CSS

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

    JAVASCRIPT

    $(document).on("touchmove",function(e){
        e.preventDefault();
    });
    $("body").on("touchstart",".scrollable",function(e){
        if(e.currentTarget.scrollTop===0){
            e.currentTarget.scrollTop = 1;
        }else if(e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight){
            e.currentTarget.scrollTop-=1;
        }
    });
    $("body").on("touchmove",".scrollable",function(e){
        e.stopPropagation();
    });
    
    $("body").hammer().on("pinch",function(e){
        e.gesture.preventDefault();
    });
    
    0 讨论(0)
提交回复
热议问题