JQuery實現淡入淡出的回到頂部按鈕

來源:互聯網
上載者:User

標籤:z-index   部分   anim   pos   alt   定位   logs   font   tom   

效果如下:

按鈕的html和css代碼如下。

<div id="gotop">    <i class="fa fa-chevron-up"></i></div>

i標籤<i class="fa fa-chevron-up"></i>使用了Font Awesome,400多個表徵圖(包括QQ、、微博)只要30KB,可無限放大,不需要js,非常的??。

按鈕gotop用fixed定位到螢幕右下角,並且隱藏。

#gotop {    position: fixed;    right: 10px;    bottom: 12%;    z-index: 666;    display: none;    width: 40px;    height: 40px;    font-size: 30px;    line-height: 40px;    text-align: center;    color: #fff;    background-color: #21759b;    border-radius: 2px;    cursor: pointer;}#gotop:hover {    background-color: #195a84;}

JQ代碼如下,上半部分檢測捲軸位置顯示和隱藏gotop按鈕,下半部分點擊按鈕後圓滑的回到頂部。

 1 $(function () {
2 // 儲存gotop按鈕 3 var gotop = $("#gotop"); 4 // 檢測捲軸位置,高於600回頂部按鈕顯示 5 $(window).scroll(function () { 6 console.log($(window).scrollTop()); 7 if ($(window).scrollTop() > 600) { 8 gotop.fadeIn(1000); 9 }10 else {11 gotop.fadeOut(1000);12 }13 });
14 // gotop按鈕點擊,圓滑回到頂部 scrollTop: 0表示到捲軸的0,800(ms)表示用時。15 gotop.click(function () {16 $(‘body,html‘).animate({ scrollTop: 0 }, 800);18 });
19 });

 沒有特效的按鈕可以看我的部落格右下角,部落格園申請JS許可權失敗了??。

JQuery實現淡入淡出的回到頂部按鈕

聯繫我們

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