window.scrollTo with options not working on Microsoft Edge

前端 未结 7 1802
不知归路
不知归路 2020-12-03 21:07

I have a strange issue which I can only replicate on Microsoft browsers (Edge and IE11 tested).

相关标签:
7条回答
  • 2020-12-03 22:01

    You can detect support for the behavior option in scrollTo using this snippet:

    function testSupportsSmoothScroll () {
      var supports = false
      try {
        var div = document.createElement('div')
        div.scrollTo({
          top: 0,
          get behavior () {
            supports = true
            return 'smooth'
          }
        })
      } catch (err) {}
      return supports
    }
    

    Tested in Chrome, Firefox, Safari, and Edge, and seems to work correctly. If supports is false, you fall back to a polyfill.

    0 讨论(0)
提交回复
热议问题