Jquery 悬停状态取决于类名
解释起来有点困难,但我想要实现的是将一个 class / css 应用于一组具有相同类名的 div。具体来说,它将是一个显示缩略图的图库。它们都一起出现,但按类名分组。当您将鼠标悬停在特定类别的图像上时,具有同一类别的所有图像将保持不透明,而其余图像则更改其不透明度。也许这可以通过更改覆盖 div 的显示来实现?
所以本质上我需要某种方式以相同的方式显示具有相同类的 div,而不管它们悬停时的类名是什么。
如果有人能指出我正确的方向,我将非常感激!
例如(当鼠标悬停在 1 类中的任一个上时,它们是不透明的,而 2 类是透明的,反之亦然):
<div class="thumbnail 1">
<img>
</div>
<div class="thumbnail 1">
<img>
</div>
<div class="thumbnail 2">
<img>
</div>
<div class="thumbnail 2">
<img>
</div>
It's a little difficult to explain, but what I am trying to achieve is to apply a class / css to a group of divs with the same class name. Specifically it will be a gallery displaying thumbnails. They all appear together but are grouped by class name. When you hover on an image for a specific class, all images with the same class will remain opaque, while the rest change their opacity. Perhaps this could be achieved by changing the display of an overlay div?
So essentially I need some way of displaying div's with the same class in the same way, irrespective of their class name on hover.
If anyone could point me in the right direction I would be very grateful!
For example (when hovering on either of class 1 they are opaque, class 2 are transparent & vice versa):
<div class="thumbnail 1">
<img>
</div>
<div class="thumbnail 1">
<img>
</div>
<div class="thumbnail 2">
<img>
</div>
<div class="thumbnail 2">
<img>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 jQuery 轻松完成此操作。该代码适用于任何元素和类组;你不需要提前知道课程内容。它只是找到所有不具有您所悬停的元素的类的元素,并将它们变暗。
如果这是您的 HTML:
...那么,这个 jQuery 应该可以正常工作。
这里是 jsFiddle 的实时示例。
You can do this easily using jQuery. This code will work for any group of elements and classes; you don't need to know what the classes will be in advance. It simply finds all the elements that don't have the class of the one you're hovering over and dims them.
If this is your HTML:
..then, this jQuery should work fine.
Live jsFiddle example here.
应该很容易做到。无需为您编写整个函数,选择器很简单:
其中“hoverclass”是 div 组及其各自图像共享的类,“imageclass”是所有具有此悬停功能的图像共享的类(这样您就不需要不会影响您的其他图标和不相关的图像),并且“doOpacityStuff()”是您可以链接其他不透明度函数的点。
Should be easy to do. Without writing the whole function for you, the selectors are simple:
Where "hoverclass" is the class shared by the group of divs with their respective images, "imageclass" is a class that all images with this hovering functionality share (so that you don't affect your other icons and unrelated images), and "doOpacityStuff()" is the point at which you can chain in your other opacity functions.
由于您的类是动态生成的,因此您首先需要获取当前元素的类,然后如果您想对不属于某个类的元素执行某些操作:
从那里您可以使用 jQuery 的 animate 函数来更改它们他们的不透明度。
有关 not-selector 的更多信息,请访问 jQuery API 网站。
Since your classes are generated dynamically, you'll first want to get the class of the current element and then if you want to do something to elements that do not belong to a class:
From there you can use jQuery's animate function to have them change their opacity.
For more information on the not-selector, visit the jQuery API website.