最近在開發(fā)網(wǎng)站需要制作返回頂部按鈕,但是我主要為后端開發(fā),對前端不太熟練,經(jīng)過網(wǎng)上資料查詢,制作出了返回頂部的按鈕,下面是兩種簡單的方式,記錄一下.喜歡本網(wǎng)站的朋友可以收藏下,會(huì)不定期更新學(xué)習(xí)資料.
第一種:引用外部jQuery
新建HTML頁面,將下面代碼復(fù)制保存,通過瀏覽器打開,即可看到效果.
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>doc</title> <style> .arrow{ border: 9px solid transparent; border-bottom-color: #3DA0DB; width: 0px; height: 0px; top:0px } .stick{ width: 8px; height: 14px; border-radius: 1px; background-color: #3DA0DB; top:15px; } #back_top div{ position: absolute; margin: auto; right: 0px; left: 0px; } #back_top{ background-color: #dddddd; height: 38px; width: 38px; border-radius: 3px; display: block; cursor: pointer; position: fixed; right: 50px; bottom: 100px; display: none; } </style> </head> <body> <div id="article"></div> <div id="back_top"> <div class="arrow"></div> <div class="stick"></div> </div> <script src="http://cdn.staticfile.org/jquery/1.11.1-rc2/jquery.min.js"></script> <script> $(function(){ for(var i =0 ;i <100;i++){ $("#article").append("<p>xxxxxxxxxx<br></p>") } }) </script> <script> $(function(){ $(window).scroll(function(){ //只要窗口滾動(dòng),就觸發(fā)下面代碼 var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //獲取滾動(dòng)后的高度 if( scrollt >200 ){ //判斷滾動(dòng)后高度超過200px,就顯示 $("#back_top").fadeIn(400); //淡入 }else{ $("#back_top").stop().fadeOut(400); //如果返回或者沒有超過,就淡出.必須加上stop()停止之前動(dòng)畫,否則會(huì)出現(xiàn)閃動(dòng) } }); $("#back_top").click(function(){ //當(dāng)點(diǎn)擊標(biāo)簽的時(shí)候,使用animate在200毫秒的時(shí)間內(nèi),滾到頂部 $("html,body").animate({scrollTop:"0px"},200); }); }); </script> </body> </html>
第二種:使用css及特殊圖標(biāo)進(jìn)行設(shè)置
全代碼打造簡潔美觀回到頂部按鈕,同上,將代碼復(fù)制進(jìn)HTML文件中,打開即可看到效果.
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>doc</title> <style> #back_top{ display:block; width:60px; height:60px; position:fixed; bottom:50px; right:40px; border-radius:10px 10px 10px 10px; text-decoration:none; display:none; background-color:#999999; } #back_top span{ display:block; width:60px; color:#dddddd; font-size:40px; text-align:center; margin-top:4px; } #back_top span:hover{ color:#cccccc; } </style> </head> <body> <div id="article"></div> <a id="back_top" href="script:;"> <span>⌆</span> </a> </div> <script> $(function(){ for(var i =0 ;i <100;i++){ $("#article").append("<p>xxxxxxxxxx<br></p>") } }) </script> <script> $(function(){ $(window).scroll(function(){ //只要窗口滾動(dòng),就觸發(fā)下面代碼 var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //獲取滾動(dòng)后的高度 if( scrollt >200 ){ //判斷滾動(dòng)后高度超過200px,就顯示 $("#back_top").fadeIn(400); //淡出 }else{ $("#back_top").stop().fadeOut(400); //如果返回或者沒有超過,就淡入.必須加上stop()停止之前動(dòng)畫,否則會(huì)出現(xiàn)閃動(dòng) } }); $("#back_top").click(function(){ //當(dāng)點(diǎn)擊標(biāo)簽的時(shí)候,使用animate在200毫秒的時(shí)間內(nèi),滾到頂部 $("html,body").animate({scrollTop:"0px"},200); }); }); </script> </body> </html>
以上兩種方式僅提供思路,直接使用也可,具體想要的圖標(biāo)可自己調(diào)試,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:吳忠 上海 山南 清遠(yuǎn) 內(nèi)蒙古 陜西 保定 廣安
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTMl頁面中返回頂部的幾種實(shí)現(xiàn)小結(jié)》,本文關(guān)鍵詞 HTMl,頁面,中,返回,頂部,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。