css:
#div{ width:100px; height:200px; background: goldenrod; position: absolute; right:0; } #div span{ width: 25px; height: 70px; position: absolute; right:100px; top:65px; background: rebeccapurple; color:#fff; }html:
<body style="height: 2000px;"> <div id="div"> <span>分享到</span> </div>
<body/>
js:
window.onscroll=function () { var oDiv=document.getElementById("div"); var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; var client=(document.documentElement.clientHeight-oDiv.offsetHeight)/2;
var t=scrollTop+client;startMove(parseInt(t))}//运动框架 var timer=null;//timer写在函数外 function startMove(itarget) { var oDiv=document.getElementById("div"); clearInterval(timer); timer=setInterval(function () { var iSpeed=(itarget-oDiv.offsetTop)/8; iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed) if(oDiv.offsetTop==itarget){ clearInterval(timer) }else{ oDiv.style.top=oDiv.offsetTop+iSpeed+"px"; } },30) }