简约的 scrollspy 将 active class 更改为标签
Minimalistic scrollspy changing active class just to a tag
谁能解决这个问题?这是关于简约 scrollspy 的代码。它在内容为 offset.top 时添加活动 class。我知道这段代码有效,但我能知道是否有办法将 active class 更改为 "a" 标签而不是其父标签吗?我删除了 .parent() 但它似乎不起作用。不欲全里活跃。提前致谢。
代码笔:https://jsfiddle.net/mekwall/up4nu/
HTML
<nav class="clearfix">
<ul class="clearfix" id="top-menu">
<li><a href="#services">Services</a></li>
<li><a href="#aboutus">About Us</a></li>
<li><a href="#testimonial">Testimonials</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
jQuery
// Cache selectors
var lastId,
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 click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
// 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 : "";
if (lastId !== id) {
lastId = id;
// Set/remove active class -- This is the part.
menuItems
.parent().removeClass("active")
.end().filter("[href='#"+id+"']").parent().addClass("active");
}
});
Here an updated working jsfiddle
首先我为 a.active 添加了一个 CSS,因为它适合 [li] space,所以无法辨别什么是活动的。
那么你必须设置 starting 为 active 标签而不是 [li]
现在您可以将最后 2 javascript 行更改为
menuItems
.parent().find('a').removeClass("active")
.find('a').end().filter("[href='#"+id+"']").addClass("active");
使用 find('a') 获取正确的标签。
谁能解决这个问题?这是关于简约 scrollspy 的代码。它在内容为 offset.top 时添加活动 class。我知道这段代码有效,但我能知道是否有办法将 active class 更改为 "a" 标签而不是其父标签吗?我删除了 .parent() 但它似乎不起作用。不欲全里活跃。提前致谢。
代码笔:https://jsfiddle.net/mekwall/up4nu/
HTML
<nav class="clearfix">
<ul class="clearfix" id="top-menu">
<li><a href="#services">Services</a></li>
<li><a href="#aboutus">About Us</a></li>
<li><a href="#testimonial">Testimonials</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
jQuery
// Cache selectors
var lastId,
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 click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
// 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 : "";
if (lastId !== id) {
lastId = id;
// Set/remove active class -- This is the part.
menuItems
.parent().removeClass("active")
.end().filter("[href='#"+id+"']").parent().addClass("active");
}
});
Here an updated working jsfiddle
首先我为 a.active 添加了一个 CSS,因为它适合 [li] space,所以无法辨别什么是活动的。
那么你必须设置 starting 为 active 标签而不是 [li]
现在您可以将最后 2 javascript 行更改为
menuItems
.parent().find('a').removeClass("active")
.find('a').end().filter("[href='#"+id+"']").addClass("active");
使用 find('a') 获取正确的标签。