Javascript 如何将Jquery.animate翻译成Vanilla JS

Javascript 如何将Jquery.animate翻译成Vanilla JS,javascript,jquery,Javascript,Jquery,我只使用Jquery代码的一小部分,我想翻译它 $("html,body").animate( { scrollTop: sections[counter].offsetTop, behavior: "smooth", }, 800 ); Window.scrollTo()不起作用 完整代码 let delay = false; let counter = 0; // le

我只使用Jquery代码的一小部分,我想翻译它

  $("html,body").animate(
      {
        scrollTop: sections[counter].offsetTop,
        behavior: "smooth",
      },
      800
    );
Window.scrollTo()
不起作用

完整代码

let delay = false;

let counter = 0;
// let scrollHeight = 0;

const sections = document.querySelectorAll(".scrolling-block");

function scrollToSection(e, directionY, directionX) {
  if (delay) return;
  // console.log(e);
  delay = true;

  if (directionY !== 0) {
    // if scroll by y
    setTimeout(function () {
      delay = false;
    }, 1500);
  } else {
    // if scroll by x
    delay = false;
  }

  if (directionY !== 0 || directionY !== -0) {
    e.preventDefault();
  }
  if (directionY > 0) {
    //scroll down
    if (counter + 1 !== sections.length) {
      // scrollHeight += sections[counter].clientHeight;
      counter++;
    } else {
      scrollHeight = scrollHeight;
      counter = counter;
    }
    $("html,body").animate(
      {
        scrollTop: sections[counter].offsetTop,
        behavior: "smooth",
      },
      800
    );

    // console.log(scrollHeight + "\n", counter);
    return counter;
  } else if (directionY < 0) {
    //scroll up
    if (counter - 1 !== -1) {
      // scrollHeight -= sections[counter - 1].clientHeight;
      counter--;
    } else {
      scrollHeight = scrollHeight;
      counter = counter;
    }

    $("html,body").animate(
      {
        scrollTop: sections[counter].offsetTop,
        behavior: "smooth",
      },
      800
    );

    // console.log(scrollHeight);
    return counter;
  }
}

window.addEventListener(
  "wheel",
  function (e) {
    let directionY = e.deltaY;

    let directionX = e.deltaX;

    let maxY = sections[sections.length - 1].offsetTop;

    if (pageYOffset < maxY - 10) {
      // console.log(scrollHeight, " < ", maxY);
      if (directionY !== 0) {
        e.preventDefault();
      }
      scrollToSection(e, directionY, directionX);
    } else {
      if (pageYOffset < maxY && directionY < 0) {
        if (directionY !== 0) {
          e.preventDefault();
        }
        scrollToSection(e, directionY, directionX);
      }
      // console.log(scrollHeight, " > ", maxY);
    }

    // e.stopPropagation();
  },
  { passive: false }
);
let delay=false;
设计数器=0;
//让滚动高度=0;
const sections=document.queryselectoral(“滚动块”);
函数滚动部分(e、方向Y、方向X){
如果(延迟)返回;
//控制台日志(e);
延迟=真;
如果(方向Y!==0){
//如果按y滚动
setTimeout(函数(){
延迟=错误;
}, 1500);
}否则{
//如果按x滚动
延迟=错误;
}
如果(方向Y!==0 | |方向Y!==-0){
e、 预防默认值();
}
如果(方向Y>0){
//向下滚动
if(计数器+1!==节长度){
//scrollHeight+=节[计数器].clientHeight;
计数器++;
}否则{
滚动高度=滚动高度;
计数器=计数器;
}
$(“html,body”).animate(
{
scrollTop:段[计数器]。偏移,
行为:“平滑”,
},
800
);
//console.log(滚动高度+“\n”,计数器);
返回计数器;
}else if(方向Y<0){
//向上滚动
如果(计数器-1!=-1){
//scrollHeight-=节[counter-1].clientHeight;
计数器--;
}否则{
滚动高度=滚动高度;
计数器=计数器;
}
$(“html,body”).animate(
{
scrollTop:段[计数器]。偏移,
行为:“平滑”,
},
800
);
//console.log(滚动高度);
返回计数器;
}
}
window.addEventListener(
“轮子”,
职能(e){
设方向y=e.deltaY;
设directionX=e.deltaX;
设maxY=sections[sections.length-1].offsetTop;
如果(页面偏移<最大值-10){
//console.log(滚动高度“<”,最大值);
如果(方向Y!==0){
e、 预防默认值();
}
滚动部分(e,方向Y,方向X);
}否则{
如果(页面偏移”,最大值);
}
//e.停止传播();
},
{被动:false}
);

从技术上讲,scroll和scrollTo应该做同样的事情

window.scrollTo({
  top: 100,
  left: 100,
  behavior: 'smooth'
});

请检查此处的文档:
[1]:
[2] :

你能举个例子吗?
window.scroll({
  top: 100,
  left: 100,
  behavior: 'smooth'
});