大佬教程收集整理的这篇文章主要介绍了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>
我希望这取决于浏览器的滚动条位置,“活动”类是正确的< 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属性不太熟悉,所以这段代码没有多大意义,但我希望你能理解.
如果有人能告诉我如何使这项工作,那将是非常酷的.
谢谢 :)
// 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"); } }
即使上述内容不在正确的轨道上,还有一些其他注意事项可能有所帮助.诸如$(‘#m1’).parent().addClass(‘active’).siblings().removeClass(‘active’);可能没有做你期望的事情.它不是将“active”类添加到li中,然后将其从li的兄弟中删除,而是实际将类添加到父级ul并将其从ul的兄弟中删除.尝试从每一行中删除.parent(),这应该有效.
此外,由于您在if条件中使用==,因此仅当值恰好为500或1000等时才会添加该类,我怀疑这是您的意图.这就是为什么在上面的代码中我将它改为< = for条件语句. 希望这可以帮助.
以上是大佬教程为你收集整理的jQuery:根据浏览器滚动条位置将css类添加到菜单项全部内容,希望文章能够帮你解决jQuery:根据浏览器滚动条位置将css类添加到菜单项所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。