jQuery:根据浏览器滚动条位置将 css 类添加到菜单项
我有一个菜单:
<ul class="menu-bottom">
<li id="m1" class="active"><a id="1" href="#"><span>Link 1</span></a></li>
<li id="m2"><a id="2" href="#"><span>Link 2</span></a></li>
<li id="m3"><a id="3" href="#"><span>Link 3</span></a></li>
</ul>
我希望根据浏览器的滚动条位置,"active" 类会正确 li > 元素。
我是这样看的:
if ($(document).height() == 500) {
$('#m1').parent().addClass('active').
siblings().removeClass('active');
}
if ($(document).height() == 1000) {
$('#m2').parent().addClass('active').
siblings().removeClass('active');
}
if ($(document).height() == 1500) {
$('#m2').parent().addClass('active').
siblings().removeClass('active');
}
我对 jQuery Dimensions 属性不是很熟悉,所以这段代码没有多大意义,但我希望你能明白。
如果有人能告诉我如何完成这项工作,那就太酷了。
谢谢 :)
I have a menu:
<ul class="menu-bottom">
<li id="m1" class="active"><a id="1" href="#"><span>Link 1</span></a></li>
<li id="m2"><a id="2" href="#"><span>Link 2</span></a></li>
<li id="m3"><a id="3" href="#"><span>Link 3</span></a></li>
</ul>
I want that depending of browser's scroller position, the "active" class goes the correct < li > element.
This is how I see it :
if ($(document).height() == 500) {
$('#m1').parent().addClass('active').
siblings().removeClass('active');
}
if ($(document).height() == 1000) {
$('#m2').parent().addClass('active').
siblings().removeClass('active');
}
if ($(document).height() == 1500) {
$('#m2').parent().addClass('active').
siblings().removeClass('active');
}
I am not very familiar with jQuery Dimensions properties so this code doesn't make much sense, but I hope you get the idea.
If someone could tell me how to make this work, it would be really cool.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前还不完全清楚你想做什么,但我会尝试一下。要获取窗口垂直滚动的量,您需要使用 jQuery 的
scrollTop()
函数。height()
函数为您提供调用它的元素的高度(以像素为单位),因此如果滚动值是您想要的值,那么它不会很有用。像这样的东西可能更接近您所需要的:即使上面的内容没有走上正确的轨道,还有其他一些需要注意的事情可能会有所帮助。
$('#m1').parent().addClass('active').siblings().removeClass('active');
等行可能不会执行您期望的操作。它实际上是将类添加到父 ul 中,然后从 ul 的同级中删除,而不是将“active”类添加到 li 中,然后将其从 li 的同级中删除。尝试从每一行中删除.parent()
,这应该可行。另外,由于您在 if 条件中使用 == ,因此仅当值恰好为 500 或 1000 等时才会添加该类,我怀疑这是否是您的意图。这就是为什么在上面的代码中我将条件语句更改为 <= 。
希望这有帮助。
It's not entirely clear what it is you're trying to do, but I'll take a stab at it. To get the amount the window has scrolled vertically you'll want to use jQuery's
scrollTop()
function. Theheight()
function gives you the height in pixels of the element upon which it is called, so it won't be very useful if the scroll value is what you want. Something like this might be closer to what you need:Even if the above isn't on the right track, there are a couple other things to note that might help. The lines such as
$('#m1').parent().addClass('active').siblings().removeClass('active');
are likely not doing what you expect. Rather than adding the 'active' class to the li and then removing it from the li's siblings, it's actually adding the class to the parent ul and removing it from the ul's siblings. Try removing.parent()
from each line and that should work.Also, since you are using == in your if conditions, the class will be added only when the value is exactly 500 or 1000 etc. which I doubt is what you intend. That's why in the code above I changed it to be <= for the conditional statements.
Hope this helps.