a链接锚点定位太生硬?试试自己让滚动条平滑滚动把
scroll2 =(target)=>{
console.log('alb')
console.log('滚动拉')
target = target?target:'aaa'; //className
const scrollPart = document.querySelector('.' + target); // 目标节点class
const top = scrollPart.getBoundingClientRect().top - 60;//设置自己滚动的高度
const pageY = window.pageYOffset;
const endPosition = top + pageY;
const startTime = +new Date();
const duration = 500; //ms
function run() {
const time = +new Date() - startTime;
window.scrollTo(0, pageY + top * (time / duration));
run.timer = requestAnimationFrame(run);
if (time >= duration) {
window.scrollTo(0, endPosition);
cancelAnimationFrame(run.timer);
}
}
requestAnimationFrame(run);
}
//调用
scroll2('classNameA');