最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中通用的jquery動畫滾屏實例

 更新時間:2022年07月26日 09:25:18   作者:北京王老師???????  
這篇文章主要介紹了JavaScript中通用的jquery動畫滾屏實例,本文通過實際代碼來詳解實現方法,需要的朋友可以參考一下

實現效果

在網站頁面上,點擊某個超鏈接,頁面跳轉到某個位置,跳轉過程有一個動畫滾動效果,這是一種比較酷的體驗。這種效果是如何實現的呢,本文通過實際代碼來詳解實現方法。

實現代碼

網頁代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>滾屏效果</title>
<script src="./assets/js/jquery.min.js"></script>
</head>
<body style=" margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; ">
<div id="header" style="height: 100px;">
<a id="tech" class="scroll-a" href="#hi-tech" rel="external nofollow"  rel="external nofollow" >技術</a>
<a id="code" class="scroll-a" href="#hi-code" rel="external nofollow"  rel="external nofollow" >代碼</a>
</div>

<div style="background-color: gray;height: 600px;">
<h1>間隔</h1>
</div>

<div style="background-color: white;height: 600px;" id="hi-tech">
<h1>技術</h1>
<a id="tohead1" class="scroll-a" href="#header" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >返回頂部</a>
</div>

<div style="background-color: gray;height: 600px;" id="hi-code">
<h1>代碼</h1>
<a id="tohead" class="scroll-a" href="#header" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >返回頂部</a>
</div>
</body>
<script>
$('#tech').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800);
return false;
});
$('#code').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-code').offset().top}, 800);
return false;
});
$('#tohead').on("click", function () {
$('html,body').animate({scrollTop:$('#header').offset().top}, 800);
return false;
});
$('#tohead1').on("click", function () {
$('html,body').animate({scrollTop:$('#header').offset().top}, 800);
return false;
});
</script>
</html>

這里主要用到了jquery的animate方法,實現思路是,當點擊某個超鏈接時,通過jquery的animate將屏幕滾動到某個位置。注意animate函數的兩個參數,一個是滾動位置,一個是動畫滾動的時間毫秒。

$('#tech').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800);
return false;
});

雖然實現了效果,這里有個問題,如果滾動的超鏈接較多,那么就要寫不少重復代碼,還要注意滾動位置不要寫錯。下面通過改變一下jquery選擇器來優(yōu)化代碼。

優(yōu)化代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>滾屏效果</title>
<script src="./assets/js/jquery.min.js"></script>
</head>
<body style=" margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; ">
<div id="header" style="height: 100px;">
<a id="tech" class="scroll-a" href="#hi-tech" rel="external nofollow"  rel="external nofollow" >技術</a>
<a id="code" class="scroll-a" href="#hi-code" rel="external nofollow"  rel="external nofollow" >代碼</a>
</div>

<div style="background-color: gray;height: 600px;">
<h1>間隔</h1>
</div>

<div style="background-color: white;height: 600px;" id="hi-tech">
<h1>技術</h1>
<a id="tohead1" class="scroll-a" href="#header" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >返回頂部</a>
</div>

<div style="background-color: gray;height: 600px;" id="hi-code">
<h1>代碼</h1>
<a id="tohead" class="scroll-a" href="#header" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >返回頂部</a>
</div>
</body>
<script>
$('.scroll-a').on("click", function () {
let scrollto = $(this).attr('href');
$('html,body').animate({scrollTop:$(scrollto).offset().top}, 800);
return false;
});
</script>
</html>

可以看出,通過使用jquery class選擇器,并使用jquery的this對象獲取滾動目標,明顯減少了代碼,代碼看起來更加清楚。

到此這篇關于JavaScript中通用的jquery動畫滾屏實例的文章就介紹到這了,更多相關JS jquery動畫滾屏內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • JavaScript常用工具函數庫匯總

    JavaScript常用工具函數庫匯總

    業(yè)務中比較常用的JavaScript工具函數,可以用在前端環(huán)境,也可以用在node服務端,統(tǒng)一整理,方便查閱,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下
    2020-09-09
  • javascript實現在網頁任意處點左鍵彈出隱藏菜單的方法

    javascript實現在網頁任意處點左鍵彈出隱藏菜單的方法

    這篇文章主要介紹了javascript實現在網頁任意處點左鍵彈出隱藏菜單的方法,設計鼠標事件及css樣式操作的相關技巧,需要的朋友可以參考下
    2015-05-05
  • Ajax中GET與POST請求操作方法梳理介紹

    Ajax中GET與POST請求操作方法梳理介紹

    Ajax全稱是Asynchronous Javascript And XML(異步JavaScript和XML),在網頁中,利用XMLHttpRequest對象和服務器進行數據交互的方式,這篇文章主要介紹了Ajax中GET與POST請求操作
    2022-11-11
  • Javascript里使用Dom操作Xml

    Javascript里使用Dom操作Xml

    Javascript里使用Dom操作Xml...
    2007-01-01
  • 讓瀏覽器非阻塞加載javascript的幾種方法小結

    讓瀏覽器非阻塞加載javascript的幾種方法小結

    通常大多數瀏覽器是并行下載資源的,但由于外部腳本的特殊性例如通過腳本改變文檔的DOM結構、腳本之間的存在依賴關系、使用document.write 向頁面輸出HTML等。
    2011-04-04
  • JavaScript中window.showModalDialog()用法詳解

    JavaScript中window.showModalDialog()用法詳解

    這篇文章主要介紹了JavaScript中window.showModalDialog()用法詳解,需要的朋友可以參考下
    2014-12-12
  • 微信小程序實現日期范圍選擇

    微信小程序實現日期范圍選擇

    這篇文章主要為大家詳細介紹了微信小程序實現日期范圍選擇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • javascript 常用功能總結

    javascript 常用功能總結

    javascript 常用功能總結,學習js的朋友可以參考下
    2012-03-03
  • TypeScript中的 ! 和 ? 操作符

    TypeScript中的 ! 和 ? 操作符

    在 TypeScript 中,!?和???是兩個非常重要且常用的操作符,分別用于非空斷言和可選鏈操作,下面就來具體介紹一下如何使用,具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • 純javascript判斷查詢日期是否為有效日期

    純javascript判斷查詢日期是否為有效日期

    很多網站都涉及到輸入日期選項,如果客戶日期輸入錯誤,可能導入查詢不到甚至查詢到錯誤的信息,為了更好的滿足用戶需求,需要對日期進行校驗,下面給大家介紹使用純javascript如何判斷查詢日期是否為有效日期,需要的朋友可以參考下
    2015-08-08

最新評論

互助| 皋兰县| 张家港市| 道真| 拜城县| 乌拉特前旗| 尼木县| 开封县| 维西| 治县。| 横山县| 微博| 武功县| 康定县| 永春县| 麦盖提县| 华宁县| 垦利县| 朔州市| 丰顺县| 建昌县| 贵溪市| 丹江口市| 奎屯市| 雷波县| 阜南县| 泰兴市| 宣恩县| 新竹县| 句容市| 封丘县| 革吉县| 沙洋县| 杨浦区| 获嘉县| 邳州市| 新巴尔虎左旗| 沂水县| 淮阳县| 淮阳县| 健康|