jQuery實現返回頂部功能

來源:互聯網
上載者:User

標籤:jquery   返回頂部   

整理兩個實現功能,一個是右下角的返回頂部,一個是右側的返回頂部,分別

             

第一種實現

一、JSP或HTML(主體結構)

在body中添加

<body id="top"><p id="back-to-top"><a href="#top"><span></span></a></p></body>

二、CSS(樣式化)

<style>p#back-to-top {position: fixed;bottom: 50px;right: 50px;}p#back-to-top a {text-align: center;text-decoration: none;color: #d1d1d1;display: block;width: 50px;    /*使用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: #d1d1d1 url(/img/back_to_top.png) no-repeat center center;border-radius: 6px;display: block;height: 50px;width: 50px;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: #979797 url(/img/back_to_top.png) no-repeat center center;}</style>
圖片自己網上找資源

三、jQuery(動態效果)

<script>$(document).ready(function() {    //首先將#back-to-top隱藏    $("#back-to-top").hide();    //當捲軸的位置處於距頂部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            },            500);            return false;        });    });});</script>



第二種實現

<style>.backToTop {display: none;width: 18px;line-height: 1.2;padding: 5px 0;background-color: #000;color: #fff;font-size: 12px;text-align: center;position: fixed;_position: absolute;right: 10px;bottom: 100px;_bottom: "auto";cursor: pointer;opacity: .6;filter: Alpha(opacity = 60);}</style>

<script>(function() {    var $backToTopTxt = "返回頂部"    $backToTopEle = $(‘<div class="backToTop"></div>‘).appendTo($("body")).text($backToTopTxt).attr("title", $backToTopTxt).click(function() {        $("html, body").animate({            scrollTop: 0        },120);    })    $backToTopFun = function() {        var st = $(document).scrollTop(),        winh = $(window).height(); (st > 0) ? $backToTopEle.show() : $backToTopEle.hide();        //IE6下的定位        if (!window.XMLHttpRequest) {            $backToTopEle.css("top", st + winh - 166);        }    };    $(window).bind("scroll", $backToTopFun);    $(function() {        $backToTopFun();    });})();</script>



聯繫我們

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