jQuery li 图像在背景中淡入淡出(在类中淡入淡出)
我目前正在使用这段代码:
var gallery = $('ul#gallery').children();
$(gallery).filter(':even').not(':last').hover(function () {$(this).toggleClass('next')});
我正在尝试让它淡入这个新类。目前,有一个
I'm currently using this code:
var gallery = $('ul#gallery').children();
$(gallery).filter(':even').not(':last').hover(function () {$(this).toggleClass('next')});
I'm trying to make it fade this new class in. Currently, there's an <li> with an image in it, no background. When the 'next' class is added, it gives it a background image when hovered over. Is there a way to just fade in the new class without making the image blink/fade at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您安装了 jQueryUI,您可以通过添加持续时间来
淡出动画类。http://jqueryui.com/demos/toggleClass/
但据我所知,有没有单独的不透明度设置仅影响背景图像。因此,如果您想淡入该元素,则需要淡入整个元素,正如您所说,这不是您想要的。
如果您确实想要这种效果,另一种方法可能是在您为该类提供的元素前面添加一个单独的元素,并淡入该元素(及其背景图像)。
该元素需要具有
绝对
定位,以便它不会影响其余内容。你最终会得到类似这样的结果:
CSS:
HTML:
jQuery:
hover()
有两个函数。一个是当您鼠标进入
时,另一个是当您鼠标离开
时。// 将所有 .background 元素的不透明度设置为 0
$('.background').css({不透明度: 0});
编辑:
仅供参考,您可以更改选择器以立即获得您想要的内容,而无需调用
.filter()
等。编辑:
更改了第一句中的措辞并添加了文档链接。
If you have jQueryUI installed, you can
fadeanimate a class by adding a duration.http://jqueryui.com/demos/toggleClass/
But as far as I know, there is no separate opacity setting that affects only the background image. So if you want to fade that in, you would need to fade the entire element, which, as you stated, is not what you want.
If you really want the effect, an alternative may be to prepend a separate element to the one you were giving the class, and fade in that element (with its background image).
The element would need to have
absolute
positioning so that it doesn't affect the rest of the content.You would end up with something like:
CSS:
HTML:
jQuery:
hover()
takes two functions. One when youmouseenter
, the other when youmouseleave
.// Set opacity to 0 for all .background elements
$('.background').css({opacity: 0});
EDIT:
FYI, you can change your selector to get what you want right away without having to call
.filter()
, and so on.EDIT:
Changed wording in first sentence and added link to docs.