正常的本頁面錨鏈接跳轉的時候跟PPT似的,特別生硬,用戶體驗非常差。
這時候我們就可以借助smooth-scroll.js這個插件,來實現本頁面的平滑的跳轉。
1首先,導入必須的JS文件
<script src="js/jquery-1.10.2.js"></script> <script src="js/jquery.smooth-scroll.min.js"></script> <script src="js/jquery.ba-bbq.js"></script>
2我們可以這樣來調用插件
$('a').smoothScroll({});
3可以根據自己的需要指定一個外部容器,那么滾動就是在這個(#container)容器內發生,而不是在頁面級別發生了
$('#container a').smoothScroll();
4我們可以可以通過下面的方式來排除指定容器的包含元素
$('#container a').smoothScroll({
excludeWithin: ['.container2']
});
5通過下面的語句來排除滿足指定條件的元素
$('a').smoothScroll({
exclude: ['.rough','#chunky']
});
6調整滑動到哪個位置就停止
$('.backtotop').smoothScroll({
offset: -100
});
7設定一個滑動開始之前的回調函數
$('a').smoothScroll({
beforeScroll: function() {
alert('ready to go!');
}
});
8設定一個滑動結束的回調函數
$('a').smoothScroll({
afterScroll: function() {
alert('we made it!');
}
});
你也可以自己配置參數
$.smoothScroll({ //滑動到的位置的偏移量 offset: 0, //滑動的方向,可取 'top' 或 'left' direction: 'top', // 只有當你想重寫默認行為的時候才會用到 scrollTarget: null, // 滑動開始前的回調函數。`this` 代表正在被滾動的元素 beforeScroll: function() {}, //滑動完成后的回調函數。 `this` 代表觸發滑動的元素 afterScroll: function() {}, //緩動效果 easing: 'swing', //滑動的速度 speed: 400, // "自動" 加速的系數 autoCoefficent: 2 });
什么?沒看懂??沒關系!!下邊還有一波無腦操作!
只需要把文件導入后,將下邊的代碼copy進去就可以了,便可輕松實現頁面中的平滑滾動
<script> $(document) .on('click', 'a[href*="#"]', function() { if ( this.hash ) { $.bbq.pushState( '#/' + this.hash.slice(1) ); return false; } }) .ready(function() { $(window).bind('hashchange', function(event) { var tgt = location.hash.replace(/^#\/?/,''); if ( document.getElementById(tgt) ) { $.smoothScroll({scrollTarget: '#' + tgt}); } }); $(window).trigger('hashchange'); }); </script>
下面就是點擊technolog滑動到 id為a1的div區,簡單吧!
<a href="#a1">Technology</a> <div id="a1"> </div>
smooth-scroll.js的下載地址?網上好多好多,找不到正確的?留言郵箱啊,博主服務一條龍。。。
