gpt4 book ai didi

jquery - 根据滚动位置更改CSS——重构糟糕的代码

转载 作者:行者123 更新时间:2023-12-01 02:10:34 24 4
gpt4 key购买 nike

我编写了一个 jQuery 函数,该函数根据导航菜单项的引用部分是否在窗口中可见来更改导航菜单项的 css 值。

$(window).scroll(function() {

var scroll = $(window).scrollTop();

if (scroll <= 590) {
$("#menu-item-25 a").addClass('blue');
$("#menu-item-26 a").removeClass('blue');
$("#menu-item-22 a").removeClass('blue');
$("#menu-item-23 a").removeClass('blue');
$("#menu-item-24 a").removeClass('blue');
}
else if (scroll >= 591 && scroll <= 1380) {
$("#menu-item-26 a").addClass('blue');
$("#menu-item-25 a").removeClass('blue');
$("#menu-item-22 a").removeClass('blue');
$("#menu-item-23 a").removeClass('blue');
$("#menu-item-24 a").removeClass('blue');
}
else if (scroll >= 1381 && scroll <= 2545) {
$("#menu-item-22 a").addClass('blue');
$("#menu-item-25 a").removeClass('blue');
$("#menu-item-26 a").removeClass('blue');
$("#menu-item-23 a").removeClass('blue');
$("#menu-item-24 a").removeClass('blue');
}
else if (scroll >= 2546 && scroll <= 2969) {
$("#menu-item-23 a").addClass('blue');
$("#menu-item-25 a").removeClass('blue');
$("#menu-item-26 a").removeClass('blue');
$("#menu-item-22 a").removeClass('blue');
$("#menu-item-24 a").removeClass('blue');
}
else if (scroll >= 2970) {
$("#menu-item-24 a").addClass('blue');
$("#menu-item-25 a").removeClass('blue');
$("#menu-item-26 a").removeClass('blue');
$("#menu-item-22 a").removeClass('blue');
$("#menu-item-23 a").removeClass('blue');
}
});

看起来非常难看。有没有更好的方法来实现这个目标?

最佳答案

之前的所有答案都可以正常工作,因为您有多种方法可以使其更好,只需对 CSS 选择器进行一些更改,但如果您将在滚动事件中进行所有这些计算,您应该阅读此 John Resign Post关于如何处理滚动事件,特别是这部分:

It’s a very, very, bad idea to attach handlers to the window scroll event. Depending upon the browser the scroll event can fire a lot and putting code in the scroll callback will slow down any attempts to scroll the page (not a good idea). Any performance degradation in the scroll handler(s) as a result will only compound the performance of scrolling overall. Instead it’s much better to use some form of a timer to check every X milliseconds OR to attach a scroll event and only run your code after a delay (or even after a given number of executions – and then a delay).
- John Resign

所以,对于你的情况,我会这样:

HTML:

<div class="menu">
<a id="menu-item-22">Link 1</a>
<a id="menu-item-23">Link 2</a>
<a id="menu-item-24">Link 3</a>
<a id="menu-item-25">Link 4</a>
<a id="menu-item-26">Link 5</a>
</div>

Jquery:

var didScroll = false;

$(window).scroll(function() {
didScroll = true;
});

setInterval(function() {
if ( didScroll ) {
didScroll = false;
var $el;

//Same that all the if else statements
switch (true) {
case (scroll >= 591 && scroll <= 1380):
$el = $("#menu-item-26 a");
break;
case (scroll >= 1381 && scroll <= 2545):
$el = $("#menu-item-22 a");
break;
case (scroll >= 2546 && scroll <= 2969):
$el = $("#menu-item-23 a");
break;
case (scroll >= 2970):
$el = $("#menu-item-24 a");
break;
default: //scroll<=590
$el = $("#menu-item-25 a");
}

//Removing blue class from all links
$('.menu a').removeClass('blue');

//Adding blue class to the matched element
$el.addClass('blue');
}
}, 50);

关于jquery - 根据滚动位置更改CSS——重构糟糕的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com