Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - window.scroll({ behavior: 'smooth' }) not working in Safari

As the title says, it works perfectly fine on Chrome. But in Safari, it just sets the page to the desired top and and left position. Is this the expected behaviour? Is there a way to make it work nicely?

like image 433
Sorakthun Ly Avatar asked Jul 08 '18 07:07

Sorakthun Ly


People also ask

Does scroll-behavior smooth work on Safari?

With the new version of Safari 15.4, the CSS property scroll-behavior: smooth will block the JS scroll on element. scrollTop = xx . The release note says scroll-behavior props will be supported by this new version, but it only blocks scrolls...

How do I make Safari scroll smoother?

Using jquery there is a equivalent for scroll-behavior: smooth. Try this: $('html, body'). animate({ scrollTop: 0 }, 1500);

What is scroll-behavior Smooth?

The scroll-behavior property specifies whether to smoothly animate the scroll position, instead of a straight jump, when the user clicks on a link within a scrollable box. Default value: auto.


1 Answers

Use smootscroll polyfill (solution for all browsers), easy applicable and lightweight dependency: https://github.com/iamdustan/smoothscroll

Once you install it via npm or yarn, add it to your main .js, .ts file (one which executes first)

import smoothscroll from 'smoothscroll-polyfill'; // or if linting/typescript complains import * as smoothscroll from 'smoothscroll-polyfill';  // kick off the polyfill! smoothscroll.polyfill(); 
like image 84
Eugen Sunic Avatar answered Sep 26 '22 20:09

Eugen Sunic