jQuery悬停与setTimeout(与hoverIntent相反)
我有一组图像,对于每个图像,我想翻转并闪烁到不同的图像半秒左右,然后恢复到原始图像,即使鼠标仍然位于图像上(即没有鼠标悬停) )
建议使用 setTimeout 但我不知道如何正确使用它。
有一个页面示例....我就像鼠标悬停时出现的图像一样,快速出现然后又消失。
我在网上搜索了示例,但找不到任何内容...任何帮助将不胜感激。
谢谢, 安德鲁
编辑:
这是我目前用于悬停图像的代码
$(document).ready(function(){
$(function() {
$('.rollover').hover(function() {
var currentImg = $(this).attr('src');
$(this).attr('src', $(this).attr('hover'));
$(this).attr('hover', currentImg);
}, function() {
var currentImg = $(this).attr('src');
$(this).attr('src', $(this).attr('hover'));
$(this).attr('hover', currentImg);
});
});
});
I have a set of images that for each one I would like to rollover and flicker to a different image for half a second or so, then revert back to the original image, even if the mouse is still over the image (ie there's no mouseout)
setTimeout was suggested but I can't figure out how to use it properly.
There's an example of the page....I'd just like the image that appears on rollover to appear then disappear again quickly.
I've scoured the web for examples and can't find anything...any help would be greatly appreciated.
Thanks,
Andrew
Edit:
This is the code I'm currently using for hover images
$(document).ready(function(){
$(function() {
$('.rollover').hover(function() {
var currentImg = $(this).attr('src');
$(this).attr('src', $(this).attr('hover'));
$(this).attr('hover', currentImg);
}, function() {
var currentImg = $(this).attr('src');
$(this).attr('src', $(this).attr('hover'));
$(this).attr('hover', currentImg);
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
setTimeout
很简单。它以函数作为第一个参数——就像许多 jQuery 方法一样——以毫秒为单位的时间作为第二个参数。我从你的代码开始并对其进行了一些重构。最初,如果用户在计时器触发之前将鼠标悬停在图像上、离开然后返回到图像上,则可能会出现竞争条件。现在,切换到备用图像和返回原始图像的逻辑是分开的。如果图像已经被换出,鼠标悬停处理程序也会短路。我还缓存了
$(this)
返回的 jQuery 对象,以稍微提高速度(缓存 jQuery 对象是一个很好的做法)。我将
hover
属性更改为data-hover
(HTML5 规范允许您使用自定义属性,但要求它们以data-
开头)并检查因为它出现在鼠标悬停选择器中。最后,我删除了额外的
ready
包装器 ($(document).ready(function(){…})
和$(function(){…}< /code> 是相同的,不需要两者都有)
代码尚未测试。
Using
setTimeout
is easy. It takes a function as the first argument — just like many jQuery methods — and the time in milliseconds as the second.I started with your code and refactored it a bit. Originally, there would have been the potential for a race condition if the user mouses over, away from, and then back over the image before the timer fires. Now, the logic for switching to the alternate image and back to the original is separate. The mouseover handler also short-circuits if the images are already swapped out. I also cache the jQuery object returned by
$(this)
, for a slight speed improvement (caching jQuery objects is good practice).I changed the
hover
attribute todata-hover
(the HTML5 spec lets you use custom attributes but requires that they start withdata-
) and checked for its presense in the mouseover selector.Finally, I removed the extra
ready
wrapper ($(document).ready(function(){…})
and$(function(){…}
are the same, no need to have both of them).The code has not been tested.
在这里您可以找到一些清晰的示例:
http://www.electrictoolbox.com/using-settimeout -javascript/
但我个人建议你使用Timers插件,使用起来更加友好:
http://jquery.offput.ca/every/
Here you can find some clear examples:
http://www.electrictoolbox.com/using-settimeout-javascript/
But I would personally advice you to use the Timers plugin, which is more friendly to use:
http://jquery.offput.ca/every/
像这样的东西
Something like
setTimeOut 有更好的方法,
您可以使用
$('.rollover').mouseenter(function() {
而不是$('.rollover').hover(function() {< /code> http://api.jquery.com/mouseenter/
There is better way that setTimeOut
you can use
$('.rollover').mouseenter(function() {
instead of$('.rollover').hover(function() {
http://api.jquery.com/mouseenter/