jQuery實現返回頂部

來源:互聯網
上載者:User

標籤:返回頂部   style   blog   color   os   io   ar   cti   

由於項目需要,寫了個返回頂部的小功能...

 1     /*返回頂部*/ 2     function toTop() { 3         $(".to_top").hide(); 4         $(window).scroll(function(){ 5             var scroll_top = $(document).scrollTop(); 6             if(scroll_top != 0){ 7                 $(".to_top").show(); 8             }else{ 9                 $(".to_top").hide();10             }11         });12         $(".to_top").click(function(){13             $("body,html").animate({scrollTop:0},500);14             $(".to_top").hide();15            16         });17          $(".to_top").mouseover(function(){18             $(".to_top").css({19                 "filter":"alpha(opacity=70)",20                 "-moz-opacity":"0.7",21                 "opacity":"0.7"22             });23              return false;24         });25           $(".to_top").mouseout(function(){26             $(".to_top").css({27                 "filter":"alpha(opacity=50)",28                 "-moz-opacity":"0.5",29                 "opacity":"0.5"30             });31              return false;32         });33     }34     toTop();

值得注意的是scrollTop()方法:

      它的作用是擷取匹配元素相對捲軸頂部的位移。(此方法對可見和隱藏元素均有效)

      用法:

          比如設定相對捲軸頂部的位移:$("div.demo").scrollTop(300);

 

在html和css代碼中也需要注意定位反回頂部按鈕的位置和使他浮在現有元素之上。

html代碼:

1 <div class="to_top" style="z-index:99999;display:block;">2  </div> 

css代碼:

 1 /*這裡是返回頂部按鈕*/ 2 .to_top { 3     position:fixed; 4     display:block; 5     background: url(../../images/footer-totop.png) no-repeat; 6     filter:alpha(opacity=50); 7     -moz-opacity:0.5; 8     opacity:0.5; 9     float:right;10     width:60px;11     height:60px;12     margin-top:37%;13     margin-left:95%;14 }

 

 

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.