<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>shangxia</title> <style> *{margin: 0;padding: 0;} #div1 {width:100px; height:100px; background:red; position:absolute; right:0;} </style> <script> window.onload = function(){ var oDiv = document.getElementById('div1'); var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; var t=scrollTop+(document.documentElement.clientHeight-oDiv.offsetHeight)/2; var timer = 0; starMove (100) function starMove (target){ clearInterval(timer) timer = setInterval(function(){ var speed=(target-oDiv.offsetTop)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed); if (oDiv.offsetTop==target) { clearInterval(timer) }else{ oDiv.style.top=oDiv.offsetTop+speed+'px'; }; txt1.value=oDiv.offsetTop+',目标:'+target; },30) } } </script> </head> <body style="height:2000px;"> <input id="txt1" type="text" style="position:fixed; top:20px;" /> <div id="div1"></div> </body> </html>
下来的时候Div不会跟随滚动条移动,问题出在哪里,而且怎么样让下拉默认位置Div居中。