標籤:lin smo push script read targe class hang title
正常的本頁面錨連結跳轉的時候跟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的?網上好多好多,找不到正確的?留言郵箱啊,博主服務一條龍。。。
頁面中的平滑滾動——smooth-scroll.js的使用