JavaScript中通用的jquery動畫滾屏實例
實現(xiàn)效果
在網(wǎng)站頁面上,點擊某個超鏈接,頁面跳轉到某個位置,跳轉過程有一個動畫滾動效果,這是一種比較酷的體驗。這種效果是如何實現(xiàn)的呢,本文通過實際代碼來詳解實現(xiàn)方法。
實現(xiàn)代碼
網(wǎng)頁代碼
<!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方法,實現(xiàn)思路是,當點擊某個超鏈接時,通過jquery的animate將屏幕滾動到某個位置。注意animate函數(shù)的兩個參數(shù),一個是滾動位置,一個是動畫滾動的時間毫秒。
$('#tech').on("click", function () { $('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800); return false; });
雖然實現(xiàn)了效果,這里有個問題,如果滾動的超鏈接較多,那么就要寫不少重復代碼,還要注意滾動位置不要寫錯。下面通過改變一下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對JSON數(shù)據(jù)排序的3個例子
這篇文章主要介紹了javascript對JSON數(shù)據(jù)排序的3個例子的相關資料2014-04-04JS和jQuery通過this獲取html標簽中的屬性值(實例代碼)
本文通過實例代碼給大家分享JS和jQuery通過this獲取html標簽中的屬性值,非常不錯,具有參考借鑒價值,需要額朋友參考下吧2017-09-09使用CSS+JavaScript或純js實現(xiàn)半透明遮罩效果的實例分享
這篇文章主要介紹了使用CSS+JavaScript或純js實現(xiàn)半透明遮罩效果的實例分享,編寫半透明遮罩層時要注意定位問題、不要滿屏遮罩,需要的朋友可以參考下2016-05-05