还是直接上代码比较实在
html:
<img src ="" id="erwei" >
css:
body{
height:5000px
}
#erwei{
position:absolute;
right:30px;
}
js:
vardiv=document.getElementById('erwei')
window.onscroll=function() {
//兼容
varscrollTop=document.documentElement.scrollTop||document.body.scrollTop;
Move(parseInt((document.documentElement.clientHeight-div.offsetHeight) /2+scrollTop))
}
vartimer
function Move(target) {
clearInterval(timer)
timer=setInterval(function() {
varspeed= (target-div.offsetTop)/4;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(div.offsetTop==target) {
clearInterval(timer)
}else{
div.style.top=div.offsetTop+speed+'px';
}
},30)
}
网友评论