Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser native scroll function: how to check compatibility

Tags:

javascript

The function scroll in "most" browsers can be used, but it seems that it can be "overloaded". From the compatibility tab, you'll see that some browsers support

element.scroll(scrollToOptions) whereas others only support element.scroll(x, y)

How can I check which method is supported (despite it having the same name) in the current browser?

like image 858
Thierry Prost Avatar asked Mar 03 '23 15:03

Thierry Prost


1 Answers

var isSmoothScrollSupported = 'scrollBehavior' in document.documentElement.style;

var scrollToOptions = {
  top: 100,
  left: 100,
  behavior: 'smooth'
}; 

if (isSmoothScrollSupported) {
    // Native smooth scrolling
    window.scroll(scrollToOptions);
} else {
    // Old way scrolling without effects
    window.scroll(scrollToOptions.left, scrollToOptions.top);
}
like image 126
Maksym Petrenko Avatar answered May 12 '23 00:05

Maksym Petrenko