实现 JavaScript 褪色图像鼠标悬停和鼠标移出效果的最佳方法是什么?
我想 jQuery 将是用于执行此操作的库。我当然可以弄清楚如何在鼠标悬停时切换图像的来源,然后在鼠标移开时将其切换回来。但是我怎样才能通过图像的淡入淡出来实现这种效果呢?
我已经尝试使用 jQuery Cycle 插件来实现这一目标,但没有成功。我觉得也许有一种更简单的方法可以做到这一点。我很感激你的帮助,有什么想法吗?
I would imagine jQuery would be the library to use in order to do this. I can certainly figure out how to switch the source of an image on mouseover and then switch it back on mouseout. But how could I achieve this effect with a fade to and from the images?
I have tried to achieve this already using the jQuery Cycle plugin and I was unsuccessful. I feel maybe there's an easier way to do this. I'd appreciate the help, any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将图像放入 DIV 中,并将 DIV 的背景设置为第一张图像。在 DIV 中放置一个图像标签,并将 SRC 指向第二个图像。
现在您可以调整 IMG 的不透明度,暴露其后面的背景图像,而无需交换任何内容。
使用 .opacity()
确保在包装 DIV 上放置尺寸,否则一旦内部图像的不透明度为零,它就会消失。
Put an image in a DIV and set the background of the DIV to the first image. Put an image tag in the DIV and point the SRC to the second image.
Now you can fiddle with the opacity of the IMG, exposing the background image behind it without having to swap anything.
use .opacity()
Make sure you put dimensions on the wrapper DIV or else it will vanish once the opacity of the image inside gets to zero.
CSS3 添加了一堆转换属性来处理类似的事情,但目前如果您决定使用 jQuery,则可以结合使用
.animate
函数和mouseenter和
mouseleave
。示例:
.stop
将清除之前的动画并使其完成,这样,如果用户将鼠标快速悬停在元素上多次,它不会让动画在很长的队列中累积。CSS3 is adding a bunch of transition properties to handle things like this, but for the moment if you're set on using jQuery you could make use of the
.animate
function combined withmouseenter
andmouseleave
.Example:
The
.stop
will clear the previous animation and make it finish so that if the user were to hover quickly over the element several times, it won't let the animations build up in a long queue.你可以试试这个
You can try this