当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Creating a random "left" position every time this animation loops

问题描述:

I have an animation of some parachutes that I'd like to add some random to. Each time it loops (using jquery timer plugin), I'd like to basically have it run from a random left position.

I can create my random number with the following function

function randomLeft() {

var randomNumber = Math.floor(Math.random() * 101);

return randomNumber+'%';

}

And I have my loop:

$('#parachute_wrap img').everyTime ( 11, function (){

$('#parachute_wrap img')

.animate({top:"1000px"},25000)

.animate({top:"-100px"});

});

I tried doing the following:

$('#parachute_wrap img').everyTime ( 11, function (){

$('#parachute_wrap img')

.animate({top:"1000px"},25000)

.animate({top:"-100px", left: randomLeft()});

});

But it looks like it doesn't like when I just throw a function in as a value.

How would you go about doing this?

网友答案:

Have you tried this:-

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

OR

$('#parachute_wrap img').everyTime ( 11, function (){
          $('#parachute_wrap img')
          .animate({top:"1000px"},25000)
          .animate({top:"-100px", left: Math.floor(Math.random() * 101) + 'px'});
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: