jQuery實現(xiàn)錨點向下平滑滾動特效示例
更新時間:2017年08月29日 07:47:13 作者:Dragonir
下面小編就為大家?guī)硪黄猨Query實現(xiàn)錨點向下平滑滾動特效示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實現(xiàn)效果:

實現(xiàn)原理:
使用jQuery animate()方法實現(xiàn)頁面平滑滾動特效
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
window.location.hash = hash;
});
簡單實例代碼:
<!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;
">點擊此處平滑滾動到第二部分</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實現(xiàn)錨點向下平滑滾動特效示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery Validate表單驗證插件 添加class屬性形式的校驗
這篇文章主要介紹了jQuery Validate表單驗證插件,在class屬性中添加校驗規(guī)則進(jìn)行簡單的校驗,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
基于jquery的多彩百分比 動態(tài)進(jìn)度條 投票效果顯示效果實現(xiàn)代碼
基于jquery的多彩百分比 動態(tài)進(jìn)度條 投票效果顯示效果實現(xiàn)代碼,學(xué)習(xí)jquery的朋友可以參考下。2011-08-08
基于jQuery.i18n實現(xiàn)web前端的國際化
這篇文章主要介紹了基于jQuery.i18n實現(xiàn)web前端的國際化,具有很好的參考價值,希望對大家有所幫助2018-05-05
jquery用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ù)的實例代碼解析,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-04-04

