<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box{width: 100px;height: 100px;background-color: red;position: absolute;}
#box1{width: 100%;height: 1000px;}
</style>
<script>
window.onload = function(){
Change(300);
var BOX = document.getElementById("box")
window.onscroll = function(){
Change(300);
}
}
var timer = null;
Change = function(goal){
var BOX = document.getElementById("box");
clearInterval(timer);
timer = setInterval(function(){
var speed = (goal - BOX.offsetTop) / 8;
BOX.style.top = BOX.offsetTop + speed +"px";
}
,30)
}
</script>
</head>
<body>
<div id="box1">
<div id="box"></div>
</div>
</body>
</html>
我已经加了
window.onscroll = function(){
Change(300);
}
为什么滚动的时候box它不跟随着滚动位置自动缓冲到top = 30px的位置呢?
我把window改成box1或者box也不行(当然都是先get到他们的id值)