Prevent “overscrolling” of web page

前端 未结 8 2368
星月不相逢
星月不相逢 2020-11-28 01:23

In Chrome for Mac, one can \"overscroll\" a page (for lack of a better word), as shown in the screenshot below, to see \"what\'s behind\", similar to the iPad or iPhone.

相关标签:
8条回答
  • 2020-11-28 02:13

    In Chrome 63+, Firefox 59+ and Opera 50+ you can do this in CSS:

    body {
      overscroll-behavior-y: none;
    }
    

    This disables the rubberbanding effect on iOS shown in the screenshot of the question. It however also disables pull-to-refresh, glow effects and scroll chaining.

    You can however elect to implement your own effect or functionality upon over-scrolling. If you for instance want to blur the page and add a neat animation:

    <style>
      body.refreshing #inbox {
        filter: blur(1px);
        touch-action: none; /* prevent scrolling */
      }
      body.refreshing .refresher {
        transform: translate3d(0,150%,0) scale(1);
        z-index: 1;
      }
      .refresher {
        --refresh-width: 55px;
        pointer-events: none;
        width: var(--refresh-width);
        height: var(--refresh-width);
        border-radius: 50%; 
        position: absolute;
        transition: all 300ms cubic-bezier(0,0,0.2,1);
        will-change: transform, opacity;
        ...
      }
    </style>
    
    <div class="refresher">
      <div class="loading-bar"></div>
      <div class="loading-bar"></div>
      <div class="loading-bar"></div>
      <div class="loading-bar"></div>
    </div>
    
    <section id="inbox"><!-- msgs --></section>
    
    <script>
      let _startY;
      const inbox = document.querySelector('#inbox');
    
      inbox.addEventListener('touchstart', e => {
        _startY = e.touches[0].pageY;
      }, {passive: true});
    
      inbox.addEventListener('touchmove', e => {
        const y = e.touches[0].pageY;
        // Activate custom pull-to-refresh effects when at the top of the container
        // and user is scrolling up.
        if (document.scrollingElement.scrollTop === 0 && y > _startY &&
            !document.body.classList.contains('refreshing')) {
          // refresh inbox.
        }
      }, {passive: true});
    </script>
    

    Browser Support

    As of this writing Chrome 63+, Firefox 59+ and Opera 50+ support it. Edge publically supported it while Safari is an unknown. Track progress here and current browser compatibility at MDN documentation

    More information

    • Chrome 63 release video
    • Chrome 63 release post - contains links and details to everything I wrote above.
    • overscroll-behavior CSS spec
    • MDN documentation
    0 讨论(0)
  • 2020-11-28 02:15

    position: absolute works for me. I've tested on Chrome 50.0.2661.75 (64-bit) and OSX.

    body {
      overflow: hidden;
    }
    
    // position is important
    #element {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      overflow: auto;
    }
    
    0 讨论(0)
提交回复
热议问题