簡體   English   中英

單擊后,導航欄中的#anchors鏈接未關閉

[英]links to #anchors in navbar not closing after clicked

我之前曾解決過以下問題:單擊后導航欄鏈接沒有崩潰,感謝@Kami,我使用下面的代碼進行了此工作

$(document).on('click','.navbar-collapse.in',function(e) {
if( $(e.target).is('a') ) {
$(this).collapse('toggle');
}
});

在導航欄折疊后Bootstrap 3中切換,不會重新打開導航

但是,當我在下面添加此漂亮的功能以防止導航欄重疊內容時,它壞了。

此功能的哪一部分導致以上內容中斷,我該如何實現?

  function scrollToAnchor() {
if($(".jquery-anchor").length > 0 && document.URL.indexOf("#") >= 0){
var anchor = document.URL.split("#")[1];
$(".jquery-anchor").each(function() {
    if($(this).attr("name") == anchor) {
        $("html,body").animate({
                scrollTop: $(this).offset().top - 50},
            'slow');
        }           
    });

}

}

$(function() {
$("a[href*='#JDG']: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 - 30 //offsets for fixed header
    }, 1000);
    return false;
  }
 }
 });

 //Executed on page load with URL containing an anchor tag.
 if($(location.href.split("#")[1])) {
  var target = $('#'+location.href.split("#")[1]);
  if (target.length) {
    $('html,body').animate({
      scrollTop: target.offset().top - 30 //offset height of header here too.
    }, 1000);
    return false;
  }
}


});

使用@Shouvik建議從偏移html錨點來調整以適應固定標頭,我更改了一行代碼,僅查找以#JDG開頭的錨點,因為沒有此鏈接到我的模態窗口的錨點就損壞了。 您可以在此處查看發生的情況,單擊“我的服務”菜單項后這些項將不會關閉。 函數放置在bootstrap.min.js文件的末尾

您實際上並不是在告訴腳本關閉菜單。 為此,請在function scrollToAnchor()函數內添加以下行:

$('.navbar-collapse.in').collapse('hide');

編輯 :再看一下腳本,上面的行應該放在下面的行中,而不是function scrollToAnchor()函數。 單擊菜單項時必須應用它:

$("a[href*='#JDG']:not([href='#'])").click(function() {
    //...
    $('.navbar-collapse.in').collapse('hide');
});

賈斯珀(Jasper)指出了為什么這些東西在一起時不起作用。

我只需要刪除

return false;

從這兩個功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM