每次动画循环时创建一个随机的“左”位置

时间:2012-03-03 09:38:20

标签: jquery

我有一些降落伞的动画,我想随机添加一些。每次循环(使用jquery计时器插件),我都希望它从一个随机的左侧位置运行。

我可以使用以下功能

创建我的随机数
function randomLeft() {
var randomNumber = Math.floor(Math.random() * 101);
return randomNumber+'%';
}

我有我的循环:

$('#parachute_wrap img').everyTime ( 11, function (){
          $('#parachute_wrap img')
          .animate({top:"1000px"},25000)
          .animate({top:"-100px"});
});

我尝试了以下操作:

$('#parachute_wrap img').everyTime ( 11, function (){
          $('#parachute_wrap img')
          .animate({top:"1000px"},25000)
          .animate({top:"-100px", left: randomLeft()});
});

但是当我只是将一个函数作为一个值抛出时,它看起来并不喜欢。

你会怎么做呢?

1 个答案:

答案 0 :(得分:0)

你试过这个: -

$('#parachute_wrap img').everyTime ( 11, function (){
          $('#parachute_wrap img')
          .animate({top:"1000px"},25000)
          .animate({top:"-100px", left: Math.floor(Math.random() * 101) + '%'});
});

$('#parachute_wrap img').everyTime ( 11, function (){
          $('#parachute_wrap img')
          .animate({top:"1000px"},25000)
          .animate({top:"-100px", left: Math.floor(Math.random() * 101) + 'px'});
});