jQuery實現(xiàn)回到頂部效果
更新時間:2020年10月19日 11:08:58 作者:willard_cui
這篇文章主要為大家詳細介紹了jQuery實現(xiàn)回到頂部效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了jQuery實現(xiàn)回到頂部效果的具體代碼,供大家參考,具體內容如下
動畫:通過點擊側欄導航,頁面到達相應的位置
jQuery方法:show(), hide(), animate()
動畫效果:

代碼:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>回到頂部</title>
<script src="D:\jQuery/jquery-3.3.1.js"></script>
<style>
body, div, ul, li{
margin: 0;
padding: 0;
list-style: none;
}
#container{
margin: 10px;
}
#header{
width: 100%;
height:200px;
border: 2px solid #000;
}
#contant ul li{
width: 100%;
height:600px;
border: 2px solid #000;
}
#footer{
width: 100%;
height:200px;
border: 2px solid #000;
}
#scroll{
position: fixed;
right: 50px;
top: 300px;
width: 80px;
background: orange;
opacity: 0.5
}
#scroll ul{
list-style:none;
}
#scroll ul li{
width: 100%;
height: 45px;
line-height:45px;
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<div id="header">頭部</div>
<div id="contant">
<ul>
<li>圖書</li>
<li>服裝</li>
<li>電子</li>
<li>寵物</li>
</ul>
</div>
<div id="footer">底部</div>
<div id="scroll">
<ul>
<li>圖書</li>
<li>服裝</li>
<li>電子</li>
<li>寵物</li>
<li>回到頂部</li>
</ul>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
//當鼠標進入側邊導航欄時改變側欄樣式
$("#scroll").mouseenter(function(){
$(this).css( "opacity",1 );
});
$("#scroll").mouseleave(function(){
$(this).css("opacity",0.5);
})
$("#scroll ul li").mouseover(function(){
$(this).css( {
"color":"red",
"cursor":"pointer"
});
});
$("#scroll ul li").mouseout(function(){
$(this).css("color","black");
})
//點擊側欄導航,頁面到達相應位置
$("#scroll ul li").click(function () {
switch($(this).index()){
case 4:
// $(window).scrollTop(0);
$(document.body).animate({"scrollTop":0},1000);
$(document.documentElement).animate({"scrollTop":0},1000);
break;
case 0:
$(document.body).animate({"scrollTop":200},1000);
$(document.documentElement).animate({"scrollTop":200},1000);
break;
case 1:
$(document.body).animate({"scrollTop":800},1000);
$(document.documentElement).animate({"scrollTop":800},1000);
break;
case 2:
$(document.body).animate({"scrollTop":1400},1000);
$(document.documentElement).animate({"scrollTop":1400},1000);
break;
case 3:
$(document.body).animate({"scrollTop":2000},1000);
$(document.documentElement).animate({"scrollTop":2000},1000);
break;
default:
break;
}
});
});
</script>
</body>
<html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
jQuery插件實現(xiàn)控制網(wǎng)頁元素動態(tài)居中顯示
這篇文章主要介紹了jQuery插件實現(xiàn)控制網(wǎng)頁元素動態(tài)居中顯示,實例分析了jQuery插件的實現(xiàn)與元素動態(tài)顯示的技巧,需要的朋友可以參考下2015-03-03
jQuery 1.9.1源碼分析系列(十)事件系統(tǒng)之主動觸發(fā)事件和模擬冒泡處理
這篇文章主要介紹了jQuery 1.9.1源碼分析系列(十)事件系統(tǒng)之主動觸發(fā)事件和模擬冒泡處理的相關資料,需要的朋友可以參考下2015-11-11
jQuery利用cookie 實現(xiàn)本地收藏功能(不重復無需多次命名)
cookie 是存儲于訪問者計算機中的變量。這篇文章主要介紹了jQuery利用cookie 實現(xiàn)本地收藏功能不重復無需多次命名,需要的朋友可以參考下2019-11-11

