jQuery悬停与setTimeout(与hoverIntent相反)

发布于 2024-09-01 07:50:36 字数 790 浏览 8 评论 0原文

我有一组图像,对于每个图像,我想翻转并闪烁到不同的图像半秒左右,然后恢复到原始图像,即使鼠标仍然位于图像上(即没有鼠标悬停) )

建议使用 setTimeout 但我不知道如何正确使用它。

http://thepool.ie/rollover/

有一个页面示例....我就像鼠标悬停时出现的图像一样,快速出现然后又消失。

我在网上搜索了示例,但找不到任何内容...任何帮助将不胜感激。

谢谢, 安德鲁

编辑:

这是我目前用于悬停图像的代码

$(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.

http://thepool.ie/rollover/

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷→糖丶微凉 2024-09-08 07:50:36

使用 setTimeout 很简单。它以函数作为第一个参数——就像许多 jQuery 方法一样——以毫秒为单位的时间作为第二个参数。

我从你的代码开始并对其进行了一些重构。最初,如果用户在计时器触发之前将鼠标悬停在图像上、离开然后返回到图像上,则可能会出现竞争条件。现在,切换到备用图像和返回原始图像的逻辑是分开的。如果图像已经被换出,鼠标悬停处理程序也会短路。我还缓存了 $(this) 返回的 jQuery 对象,以稍微提高速度(缓存 jQuery 对象是一个很好的做法)。

我将 hover 属性更改为 data-hover (HTML5 规范允许您使用自定义属性,但要求它们以 data- 开头)并检查因为它出现在鼠标悬停选择器中。

最后,我删除了额外的 ready 包装器 ($(document).ready(function(){…})$(function(){…}< /code> 是相同的,不需要两者都有)

$(function() {
    $('.rollover[data-hover]').mouseover(function() {
        var $this = $(this), originalImage = $this.attr('src');
        if ($this.data('imagesSwapped')) {
            return;
        }
        $this.attr('src', $this.attr('data-hover')).data('imagesSwapped', true);
        setTimeout(function(){
            $this.attr('src', originalImage).removeData('imagesSwapped');
        }, 500);
    });
});

代码尚未测试。

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 to data-hover (the HTML5 spec lets you use custom attributes but requires that they start with data-) 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).

$(function() {
    $('.rollover[data-hover]').mouseover(function() {
        var $this = $(this), originalImage = $this.attr('src');
        if ($this.data('imagesSwapped')) {
            return;
        }
        $this.attr('src', $this.attr('data-hover')).data('imagesSwapped', true);
        setTimeout(function(){
            $this.attr('src', originalImage).removeData('imagesSwapped');
        }, 500);
    });
});

The code has not been tested.

温柔戏命师 2024-09-08 07:50:36

在这里您可以找到一些清晰的示例:

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/

活泼老夫 2024-09-08 07:50:36

像这样的东西

$('#myid').hover(function() {
    $(this).addClass('hovered');
    setTimeout(function() { $('#myid').removeClass('hovered'); }, 100);
});

Something like

$('#myid').hover(function() {
    $(this).addClass('hovered');
    setTimeout(function() { $('#myid').removeClass('hovered'); }, 100);
});
乖乖哒 2024-09-08 07:50:36

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文