<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>

</head>
<body>
<div id="e" style="width: 100px;height: 1000px;background-color: red;position: relative;left: 0;">

</div>
<button id="but">按钮</button>
<script>

var progress = 0;
let e = document.getElementById('e')
let but = document.getElementById('but')
but.onclick = function () {
//回调函数
let top = document.documentElement.scrollTop
let step = top / 10
function render() {
if(progress === 10) {
progress = 0
}
progress += 1; //修改图像的位置
if (progress < 10) {
//在动画没有结束前,递归渲染
console.log(progress,new Date().toLocaleString())
e.style.left = progress + 'px'
e.style.width = progress *10 +100+ 'px'
document.documentElement.scrollTop = document.documentElement.scrollTop - step
window.requestAnimationFrame(render);
}
}
//第一帧渲染
let id = window.requestAnimationFrame(render)
//cancelAnimationFrame(id) 销毁这个动画函数
}

</script>
</body>
</html>