使用jQuery實現返回頂部功能,jquery實現返回頂部

來源:互聯網
上載者:User

使用jQuery實現返回頂部功能,jquery實現返回頂部

很多網站上都有返回頂部的效果,本文闡述如何使用jquery實現返回頂部按鈕。

首先需要在頂部添加如下html元素:

<p id="back-to-top"><a href="#top"><span></span>返回頂部</a></p>

其中a標籤指向錨點top,可以在頂部防止一個<a name="top"></a>的錨點,這樣在瀏覽器不支援js時也可以實現返回頂部的效果了。

要想讓返回頂部的圖片顯示在右側,還需要一些css樣式,如下:

/*returnTop*/p#back-to-top{    position:fixed;    display:none;    bottom:100px;    right:80px;}p#back-to-top a{    text-align:center;    text-decoration:none;    color:#d1d1d1;    display:block;    width:64px;    /*使用CSS3中的transition屬性給跳轉連結中的文字添加漸層效果*/    -moz-transition:color 1s;    -webkit-transition:color 1s;    -o-transition:color 1s;}p#back-to-top a:hover{    color:#979797;}p#back-to-top a span{    background:transparent url(/static/imgs/sprite.png?1202) no-repeat -25px -290px;    border-radius:6px;    display:block;    height:64px;    width:56px;    margin-bottom:5px;    /*使用CSS3中的transition屬性給<span>標籤背景顏色添加漸層效果*/    -moz-transition:background 1s;    -webkit-transition:background 1s;    -o-transition:background 1s;}#back-to-top a:hover span{    background:transparent url(/static/imgs/sprite.png?1202) no-repeat -25px -290px;}

  

上面樣式中的背景圖片是雪碧圖,下面提供兩個單獨的返回頂部圖片方便朋友們使用:

   

有了html和樣式,我們還需要用js控制返回頂部按鈕,在頁面滾動時漸隱漸現返回頂部按鈕。

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script><script>$(function(){        //當捲軸的位置處於距頂部100像素以下時,跳轉連結出現,否則消失        $(function () {            $(window).scroll(function(){                if ($(window).scrollTop()>100){                    $("#back-to-top").fadeIn(1500);                }                else                {                    $("#back-to-top").fadeOut(1500);                }            });            //當點擊跳轉連結後,回到頁面頂部位置            $("#back-to-top").click(function(){                $('body,html').animate({scrollTop:0},1000);                return false;            });        });    });</script>

這樣就可以了。

注意在載入頁面後需要向下拖動一點捲軸才可以看到返回頂部的。


html網頁中怎實現“返回頂部”的效果?


將下面的代碼儲存成一個JS檔案。然後在頁面裡調用。其中/backtotop.gif為返回頂部的圖片。

function setCss(){var a=[];a.push(".back_to_top{display:none;width:50px;height:50px;overflow:hidden;right:40px;position:fixed; bottom:50px;_position:absolute;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-134));}\n");a.push(".back_to_top a{display:inline-block;width:50px;height:50px;overflow:hidden;background:url(/backtotop.gif) 0px 0px no-repeat;}\n");a.push(".back_to_top a:hover{background-position:-50px 0px}");try{var b=document.createStyleSheet();b.cssText=a.join("");}catch(c){var b=document.createElement("style");b.type="text/css";b.textContent=a.join("");document.getElementsByTagName("HEAD").item(0).appendChild(b);}}function createLink(){var a=document.createElement("div");a.id="JS_back_to_top";a.className="back_to_top";a.innerHTML='<a href="#" title="返回頂部"></a>';document.body.appendChild(a);}function GetPageScroll(){var b,a;if(window.pageYOffset){a=window.pageYOffset;b=window.pageXOffset;}else{if(document.documentElement&&document.documentElement.scrollTop){a=document.documentElement.scrollTop;b=document.documentElement.scrollLeft;}else{if(document.body){a=document.body.scrollTop;b=document.body.scrollLeft;}}}return{x:b,y:a};}if(window.attachEvent){window.attachEvent("onscroll",backToTop);}......餘下全文>>

蘇健[權威專家]

一個jquery的返回頂部的問題

#back-to-top{position:fixed; bottom:150px; right:30px;}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.