简体   繁体   English

在页面滚动中,将活动类添加到<a href>固定菜单中的</a>匹配<a href>哈希</a>

[英]On page scroll add active class to matching <a href>hash in fixed menu

This one has been taking up a bit of my time to figure out so I'm appealing for someone to brighten my day. 这个人已经花了我一些时间来弄清楚,所以我呼吁有人来改善我的生活。

This popular piece of code, works great if your menu links only contain #hashlocation - it updates your menu item with the active class when you down at that part of the page. 如果您的菜单链接仅包含#hashlocation,那么这段流行的代码非常有用-当您浏览页面的那部分时,它将使用活动类更新菜单项。

// Cache selectors
var topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";
   // Set/remove active class
   menuItems
     .parent().removeClass("active")
     .end().filter("[href=#"+id+"]").parent().addClass("active");
});​

The issue for me , is that it obviously doesn't work if my menu contains other pages that are not hash tags, and in particular with the ones that do have hashtags, they are preceded with the full URL . 对我来说问题是,如果我的菜单包含非哈希标签的其他页面,则显然不起作用,尤其是那些带有哈希标签的页面,它们之前都带有完整的URL

I need this code to be adjusted so that it does not throw a "syntax error, unrecognized expression", and also accept the fact that the menu may contain other links pointing elsewhere. 我需要调整此代码,以便它不会引发“语法错误,无法识别的表达式”,并且还接受菜单可能包含指向其他位置的其他链接这一事实。

Here is a fiddle http://jsfiddle.net/stevenmunro/ytbhrz1g/2/ 这是一个小提琴http://jsfiddle.net/stevenmunro/ytbhrz1g/2/

if you would remove the full URL before the hash from "Foo" it will work. 如果您要从“ Foo”中删除哈希之前的完整URL,它将起作用。

I have tried 我努力了

menuItems = topMenu.find('a[href*=#]'),

and also inside scrollItems = menuItems.map 以及在scrollItems = menuItems.map中

var item = $($(this).attr("href").split("#"));
item = $("#" + item[1]);

References 参考文献

Change Active Menu Item on Page Scroll? 更改页面滚动上的活动菜单项?

Appreciate your help. 感谢您的帮助。

Thanks to James Hay for putting me on the right track. 感谢James Hay使我走上正确的道路。 The suggestion was very similar to what I had already tried, however it's now a one-liner and works well. 该建议与我已经尝试过的建议非常相似,但是现在它是单线的,并且效果很好。

I also changed; 我也改变了;

menuItems = topMenu.find("a"),

to

menuItems = topMenu.find('a[href*=#]'),

and changed; 并且改变了;

menuItems
 .parent().removeClass("active")
 .end().filter("[href=#"+id+"]").parent().addClass("active");

to

menuItems
 .parent().removeClass("active")
 .end().filter("[href$=" + id + "]").parent().addClass("active");

As long as the items in the menu and in order as they are too down the page, this will work. 只要菜单中的项目以及它们在页面中的顺序过高,就可以使用。 If the menu items are scrambled up, it will not change as expected. 如果菜单项被加扰,则不会按预期更改。 Perhaps that is something else we could figure out too, but the question is answered so thank you. 也许这也是我们可以解决的其他问题,但问题已得到解答,非常感谢。

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

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