jQuery實(shí)現(xiàn)錨點(diǎn)向下平滑滾動(dòng)特效示例
實(shí)現(xiàn)效果:
實(shí)現(xiàn)原理:
使用jQuery animate()方法實(shí)現(xiàn)頁面平滑滾動(dòng)特效
$('html, body').animate({ scrollTop: $(hash).offset().top }, 800, function(){ window.location.hash = hash; });
簡(jiǎn)單實(shí)例代碼:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ // Add smooth scrolling to all links $("a").on('click', function(event) { // Make sure this.hash has a value before overriding default behavior if (this.hash !== "") { // Prevent default anchor click behavior event.preventDefault(); // Store hash var hash = this.hash; // Using jQuery's animate() method to add smooth page scroll // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area $('html, body').animate({ scrollTop: $(hash).offset().top }, 800, function(){ // Add hash (#) to URL when done scrolling (default click behavior) window.location.hash = hash; }); } // End if }); }); </script> <style> body, html, .main { height: 100%; } section { min-height: 100%; } </style> </head> <body> <a href="#section2" rel="external nofollow" style=" font-size: 30px; font-weight: bold; text-align: center; ">點(diǎn)擊此處平滑滾動(dòng)到第二部分</a> <div class="main"> <section></section> </div> <div class="main" id="section2"> <section style=" background-color: #03c03c; color: #fff; font-size: 30px; text-align: center"> SECTION 2 </section> </div> </body> </html>
以上這篇jQuery實(shí)現(xiàn)錨點(diǎn)向下平滑滾動(dòng)特效示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery Validate表單驗(yàn)證插件 添加class屬性形式的校驗(yàn)
這篇文章主要介紹了jQuery Validate表單驗(yàn)證插件,在class屬性中添加校驗(yàn)規(guī)則進(jìn)行簡(jiǎn)單的校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01基于jquery的多彩百分比 動(dòng)態(tài)進(jìn)度條 投票效果顯示效果實(shí)現(xiàn)代碼
基于jquery的多彩百分比 動(dòng)態(tài)進(jìn)度條 投票效果顯示效果實(shí)現(xiàn)代碼,學(xué)習(xí)jquery的朋友可以參考下。2011-08-08基于jQuery.i18n實(shí)現(xiàn)web前端的國(guó)際化
這篇文章主要介紹了基于jQuery.i18n實(shí)現(xiàn)web前端的國(guó)際化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2018-05-05jquery用offset()方法獲得元素的xy坐標(biāo)
jquery 獲得元素的 xy坐標(biāo),用offset()方法,body屬性設(shè)置margin :0;padding:0;,需要的朋友可以參考下2014-09-09詳解jQuery中的getAll()和cleanData()
這篇文章主要介紹了jQuery之getAll()和cleanData()函數(shù)的實(shí)例代碼解析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-04-04