jQuery悬停以无限循环改变各种背景,直到鼠标移出

发布于 2024-09-18 22:03:41 字数 750 浏览 1 评论 0原文

我有一个带有背景图像的

,它将更改背景 4 次,延迟 300 毫秒。我尝试过使用 setTimeout ,它似乎工作正常,但是clearTimeout(t);当鼠标移出时由于背景继续变化而失败。

$(document).ready(function() {
    $(".image").hover(function(){
        var obj = $('.image');
        $(this).css("background-position", "0 -90");
        var t=setTimeout(function(){obj.css("background-position", "0 -180")}, 300);
        var t=setTimeout(function(){obj.css("background-position", "0 -270")}, 600);
        var t=setTimeout(function(){obj.css("background-position", "0 -360")}, 900);
    }, function() {
        $(this).css("background-position", "0 0");
        clearTimeout(t);
    });
});

我也想在悬停函数中插入一种无限循环的方法,直到释放鼠标。

对不起我的学校英语。

提前致谢!

I've a <div> with a background-image that will change the background 4 times with a delay of 300ms. I've tried with setTimeout which seems to work correct, but clearTimeout(t); when the mouse moves out fails because the backgrounds continue changing.

$(document).ready(function() {
    $(".image").hover(function(){
        var obj = $('.image');
        $(this).css("background-position", "0 -90");
        var t=setTimeout(function(){obj.css("background-position", "0 -180")}, 300);
        var t=setTimeout(function(){obj.css("background-position", "0 -270")}, 600);
        var t=setTimeout(function(){obj.css("background-position", "0 -360")}, 900);
    }, function() {
        $(this).css("background-position", "0 0");
        clearTimeout(t);
    });
});

I would like too to insert to the hover function a way to have a infinite loop until the mouse is released.

Sorry for my school English.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

伪心 2024-09-25 22:03:41

将 t var dec 移到函数之外。那么它就会在闭包中。

例如这样的:

$(document).ready(function() {
    var t1,t2,t3;

    $(".image").hover(function(){
        var obj = $('.image');
        $(this).css("background-position", "0 -90");
        var t1=setTimeout(function(){obj.css("background-position", "0 -180")}, 300);
        var t2=setTimeout(function(){obj.css("background-position", "0 -270")}, 600);
        var t3=setTimeout(function(){obj.css("background-position", "0 -360")}, 900);
    }, function() {
        $(this).css("background-position", "0 0");
        clearTimeout(t1);
        clearTimeout(t2);
        clearTimeout(t3);
    });
});

这段代码还有其他问题,但我只是回答这个问题。

move the t var dec outside of the function. then it will be in the closure.

eg something like this:

$(document).ready(function() {
    var t1,t2,t3;

    $(".image").hover(function(){
        var obj = $('.image');
        $(this).css("background-position", "0 -90");
        var t1=setTimeout(function(){obj.css("background-position", "0 -180")}, 300);
        var t2=setTimeout(function(){obj.css("background-position", "0 -270")}, 600);
        var t3=setTimeout(function(){obj.css("background-position", "0 -360")}, 900);
    }, function() {
        $(this).css("background-position", "0 0");
        clearTimeout(t1);
        clearTimeout(t2);
        clearTimeout(t3);
    });
});

There are other issues with this code but I'm just answering the question.

聆听风音 2024-09-25 22:03:41

因为这样变量t会被前两个setTimeout覆盖两次,所以只保留最后一个setTimeout的代码,所以当您调用 clearTimeout 仅清除最后一个 setTimeout

您可以做的是使用三个不同的变量来存储它,改为 setInterval ,或者使用循环来设置超时。

Because with this the variable t is overwritten twice by the two previous setTimeouts, so only the code for the last setTimeout is preserved, so when you call clearTimeout you're only clearing the last setTimeout.

What you can do is to use three different variables to store this, change to setInterval instead, or use a loop to set the timeouts.

悲念泪 2024-09-25 22:03:41

你试过jquery的animate吗?可能会简化很多事情——他们会为你处理所有的计时器和东西。它已融入您已经使用的库中,所以为什么不利用它呢?

$('.image').mouseover(function(){
    $(this).stop().animate(
        {backgroundPosition:"(0 -360px)"}, 
        {duration:1800})
    })
.mouseout(function(){
    $(this).stop().animate(
        {backgroundPosition:"(0 0)"}, 
        {duration:100})
    });

默认情况下,它将使用线性动画,但您可以设置不同的缓动,如果您想要一些不同的东西。

Have you tried jquery's animate? Would probably simplify things a lot -- they'll deal with all the timers and stuff for you. It's baked into the library you're already using so why not take advantage of it?

$('.image').mouseover(function(){
    $(this).stop().animate(
        {backgroundPosition:"(0 -360px)"}, 
        {duration:1800})
    })
.mouseout(function(){
    $(this).stop().animate(
        {backgroundPosition:"(0 0)"}, 
        {duration:100})
    });

By default it's going to use a liniar animation, but you could set up different easing if you wanted something different.

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