課程
/前端開(kāi)發(fā)
/HTML/CSS
/回到頂部效果
IE6到8有bug,按鈕要按兩次,不到為啥?誰(shuí)在IE上試了?
2016-03-03
源自:回到頂部效果 4-4
正在回答
或許你自己寫(xiě)的代碼有疏漏,可以參考下面的代碼
html
<!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title>回到頂部</title> <link?rel="stylesheet"?type="text/css"?href="style.css"> <script?type="text/javascript"?src="script.js"></script> </head> <body> <div> <img?src="background.jpg"?/> </div> <a?href="javascript:;"?id="btn"?title="回到頂部"?></a> </body> </html>
js
//頁(yè)面加載后觸發(fā) window.onload?=?function?()?{ var?obtn?=?document.getElementById('btn'); var?timer?=?null; var?isTop?=true; var?clientHeight?=?document.documentElement.clientHeight; window.onscroll?=?function()?{ var?osTop?=?document.documentElement.scrollTop?||?document.body.scrollTop; if(clientHeight?>?osTop){ obtn.style.display?=?'none'; }else{ obtn.style.display?=?'block'; } if(!isTop){ clearInterval(timer); } isTop?=?false; } obtn.onclick?=?function()?{? //設(shè)置定時(shí)器 timer?=?setInterval(function(){ //獲取滾動(dòng)條距離頂部的高度 var?osTop?=?document.documentElement.scrollTop?||?document.body.scrollTop; var?ispeed?=?Math.floor(-osTop?/?5); document.documentElement.scrollTop??=?document.body.scrollTop?=?osTop?+?ispeed; console.log('osTop?-?ispeed') isTop?=?true; if(osTop?==?0){ clearInterval(timer); } },?30); } }
css
.box{ width:?1190px; margin:0?auto; } #btn{ width:?40px; height:?40px; position:?fixed; left:?45%; display:none; margin-left:?600px; bottom:?30px; background:?url(btn.jpg)?no-repeat?left?top?; } ?#btn:hover{ ? background:?url(btn.jpg)?no-repeat?0?-39px; ?}
阿伊舍999 提問(wèn)者
舉報(bào)
回到頂部網(wǎng)站不可缺少的一部分,用JS實(shí)現(xiàn)炫酷拉風(fēng)的回到頂部效果
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢(xún)優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-03-04
或許你自己寫(xiě)的代碼有疏漏,可以參考下面的代碼
html
js
css