2014-12-19 109 views
1

如何使用頁面向下滾動,如in this example沒有任何連接使用導航欄,特別是在JavaScript部分?當用戶點擊按鈕時引導向下滾動

我想要發生的事情就像在這個例子中:一旦用戶點擊按鈕,它將直接進入節頁面,它應該像樣本鏈接一樣平滑滾動。

這裏是我的示例代碼

<div class="jumbotron"> 

<div class="career-jumbotron"> 
    <div class="container"> 
     <a href="#opportunities" class="page-scroll btn btn-xl"> 
     Button</a> 
    </div> 
</div> 
<section id="opportunities"> 
    <div class="container"> 

    -----sample text----- 

    </div> 
</section> 
+0

歡迎來到SO。你嘗試過什麼嗎?如果你有你的代碼,你能分享給我們嗎?然後,有人可以幫助你。 :) – jazzurro 2014-12-19 01:15:34

+1

這是示例代碼 – ace 2014-12-19 03:26:47

回答

0

下面是一段JavaScript代碼,以獲得平滑滾動同一頁面鏈接,其手錶和適用於所有環節的流暢的效果開始#

你需要添加jQuery庫

$(function(){ 
    $('a[href*=#]:not([href=#])').click(function() { 
     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
      $('html,body').animate({ 
      scrollTop: (target.offset().top - 135) // adjust this according to your content 
      }, 1000); 
      return false; 
     } 
     } 
    }); 
}); 
+0

MS Edge寫入:語法錯誤,無法識別的表達式:a [href * =#]:not([href =#]) – machj 2017-11-20 08:31:32