Div scrolling freezes sometimes if I use -webkit-overflow-scrolling

23,350

Solution 1

For me, the freezing was repeatable and happened when trying to scroll up or down when already at the top or bottom, respectively. The fix was to add some listeners for touchstart and touchmove and detect these cases and event.preventDefault() on ’em.

Something like the following, where .scroller is the div that will actually scroll (changes to scrollTop).

var lastY = 0;
var targetElt = document.querySelector(".scroller");

targetElt.addEventListener('touchstart', function(event) {
    lastY = event.touches[0].clientY;
});

targetElt.addEventListener('touchmove', function(event) {
    var top = event.touches[0].clientY;

    var scrollTop = event.currentTarget.scrollTop;
    var maxScrollTop = event.currentTarget.scrollHeight -
        $(event.currentTarget).outerHeight();
    var direction = lastY - top < 0 ? 'up' : 'down';

    if (
        event.cancelable && (
            (scrollTop <= 0 && direction === 'up') ||
            (scrollTop >= maxScrollTop && direction === 'down')
        )
    )
      event.preventDefault();

    lastY = top;
});

I hope this helps the next poor soul that encounters this horrible bug! Good luck and keep fighting!

Solution 2

Try using overflow: hidden on body. This should resolve the issue: https://codepen.io/cppleon/pen/vYOgKzX

HTML

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  </head>
  <body>
    <div id="scrollable-content">
      <div class="site-header"></div>
      <div class="main-content"></div>
    </div>
  </body>
</html>

CSS

body {
  /* magic is here */
  overflow: hidden;
}

#scrollable-content {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 50%;
  background-color: gray;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.site-header {
  width: 100%;
  height: 120px;
  background-color: orange;
}

.main-content {
  height: 200%;
}

Solution 3

Stable solution

After many days to try to fix it, i saw that the problem comes from fixed body element, maybe because you don't want your users to see your page body bounce when the scroll is blocked: cf this example. When the body is fixed and you're experiencing scrolling freeze bug, if you inspect the body with Desktop Safari on you iOS device, you can see it's kind of "artificially" moving... yes webkit stuff...

I tried all solutions listed on this threat but also on github similar issues. No one was working.

The only stable fix for me is to use this package : body-scroll-lock and remove the fixed on your body element. Right now you can both enjoy fixed body and no scrolling freezing bugs.

Hope it will help people who are currently creating progressive web apps on IOS.

Solution 4

I used the below code I think is working.

var scrollTimer;
$('.scroller').on('scroll',function(e){
      clearTimeout(scrollTimer);
      scrollTimer = setTimeout(() => {
        this.scrollTop = Math.max(1, Math.min(this.scrollTop, this.scrollHeight - this.clientHeight - 1));
      }, 300);
});

Solution 5

I got the same problem. But that solved easily. This is what i have done: deleted height property of div that was scroll-able. Maybe you are not in the same situation as me and this will not work for you.

Share:
23,350
Adem
Author by

Adem

developer c c++ Java Javascript Java EE Android Objective-C Swift iOS Cross Mobile Development Game Development vxml Voice Platforms cocos2d-x chatbot Node.js

Updated on September 03, 2021

Comments

  • Adem
    Adem over 2 years

    if I use -webkit-overflow-scrolling for a scrolling div, it scrolls perfectly with native momentum. But, div itself sometimes freezes and does not respond my finger moves. After 2-3 seconds later, it becomes again scrollable.

    I don't know how I am reproducing this problem. But, as I see there are two main behavior creates this situation.

    First, If I wait for a while, for instance, 20 seconds, and touch the div, it does not respond. I wait a couple of seconds, and it becomes working again.

    Second, I touch several times quickly, and then, it becomes freezing, and again, after a couple of seconds later, it starts working again.

    How can I prevent this freezing?

  • Mikhail Vasin
    Mikhail Vasin over 5 years
    This is the workaround that finally worked for me! Open in iOS Safari the live example and check out its code. The only downside is the glitchy absence of the bounce effect when already at the top/bottom, but it's acceptable and seems to be the the best option we have until WebKit maintainers fix the bug.
  • whyAto8
    whyAto8 over 5 years
    @wesley , do we know, why this happens, as in this freeze when only at top or bottom.
  • Admin
    Admin over 5 years
    @whyAto8 iOS Safari bug ¯_(ツ)_/¯, probably related to how it "bounces" when at top and bottom. Something about already being there, it probably kicks off the bounce affect and is non-responsive until that finishes but since you're already there it just bugs out.
  • whyAto8
    whyAto8 over 5 years
    @WesleyReitzfeld Okay, got it. Is it kind of logged with them and they dont want to fix it.
  • Admin
    Admin over 5 years
    @whyAto8 I don't know how to log it with them.
  • whyAto8
    whyAto8 over 5 years
    @WesleyReitzfeld No worries. Your solution looked good and worked fine for me. Thanks.
  • Jette
    Jette about 5 years
    How exactly are you using body-scroll-lock to prevent the freezing?
  • Aarbel
    Aarbel about 5 years
    I apply it to every scrolling element
  • Ben Crook
    Ben Crook almost 5 years
    This errors for me - event.currentTarget.outerHeight is not a function. After resolving that it worked a treat thanks. I would edit the answer with the fix but as no one else has mentioned it I presume it worked for them. If anyone else runs into that it's because outerHeight is a jQuery method so you need to fire it on a jQuery element. Change to event.currentTarget.outerHeight() to $(event.currentTarget).outerHeight()
  • Marcin Czenko
    Marcin Czenko almost 5 years
    Wow: this is actually working for me so far. Would be nice to know how did you get to it.
  • Mihey Mik
    Mihey Mik over 4 years
    Please pay attention scrollTop <= 0 not top === 0, sometimes it is -1 and it freezes again :)
  • Admin
    Admin over 4 years
    @MiheyMik ty, fixed!
  • victor.chicu
    victor.chicu almost 4 years
    Scroll freez is constantly reproducible on the iPhone when you do fast scrolling with tapping in the same time at the bottom nor at top and the scroll issue dissapears when scroll goes completely hidden. overflow: hidden in the body solved my problem
  • simlmx
    simlmx over 2 years
    I had a bug with the exact same symptoms on Chrome/Firefox on Mac when scrolling in an <iframe>. Fixed it using tips from this post.
  • tsh
    tsh over 2 years
    Shouldn't outerHeight be changed to clientHeight? outerHeight by jQuery includes margin.
  • Solo
    Solo over 2 years
    For those not using jQuery in 2021.. You can replace el.outerHeight() with el.offsetHeight.