滚动方式有2种
// 假设你有一个元素的ID是'element-id' const element = document.getElementById('element-id'); // 滚动到该元素 element.scrollIntoView(); // 如果你想要平滑滚动,可以传递一个选项对象 element.scrollIntoView({ behavior: 'smooth' }); PS:这种方式有2种已知情况下会不生效
scrollIntoView()的元素的父容器必须设置滚动,例如:overflow-y: auto;,请注意,这里说的是父容器,是不包含祖先容器的!!!scrollIntoView()和requestAnimationFrame()目前不能同时存在// 假设你有一个元素的ID是'element-id' const element = document.getElementById('element-id'); // 滚动到该元素 element.scrollTo(0, 100); // x,y坐标 // 如果你想要平滑滚动,可以传递一个选项对象 element.scrollTo({ top: offsetTop, behavior: 'smooth' });