<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
window.onscroll=function(){
var oDiv=document.getElementById("div1");
scrollTop=document.documentElement.scrollTop;
//div距离页面顶部的距离=运动函数( parseInt 取整((当前网页高度- div 的实际高度)除以2+滑动距离)
oDiv.style.top=motion(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop));
}
var timer=null;
function motion(iTarget){
var oDiv=document.getElementById("div1");
clearInterval(timer);
timer=setInterval(function(){
var spead=0;
spead=(iTarget-oDiv.offsetTop)/2;
spead=spead>0?Math.ceil(spead):Math.floor(spead);
if (iTarget===oDiv.offsetTop) {
clearInterval(timer);
} else {
oDiv.style.top=oDiv.offsetTop+spead+'px';
}
},17)
}
</script>
<style>
*{
margin: 0;
padding: 0;
}
#div1{
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
top: 50%;
}
</style>
<body style="height:10000px">
<div id="div1"></div>
</body>
</html>
网友评论