jQuery:根据浏览器滚动条的位置向菜单项添加css类
我有菜单:jQuery:根据浏览器滚动条的位置向菜单项添加css类,jquery,browser,scroll,dimensions,Jquery,Browser,Scroll,Dimensions,我有菜单: <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>
<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>
我对jQuery维度属性不是很熟悉,所以这段代码没有多大意义,但我希望您能理解
如果有人能告诉我怎么做,那就太酷了
谢谢:)不完全清楚你想做什么,但我会试试看。要获取窗口垂直滚动的数量,您需要使用jQuery的
scrollTop()
函数。height()。类似的内容可能更接近您的需要:
// bind a function to the window's scroll event, this will update
// the 'active' class every time the user scrolls the window
$(window).scroll(function() {
// find the li with class 'active' and remove it
$("ul.menu-bottom li.active").removeClass("active");
// get the amount the window has scrolled
var scroll = $(window).scrollTop();
// add the 'active' class to the correct li based on the scroll amount
if (scroll <= 500) {
$("#m1").addClass("active");
}
else if (scroll <= 1000) {
$("#m2").addClass("active");
}
else {
$("#m3").addClass("active");
}
}
//将函数绑定到窗口的滚动事件,这将更新
//每次用户滚动窗口时的“活动”类
$(窗口)。滚动(函数(){
//找到类为“active”的li并将其删除
$(“ul.menu-bottom li.active”).removeClass(“active”);
//获取窗口已滚动的数量
var scroll=$(窗口).scrollTop();
//根据滚动量将“活动”类添加到正确的li
如果(非常感谢!它正是我想要的:)没问题。很高兴我能帮助你。我想你错过了);在你的最后一行…谢谢!
// bind a function to the window's scroll event, this will update
// the 'active' class every time the user scrolls the window
$(window).scroll(function() {
// find the li with class 'active' and remove it
$("ul.menu-bottom li.active").removeClass("active");
// get the amount the window has scrolled
var scroll = $(window).scrollTop();
// add the 'active' class to the correct li based on the scroll amount
if (scroll <= 500) {
$("#m1").addClass("active");
}
else if (scroll <= 1000) {
$("#m2").addClass("active");
}
else {
$("#m3").addClass("active");
}
}