浏览器原生滚动功能:如何检查兼容性

Thi*_*ost 3 javascript

“大多数”浏览器中的功能滚动都可以使用,但似乎可以“重载”。在兼容性选项卡中,您会看到某些浏览器支持

element.scroll(scrollToOptions) 而其他人只支持 element.scroll(x, y)

如何检查当前浏览器支持哪种方法(尽管它具有相同的名称)?

Mak*_*nko 6

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);
}
Run Code Online (Sandbox Code Playgroud)