使用大量选择器简化此 jQuery 代码

发布于 2024-10-07 04:35:03 字数 2292 浏览 1 评论 0原文

我如何简化此代码?鼠标事件都非常相似。因此应该有一种方法来缩短代码。

谢谢, 约翰内斯

 var $allItems   = $('ul.image-grid li'),
    $kindertanzItems  = $('li[data-type=kindertanz]'),
    $urbanItems   = $('li[data-type=urban]'),
    $ethnoItems   = $('li[data-type=ethno]'),
    $gesundheitItems  = $('li[data-type=gesundheit]');


   $kindertanzItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $kindertanzItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "black"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $kindertanzItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $urbanItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $urbanItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "#101011"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $urbanItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $ethnoItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $ethnoItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "black"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $ethnoItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $gesundheitItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $gesundheitItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "#101011"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $gesundheitItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });

how can i simplify this code? The Mouse-Events are all quite similar. Therefore there should be a way to shorten the code.

THANKS,
Johannes

 var $allItems   = $('ul.image-grid li'),
    $kindertanzItems  = $('li[data-type=kindertanz]'),
    $urbanItems   = $('li[data-type=urban]'),
    $ethnoItems   = $('li[data-type=ethno]'),
    $gesundheitItems  = $('li[data-type=gesundheit]');


   $kindertanzItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $kindertanzItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "black"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $kindertanzItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $urbanItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $urbanItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "#101011"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $urbanItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $ethnoItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $ethnoItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "black"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $ethnoItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });


   $gesundheitItems.hover(function(){
    $allItems.each(function(){
     $(this).css("opacity","0.1"); 
    });
    $gesundheitItems.each(function(){
     $(this).css("opacity","1").css("background-color","#232628").css("border-color", "#101011"); 
    });
   }).mouseleave(function(){
    $allItems.each(function(){
     $(this).css("opacity","1"); 
    });
    $gesundheitItems.each(function(){
     $(this).css("background-color","#33373b").css("border-color", "#222527"); 
    });
   });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-10-14 04:35:03

您只应用样式 - 因此您应该使用 CSS 而不是 Javascript。

创建一个 CSS 文件并添加以下内容:

ul.image-grid:hover {
   /* Opacity and color rules for whole ul in here */
}

您应该向它们添加类,而不是通过数据类型来识别

  • ,这样您就可以执行以下操作:
  • ul.image-grid > li.kindertanz:hover {
       /* Opacity and color rules for this li in here */
    }
    

    更新:要求是突出显示不仅是鼠标悬停在哪个

  • 上,还有具有相同数据类型 的所有其他
  • 。执行此操作的 Javascript 是:
  • var $allItems = $("ul.image-grid li");
    
    // Notice that hover() can take two functions,
    // one for mouseenter, one for mouseleave
    $allItems.hover(function () {
       // Mouseenter
       $allItems.css("opacity", "0.1"); // No need for each(),
                                        // jquery will apply to all elements
    
       // Depending on your jquery version...
       // If you're using jQuery 1.4.3+ you can do this:
       var type = $(this).data("type");
       // If you're using jQuery 1.4.2 and below, you have to do this:
       var type = $(this).attr("data-type");
    
       // Get all items of the same type
       $("li[data-type=" + type + "]").css({
           // You can set multiple CSS rules in one go like this
           "opacity": "1",
           "background-color": "#232628",
           "border-color": "black"
       });
    }, function () {
       // Do something similar on mouseleave
    });
    

    希望最终有所帮助!

    旁注:虽然上面的方法可行,但我建议为突出显示状态设置一个 CSS 类。这样,您就不必弄乱 JavaScript 中的所有 CSS 属性,只需对所有项目执行 .removeClass("highlight") ,然后 .addClass("highlight") > 对于其他人。

    You're only applying styles - therefore you should be using CSS not Javascript.

    Create a CSS file and add the following:

    ul.image-grid:hover {
       /* Opacity and color rules for whole ul in here */
    }
    

    And instead of identifying <li>s by their data-type, you should add classes to them, so you can do:

    ul.image-grid > li.kindertanz:hover {
       /* Opacity and color rules for this li in here */
    }
    

    Update: The requirement is to highlight not only whichever <li> the mouse is hovering over, but also every other <li> with the same data-type. The Javascript to do this is:

    var $allItems = $("ul.image-grid li");
    
    // Notice that hover() can take two functions,
    // one for mouseenter, one for mouseleave
    $allItems.hover(function () {
       // Mouseenter
       $allItems.css("opacity", "0.1"); // No need for each(),
                                        // jquery will apply to all elements
    
       // Depending on your jquery version...
       // If you're using jQuery 1.4.3+ you can do this:
       var type = $(this).data("type");
       // If you're using jQuery 1.4.2 and below, you have to do this:
       var type = $(this).attr("data-type");
    
       // Get all items of the same type
       $("li[data-type=" + type + "]").css({
           // You can set multiple CSS rules in one go like this
           "opacity": "1",
           "background-color": "#232628",
           "border-color": "black"
       });
    }, function () {
       // Do something similar on mouseleave
    });
    

    Hope that finally helps!

    Sidenote: while the above will work, I'd recommend setting a CSS class for the highlighted state. That way instead of messing with all the CSS attributes in your javascript, you can simply do .removeClass("highlight") for all items, and .addClass("highlight") for the others.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文