大家在瀏覽網(wǎng)站時(shí)有沒有發(fā)現(xiàn),往下滑動(dòng)頁面會(huì)出現(xiàn)一個(gè)返回頂部的按鈕,點(diǎn)擊就能返回頂部,那你知道html頁面返回頂部怎么實(shí)現(xiàn)的嗎?這篇文章就給大家分享最簡單的html頁面返回頂部的jquery代碼,感興趣的朋友可以參考一下。
實(shí)現(xiàn)頁面返回頂部的效果需要用到很多JavaScript知識(shí),比如:function(),animate,fadeOut等等,如有不清楚的小伙伴可以參考PHP中文網(wǎng)的相關(guān)文章,或者訪問 JavaScript視頻教程。
實(shí)例描述:用戶往下滑動(dòng)頁面,當(dāng)滾動(dòng)條距離頂部的距離大于100px時(shí),用fadeIn顯示返回按鈕,當(dāng)點(diǎn)擊按鈕返回頂部后,按鈕消失,具體代碼如下:
HTML部分:
<div id="wrapper"> <div class="cont1"></div> <div class="cont2"></div> <div class="cont3"></div> <div class="cont4"></div> <a href="javascript:void(0)" id="toTop" style="display: block;"> </a> </div>
CSS部分:
*{margin: 0;padding: 0;} #wrapper{margin: 0 auto;width: 500px;} .cont1{height: 500px;background-color: wheat;} .cont2{height: 500px;background-color: honeydew;} .cont3{height: 500px;background-color: blueviolet;} .cont4{height: 500px;background-color: pink;} #toTop {display: none;text-decoration: none;position: fixed;bottom: 20px;right: 20px;overflow: hidden;width: 50px;height: 50px;background: url(img/icon_top.png) no-repeat center center;}
JavaScript部分:
注:因?yàn)橛玫氖莏Query寫的,要記得引入jQuery文件啊
$(function(){ //當(dāng)滾動(dòng)條的位置處于距頂部100像素以下時(shí),跳轉(zhuǎn)鏈接出現(xiàn),否則消失 $(function () { $(window).scroll(function(){ if ($(window).scrollTop()>100){ $("#toTop").fadeIn(1000); } else { $("#toTop").fadeOut(1000); } }); //當(dāng)點(diǎn)擊跳轉(zhuǎn)鏈接后,回到頁面頂部位置 $("#toTop").click(function(){ $('body,html').animate({scrollTop:0},1000); return false; }); }); });
效果圖:
以上給大家分享了如何用jQuery實(shí)現(xiàn)HTML頁面返回頂部的代碼,步驟很詳細(xì),簡單易懂,初學(xué)者可以自己動(dòng)手嘗試,看看你的代碼能不能實(shí)現(xiàn)回到頂部的效果,希望這篇文章對(duì)你有所幫助!
【相關(guān)教程推薦】
1. jQuery中文參考手冊(cè)
2. jQuery視頻教程?
3. bootstrap教程?
想要下載回到頂部效果代碼,可訪問:回到頂部代碼 欄目!
以上就是如何用jQuery實(shí)現(xiàn)頁面返回頂部的效果(附代碼)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)