為什么我的鼠標(biāo)點(diǎn)擊事件不能成功?
<script language="javascript">
window.onload=function(){
?var btn=document.getElementById('one');
?var timer=null;//創(chuàng)建定時(shí)器;
?var itop=true;
?
?//滾動(dòng)條滾動(dòng)時(shí)觸發(fā)事件
?window.onscroll=function(){
? if(!itop)
? {
? ? clearInterval(timer); ?//滾動(dòng)時(shí)如果點(diǎn)擊畫面,滾動(dòng)會(huì)停止;
? }
? itop=false;
?
?}
?
?btn.onclick=function(){
? ?
//設(shè)置定時(shí)器
timer=setInterval(function(){
? ?var top=document.documentElement.scrollTop||document.body.scrollTop;//獲取滾動(dòng)條距離頂部的數(shù)值,||解決兼容性問題;
//滾動(dòng)條依次往上滾動(dòng);
var ispeed=Math.floor(-top/6);//用數(shù)學(xué)函數(shù)將小數(shù)部分舍去;
document.documentElement.scrollTop=document.body.scrollTop=top+ispeed;
? ?itop=true;
//如果回到頂部就關(guān)閉定時(shí)器
if(top==0)
{
clearInterval(timer);
}
},30);//滾動(dòng)時(shí)間
?}
}
</script>
<style type="text/css">
#content{
width:1000px;
height:5000px;
border:black 1px solid;
margin:auto;
}
#one{
?border:black 1px solid;
?background:url("123.jpg") no-repeat left top;
?width:40px;
?height:40px;
?position:fixed;
?left:50%;
?bottom:30px;
?margin-left:510px;
}
#one:hover{
?background:url("123.jpg") no-repeat left -40px;
}
</style>
</head>
<body>
<div id="content">
</div>
<a href="javascript:;" id="one" title="回到頂部"></a>
</body>
</html>
2016-07-21
注意瀏覽器的兼容問題
2016-01-18
在谷歌下是可以的