頁面中的平滑滾動——smooth-scroll.js的使用

來源:互聯網
上載者:User

標籤: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的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.