jQuery动画回调问题

发布于 2024-11-14 23:58:19 字数 373 浏览 6 评论 0原文

function thumb_wheel() {
    $("#slider .nivo-controlNav img").animate({left: '-600px'}, 14000, function(){  
        $(this).css({left:'0px'}, function(){
            thumb_wheel();
        });
    });
};

基本上,一旦动画完成,我希望它将“left”重置为 0px,并再次调用自身。我尝试过使用 setInterval 等不同的方式对其进行格式化...检查了 Google、Stack 和 #jQuery...无论我尝试什么,当我添加重置时它似乎都会“挂起”。有什么想法吗?

function thumb_wheel() {
    $("#slider .nivo-controlNav img").animate({left: '-600px'}, 14000, function(){  
        $(this).css({left:'0px'}, function(){
            thumb_wheel();
        });
    });
};

Basically, once the animation finishes, I would like it to reset "left" to 0px, and call itself again. I've tried formatting it different ways, using setInterval, etc... Checked Google, Stack, and #jQuery... and no matter what I try, it seems to 'hang' when I add the reset. Any ideas?

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

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

发布评论

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

评论(2

坏尐絯 2024-11-21 23:58:19

您可以在此 jsfiddle 中看到一个可行的解决方案:
http://jsfiddle.net/GjemU/

您的代码中存在一些语法错误,并且您尝试在 .css 上使用不存在的回调

You can see a working solution in this jsfiddle:
http://jsfiddle.net/GjemU/

You had some syntax errors in your code as well as you tried to use a non-existing callback on .css

狼性发作 2024-11-21 23:58:19

回调在不同的作用域中被调用,因此 this 变量指向的对象与您想象的不同。
解决方案是使用闭包:

function thumb_wheel() {
    var el = $("#slider .nivo-controlNav img");
    el.animate({left: '-600px'}, 14000, function(){  
         el.css({left:'0px'});
         thumb_wheel();
    });
};

the callback is being called in a different scope, so the this variable points to a different object than you think.
The solution is to a use closure:

function thumb_wheel() {
    var el = $("#slider .nivo-controlNav img");
    el.animate({left: '-600px'}, 14000, function(){  
         el.css({left:'0px'});
         thumb_wheel();
    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文