JavaScript中通用的jquery動(dòng)畫(huà)滾屏實(shí)例
實(shí)現(xiàn)效果
在網(wǎng)站頁(yè)面上,點(diǎn)擊某個(gè)超鏈接,頁(yè)面跳轉(zhuǎn)到某個(gè)位置,跳轉(zhuǎn)過(guò)程有一個(gè)動(dòng)畫(huà)滾動(dòng)效果,這是一種比較酷的體驗(yàn)。這種效果是如何實(shí)現(xiàn)的呢,本文通過(guò)實(shí)際代碼來(lái)詳解實(shí)現(xiàn)方法。
實(shí)現(xiàn)代碼
網(wǎng)頁(yè)代碼
<!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" >技術(shù)</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>技術(shù)</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方法,實(shí)現(xiàn)思路是,當(dāng)點(diǎn)擊某個(gè)超鏈接時(shí),通過(guò)jquery的animate將屏幕滾動(dòng)到某個(gè)位置。注意animate函數(shù)的兩個(gè)參數(shù),一個(gè)是滾動(dòng)位置,一個(gè)是動(dòng)畫(huà)滾動(dòng)的時(shí)間毫秒。
$('#tech').on("click", function () { $('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800); return false; });
雖然實(shí)現(xiàn)了效果,這里有個(gè)問(wèn)題,如果滾動(dòng)的超鏈接較多,那么就要寫(xiě)不少重復(fù)代碼,還要注意滾動(dòng)位置不要寫(xiě)錯(cuò)。下面通過(guò)改變一下jquery選擇器來(lái)優(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" >技術(shù)</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>技術(shù)</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>
可以看出,通過(guò)使用jquery class選擇器,并使用jquery的this對(duì)象獲取滾動(dòng)目標(biāo),明顯減少了代碼,代碼看起來(lái)更加清楚。
到此這篇關(guān)于JavaScript中通用的jquery動(dòng)畫(huà)滾屏實(shí)例的文章就介紹到這了,更多相關(guān)JS jquery動(dòng)畫(huà)滾屏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- jquery.jsPlumb實(shí)現(xiàn)拓?fù)鋱D
- JavaScript/jQuery實(shí)現(xiàn)切換頁(yè)面效果
- JavaScript實(shí)現(xiàn)樓梯滾動(dòng)特效(jQuery實(shí)現(xiàn))
- ajax在js中和jQuery中的用法實(shí)例詳解
- js實(shí)現(xiàn)七夕表白彈幕效果 jQuery實(shí)現(xiàn)彈幕技術(shù)
- JavaScript與JQuery框架基礎(chǔ)入門教程
- JS實(shí)現(xiàn)jQuery的append功能
- jQuery是用來(lái)干什么的 jquery其實(shí)就是一個(gè)js框架
相關(guān)文章
javascript設(shè)計(jì)模式之解釋器模式詳解
這篇文章主要介紹了javascript設(shè)計(jì)模式之解釋器模式詳解,當(dāng)有一個(gè)語(yǔ)言需要解釋執(zhí)行,并且可以將該語(yǔ)言中的句子表示為一個(gè)抽象語(yǔ)法樹(shù)的時(shí)候,可以考慮使用解釋器模式,需要的朋友可以參考下2014-06-06js中的scroll和offset 使用比較的實(shí)例與分析
這篇文章介紹了js中的scroll和offset使用比較的實(shí)例與分析,有需要的朋友可以參考一下2013-09-09JavaScript中判斷的優(yōu)雅寫(xiě)法示例
判斷語(yǔ)句這個(gè)我們不陌生,就是判斷是否滿足指定的條件,如果滿足則執(zhí)行一定的代碼,否則跳過(guò),下面這篇文章主要給大家介紹了關(guān)于JavaScript中判斷的優(yōu)雅寫(xiě)法,需要的朋友可以參考下2021-10-10javascript對(duì)JSON數(shù)據(jù)排序的3個(gè)例子
這篇文章主要介紹了javascript對(duì)JSON數(shù)據(jù)排序的3個(gè)例子的相關(guān)資料2014-04-04JS和jQuery通過(guò)this獲取html標(biāo)簽中的屬性值(實(shí)例代碼)
本文通過(guò)實(shí)例代碼給大家分享JS和jQuery通過(guò)this獲取html標(biāo)簽中的屬性值,非常不錯(cuò),具有參考借鑒價(jià)值,需要額朋友參考下吧2017-09-09使用CSS+JavaScript或純js實(shí)現(xiàn)半透明遮罩效果的實(shí)例分享
這篇文章主要介紹了使用CSS+JavaScript或純js實(shí)現(xiàn)半透明遮罩效果的實(shí)例分享,編寫(xiě)半透明遮罩層時(shí)要注意定位問(wèn)題、不要滿屏遮罩,需要的朋友可以參考下2016-05-05