How to enable smooth scrolling on mobile iOs?

Viewed 9398

How can i enable smooth scroll on mobile ios, webkit-overflow-scroll is not working

5 Answers

Unfortunately Smooth Scrolling is not possible in Safari on OSX and iOS natively. But there are some JavaScript polyfills for that. For example: https://github.com/iamdustan/smoothscroll You have to initialize it with JavaScript, not with CSS.

Using jQuery you can do like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

JS

$(document).ready(function(){
  // Add smooth scrolling to all links with the class scroll
  $("a.scroll").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

HTML

<a class="scroll" href="#">Text</a>

So i solved this problem with smoothscroll-polyfill, this is enable to scroll my page smoothly on mobile ios

For anyone who wants to customize/enhance the smooth-scrolling with support for basically any modern browser (Safari/Safari for iOS included) you can take a look at this API I wrote.
Compared to the standard polyfill it allows to use custom ease functions, smooth-scrolling multiple elements at the same time, interrupting the scrolling-animations and more on both the x and y axises.
You can find more about it here.

Use the next CDN for a pollyfill.

<script src="https://cdnjs.cloudflare.com/ajax/libs/iamdustan-smoothscroll/0.4.0/smoothscroll.min.js"></script>
Related