/**
*
* @param {*} currentY 起始位置
* @param {*} targetY 目标位置
*/
export function scrollAnimation(currentY, targetY) {
// 计算需要移动的距离
let needScrollTop = targetY - currentY
let _currentY = currentY
setTimeout(() => {
// 一次调用滑动帧数,每次调用会不一样
const dist = Math.ceil(needScrollTop / 10)
_currentY += dist
window.scrollTo(_currentY, currentY)
// 如果移动幅度小于十个像素,直接移动,否则递归调用,实现动画效果
if (needScrollTop > 10 || needScrollTop < -10) {
scrollAnimation(_currentY, targetY)
} else {
window.scrollTo(_currentY, targetY)
}
}, 1)
};
原生js,页面body滚动到指定位置
猜你喜欢
转载自blog.csdn.net/pinhmin/article/details/130880479
今日推荐
周排行