let currentTop = window.scrollY // 初始位置 let targetTop = document.querySelector(e.currentTarget.getAttribute('href')).offsetTop - 70 //目标位置 let s = targetTop - currentTop let t = Math.abs((s/100)*300) if(t>500) {t=500} function animate(time) { requestAnimationFrame(animate); TWEEN.update(time); } requestAnimationFrame(animate);
var coords = { x: currentTop }; var tween = new TWEEN.Tween(coords) .to({ x: targetTop}, t) .easing(TWEEN.Easing.Cubic.InOut) .onUpdate(function() { window.scroll(0, coords.x) }) .start(); // window.scroll(0, top) } }