为什么我的点击后就没法停止呢?
<!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>
<style>
body {
height: 5000px;
background-image: linear-gradient(to bottom, red, blue, green);
}
button {
position: fixed;
bottom: 100px;
right: 100px;
height: 20px;
width: 100px;
cursor: pointer;
}
</style>
</head>
<body>
<button id="btn">返回顶部</button>
<script>
var btn = document.getElementById('btn');
var timer;
btn.onclick = function () {
clearInterval(timer)
timer = setInterval(
function () {
document.documentElement.scrollTop-=200;
if (document.documentElenent.scrollTop <= 0) {
clearInterval(timer);
}
}, 20)
}
</script>
</body>
</html>
正在回答 回答被采纳积分+1
没看懂呢?哪里属性写错了?
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星