window.onscroll=function(){ var oDiv=document.getElementById('div1'); var scrollTop=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; //oDiv.style.top=document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop+'px'; startMove(document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop); }; var timer=null; function startMove(iTarget){ var oDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function(){ var speed=(iTarget-oDiv.offsetTop)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(iTarget==oDiv.offsetTop) {clearInterval(timer);}else{ oDiv.style.top=oDiv.offsetTop+speed+'px' }},30); };
是不是因为window.onscroll的兼容性问题?该如何解决?请大神帮忙指点指点
可能是clientHeight的跨浏览器兼容性问题,参考JavaScript Window - 浏览器对象模型
我做了个在线演示,你可以点击查看是否在不同的浏览器上都实现你的需求。我在IE11, Chrome 34和Ubuntu的FirFox34都测试过可以。
http://www.phpernote.com/javascript-function/510.html
http://blog.sina.com.cn/s/blog_978037c60101151t.html
http://blog.csdn.net/kalision/article/details/7066741