使用大量选择器简化此 jQuery 代码
我如何简化此代码?鼠标事件都非常相似。因此应该有一种方法来缩短代码。
谢谢, 约翰内斯
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只应用样式 - 因此您应该使用 CSS 而不是 Javascript。
创建一个 CSS 文件并添加以下内容:
您应该向它们添加类,而不是通过数据类型来识别
,这样您就可以执行以下操作:
更新:要求是突出显示不仅是鼠标悬停在哪个
上,还有具有相同
数据类型
的所有其他希望最终有所帮助!
旁注:虽然上面的方法可行,但我建议为突出显示状态设置一个 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:
And instead of identifying
<li>
s by their data-type, you should add classes to them, so you can do:Update: The requirement is to highlight not only whichever
<li>
the mouse is hovering over, but also every other<li>
with the samedata-type
. The Javascript to do this is: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.