简体   繁体   English

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

[英]Browser native scroll function: how to check compatibility

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) element.scroll(scrollToOptions)而其他的只支持element.scroll(x, y)

How can I check which method is supported (despite it having the same name) in the current browser?如何检查当前浏览器支持哪种方法(尽管它具有相同的名称)?

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM