我使用window.scrollBy()进行平滑滚动,但不适用于Safari

时间:2019-02-05 08:52:51

标签: javascript scroll

我使用window.scrollBy()进行平滑滚动,但不适用于Safari。修复它的最佳方法是什么?

function scroll(id: number) {
  for (let k = 0; k < sectionsForScroll.length; k++) {
    if (sectionsForScroll[k].dataset.navId == id) {
      const bigginer = sectionsForScroll[0].getBoundingClientRect().top;
      console.log(bigginer + 'px  how far we from the start ');

      const distanceToGo = sectionsForScroll[k].getBoundingClientRect().top;
      console.log(sectionsForScroll[k].offsetTop);

      const distanceToScroll = bigginer - distanceToGo;
      console.log(distanceToGo + ' where we have to go ');
      console.log(distanceToScroll + ' what the distanse we need to scroll ');

      window.scrollBy({left:0, top:distanceToGo, behavior:"smooth"});
    }
  }
}

0 个答案:

没有答案