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

标签 jquery wordpress

我编写了一个 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/

相关文章:

php - jQuery 表单提交问题

javascript - 创建一个javascript函数来隐藏元素?

wordpress - 如何将类和元素添加到 WordPress "wp_nav_menu"子菜单中的 <a> 标签?

php - 将 WooCommerce 订单项自定义字段总和保存为新元数据

wordpress - 如何更改 Google 对网站名称的显示

jquery - 无法在 "beforeunload"事件中设置背景图像属性

javascript - 使用 C# Restsharp,如何复制表单帖子?

javascript - 控制 Google Mx 的缩放级别

Wordpress 多站点 Nginx 子目录安装无法再找到数据库

css - 如果仅使用某个模板,如何添加功能