標籤:
先從js事件來考慮,這個功能是怎麼實現的。
1:當下滑滑鼠的時候,出現按鈕。
2:點擊按鈕,回到頂部。按鈕隱藏。
(1)先弄個預留位置,按鈕要在哪個位置顯示
1 <!-- 擴充 id 也可以有樣式,不過是#id顯示-->2 <ul id="jump">3 <!-- display:none:內聯標籤 tops 點擊 之後 到top-->4 <li style="display: none; height: 50px;"><a id="tops" href="#top" title="返回頂部"></a></li>5 <!-- <li style=""></li> -->6 </ul>
(2)滑動滑鼠,觸發事件;當超過多少距離的時候,顯示,或者隱藏
1 $(window).scroll(function() {2 if ($(window).scrollTop() > 50) {3 $(‘#jump li:eq(0)‘).fadeIn(800);//逐漸顯示4 } else {5 $(‘#jump li:eq(0)‘).fadeOut(800);//逐漸隱藏6 }7 });
(3)點擊按鈕,觸發事件;以動畫效果回到頂部
1 $("#top").click(function() {2 $(‘body,html‘).animate({scrollTop:0},400);//以動畫的效果返回3 });
接著來說說他的樣式:
1:在哪個位置出現,預留位置在哪裡2:按鈕的樣式
(1):首先肯定是預留位置的樣式吧,在哪裡出現
1 /*出現的位置*/ 2 #jump{ 3 position:fixed; /*position:定位 fixed:相對於視窗*/ 4 _position:absolute; 5 top:400px; /*距離頂端400像素*/ 6 _top:expression(documentElement.scrollTop + "px"); 7 left: 50%; /*元素自己距離左邊的多少*/ 8 margin-left:550px; /*margin外邊框屬性*/ 9 _margin-right:0px;10 width:40px;11 z-index: 400; /*元素的疊隊順序*/12 }
(2):點擊按鈕的樣式與背景
1 /*點擊框的樣式*/ 2 #tops{ 3 display:block; /*元素應該產生框的類型*/ 4 width:40px; 5 height:42px; 6 cursor:pointer; /*要顯示的游標的形狀 這個是一個手*/ 7 } 8 /*背景圖片*/ 9 #tops{10 background:url(../img/top.png) repeat scroll 0% 0% transparent;/*背景圖片*/11 }
(3)當把滑鼠放在按鈕上顯示的樣式
1 /*滑鼠放在上面就會顯示的樣式*/2 #tops:hover{3 background:url(../img/toped.png) repeat scroll 0% 0% transparent;4 }
然後他就出來了,哈哈。
總結
網頁怎麼實現返回頂部的按鈕