溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery如何實(shí)現(xiàn)小火箭返回頂部特效

發(fā)布時(shí)間:2021-05-24 14:18:39 來源:億速云 閱讀:380 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)jQuery如何實(shí)現(xiàn)小火箭返回頂部特效,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

jquery實(shí)現(xiàn)小火箭返回頂部案例,供大家參考,具體內(nèi)容如下

1. 滾動(dòng)頁面,當(dāng)頁面距離頂部超出1000px,顯示小火箭。

封裝在scroll函數(shù)里,當(dāng)前頁面距離頂部為$(window).scrollTop >=1000

小火箭顯示和隱藏用fadeIn和fadeOut

//當(dāng)頁面超出1000px的時(shí)候,讓小火箭顯示,如果小于1000px,則隱藏
   $(window).scroll(function () {
    if ($(window).scrollTop() >= 1000) {
     $(".actGotop").stop().fadeIn(1000);
    } else {
     $(".actGotop").stop().fadeOut(1000);
    }
   })
});

2. 當(dāng)小火箭出現(xiàn)后,點(diǎn)擊小火箭,返回到頁面頂部。

在外面出冊點(diǎn)擊事件,獲取頁面,html或者body, 返回用animate動(dòng)畫函數(shù),到頂部即scrollTop為0,時(shí)間可以設(shè)置

$(".actGotop").click(function () {
   $("html,body").stop().animate({ scrollTop: 0 }, 1000);
  });

3. 如果要讓小火箭點(diǎn)擊后,直接回到頂部,可以只設(shè)置$(window).scrollTop(0),既可

$(".actGotop").click(function () {
   //$("html,body").stop().animate({ scrollTop: 0 }, 1000);
   //scrollTop為0
   $(window).scrollTop(0);
  });

整體代碼如下:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
  body {
   height: 8000px;
  }

  a {
   color: #FFF;
  }

  .actGotop {
   position: fixed;
   bottom: 50px;
   right: 50px;
   width: 150px;
   height: 195px;
   display: none;
   z-index: 100;
  }

  .actGotop a,
  .actGotop a:link {
   width: 150px;
   height: 195px;
   display: inline-block;
   background: url(images/gotop.png) no-repeat;
   outline: none;
  }

  .actGotop a:hover {
   width: 150px;
   height: 195px;
   background: url(images/gotop.gif) no-repeat;
   outline: none;
  }
 </style>


</head>

<body>
 <!-- 返回頂部小火箭 -->
 <div class="actGotop"><a href="javascript:;" rel="external nofollow" title="Top"></a></div>


 <script src="jquery-1.12.4.js"></script>
 <script>


  $(function () {
   //當(dāng)頁面超出1000px的時(shí)候,讓小火箭顯示,如果小于1000px,則隱藏
   $(window).scroll(function () {
    if ($(window).scrollTop() >= 1000) {
     $(".actGotop").stop().fadeIn(500);
    } else {
     $(".actGotop").stop().fadeOut(500);
    }
   })
  });

  //在外面注冊
  $(".actGotop").click(function () {
   $("html,body").stop().animate({ scrollTop: 0 }, 1000);
   //scrollTop為0
   // $(window).scrollTop(0);
  });
 </script>
</body>

</html>

jquery是什么

jquery是一個(gè)簡潔而快速的JavaScript庫,它具有獨(dú)特的鏈?zhǔn)秸Z法和短小清晰的多功能接口、高效靈活的css選擇器,并且可對CSS選擇器進(jìn)行擴(kuò)展、擁有便捷的插件擴(kuò)展機(jī)制和豐富的插件,是繼Prototype之后又一個(gè)優(yōu)秀的JavaScript代碼庫,能夠用于簡化事件處理、HTML文檔遍歷、Ajax交互和動(dòng)畫,以便快速開發(fā)網(wǎng)站。

關(guān)于“jQuery如何實(shí)現(xiàn)小火箭返回頂部特效”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI