Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每次运行jQuery函数(以防反弹效果)_Jquery - Fatal编程技术网

每次运行jQuery函数(以防反弹效果)

每次运行jQuery函数(以防反弹效果),jquery,Jquery,一个简单的问题,如何运行jQuery bounce悬停在always bounce下面。所以我不需要悬停它,它每次都会反弹 代码如下: $(document).ready(function(){ $(".button").hover(function(){ $(".button img") .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump

一个简单的问题,如何运行jQuery bounce悬停在always bounce下面。所以我不需要悬停它,它每次都会反弹

代码如下:

$(document).ready(function(){
        $(".button").hover(function(){
            $(".button img")
            .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump
            .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100) // second jump
            .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100); // the last jump
        });
    });

这将只需将2000更改为您想要的任何时间(动画完成后)

如果你想让它停下来,就打电话

clearInterval(timer);

这将只需将2000更改为您想要的任何时间(动画完成后)

如果你想让它停下来,就打电话

clearInterval(timer);

将反弹代码放入函数中,并从上一个动画的回调调用它,以便重新启动:

function bounce_img(img) {
    img.animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump
        .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100) // second jump
        .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100,  // last jump
                function() {bounce_img(img);}); // restart
}

$(document).ready() {
    $(".button img").each(function() {
        bounce_img($(this));
    });
});

将反弹代码放入函数中,并从上一个动画的回调调用它,以便重新启动:

function bounce_img(img) {
    img.animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump
        .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100) // second jump
        .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100,  // last jump
                function() {bounce_img(img);}); // restart
}

$(document).ready() {
    $(".button img").each(function() {
        bounce_img($(this));
    });
});

您应该使用
setInterval
和timer=动画的总延时

setInterval(function(){
    $(".button img")
    .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump
    .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100) // second jump
    .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100); // the last jump
},600);
或递归函数

function bounce(){
    $(".button img")
        .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200)
        .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100)
        .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100,function(){
        bounce();
    }); 
}
bounce();

您应该使用
setInterval
和timer=动画的总延时

setInterval(function(){
    $(".button img")
    .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200) // first jump
    .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100) // second jump
    .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100); // the last jump
},600);
或递归函数

function bounce(){
    $(".button img")
        .animate({top:"-10px"}, 200).animate({top:"-4px"}, 200)
        .animate({top:"-7px"}, 100).animate({top:"-4px"}, 100)
        .animate({top:"-6px"}, 100).animate({top:"-4px"}, 100,function(){
        bounce();
    }); 
}
bounce();

使用
setInterval
定期运行函数。使用
setInterval
定期运行函数。更好的解决方案优于setInterval IMO,+1更好的解决方案优于setInterval IMO,+1