一、页面整体没有滚动条时:
可以使用锚点链接,不过多追赘述;
二、页面本身有滚动条,操作局部滚动条,而不滑动整体页面:
1、用scrollTo:
"lianxiangText"是有滚动条,需要滚动条定位的盒子id;
targetId是目标文字所在标签的id;
const boxElement = document.getElementById("lianxiangText");
const targetElement = document.getElementById("targetId");
const targetRect = targetElement.getBoundingClientRect();
boxElement.scrollTo({
top: targetRect.top,
behavior: 'smooth'
});
2、scrollIntoView()
const dom = document.getElementById("targetId");
dom.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
还有其他方法,先总结到这里,也欢迎大家评论区补充~