2016-12-14 67 views
0

我有一個完整的引導程序佈局,固定導航欄高度爲140px,每個div部分都通過導航鏈接。當我點擊鏈接轉到about部分時,它似乎直接轉到h2標籤,而不是about section div的開頭。有沒有辦法可以使用js來修復截斷問題?鏈接到頂部div

<ul> 
<li><a href>#about</a></li> 
</ul> 

部分{填充:5em的0 5em的0}

<section id="about"> 
    <div class="container"> 
    <div class="text-center"> 
    <h2>About</h2> 
    <h3>About Subheading</h3> 
     <div class="about"> 
      <div class="row"> 
       <div class="col-lg-12"> 
        </div> 
       </div> 

      </div> 
     </div> 
    </div> 
    </div> 
    </div> 
</section> 

回答

0

剛纔試了我自己,我無法重現您的問題:

http://jsfiddle.net/DTcHh/27912/

你有許多您發佈的HTML中的錯誤,例如缺少href屬性和額外的</div>結束標記

+0

我有導航欄靜態頂採用的140px 20像素填充頂部和底部的高度。點擊鏈接時是否需要一些偏移量,以便它進入

的開始> – 7O07Y7

+0

我已經更新了小提琴,似乎仍然跳到了節 – ioseph

1

試試這個

編輯的代碼視圖

$(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 - 140 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
});