简体   繁体   English

单击后,导航栏中的#anchors链接未关闭

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

I had previously solved issue where my navbar links weren't collapsing after being clicked and thanks to @Kami I had this working using the below code 我之前曾解决过以下问题:单击后导航栏链接没有崩溃,感谢@Kami,我使用下面的代码进行了此工作

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

from Bootstrap 3 after navbar collapse, toggle wont reopen nav 在导航栏折叠后Bootstrap 3中切换,不会重新打开导航

but when I added this nice function below to keep the navbar from overlapping content it broke. 但是,当我在下面添加此漂亮的功能以防止导航栏重叠内容时,它坏了。

What part of this function caused the above to break and how can I implement both? 此功能的哪一部分导致以上内容中断,我该如何实现?

  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;
  }
}


});

Using @Shouvik suggestion from offsetting an html anchor to adjust for fixed header I changed one line of code to only look for anchors that started with #JDG as without this links to anchors to my modal windows broke. 使用@Shouvik建议从偏移html锚点来调整以适应固定标头,我更改了一行代码,仅查找以#JDG开头的锚点,因为没有此链接到我的模态窗口的锚点就损坏了。 You can see what is happening here where my services menu items wont close after being clicked. 您可以在此处查看发生的情况,单击“我的服务”菜单项后这些项将不会关闭。 the functions are placed at the end of the bootstrap.min.js file 函数放置在bootstrap.min.js文件的末尾

You are not actually telling your script to close the menu. 您实际上并不是在告诉脚本关闭菜单。 In order to do that, add the following line inside your function scrollToAnchor() function: 为此,请在function scrollToAnchor()函数内添加以下行:

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

EDIT : Having another look at the script, the above line should be placed inside the following lines instead of function scrollToAnchor() function, ie. 编辑 :再看一下脚本,上面的行应该放在下面的行中,而不是function scrollToAnchor()函数。 it has to apply when you click the menu item: 单击菜单项时必须应用它:

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

Jasper pointed out the issue why these weren't working when together. 贾斯珀(Jasper)指出了为什么这些东西在一起时不起作用。

I just had to remove the 我只需要删除

return false;

from both functions. 从这两个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM