Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  115] [ 4]  / answers: 1 / hits: 96468  / 13 Years ago, mon, october 31, 2011, 12:00:00

I have a Bee image and I want to animate it using jQuery.



The idea is to move the image from left (outside of screen) to right (outside of screen) to create an effect like it's flying.


More From » jquery

 Answers
13

Your bee needs to be absolutely positioned, something like this:



<div id=b style=position:absolute; top:50px>B</div>


I've used a div here, but it could just as well be an <img> tag. As meo pointed out, don't forget the top attribute, because some browsers don't work without it. Then you can animate it:



$(document).ready(function() {
$(#b).animate({left: +=500}, 2000);
$(#b).animate({left: -=300}, 1000);
});


Here is a jsfiddle demo.



If you want to have a continuous animation as Hira pointed out, put the animation code in functions, make sure the left and right movement is the same, and use the onComplete option of animate() to call the next animation:



function beeLeft() {
$(#b).animate({left: -=500}, 2000, swing, beeRight);
}
function beeRight() {
$(#b).animate({left: +=500}, 2000, swing, beeLeft);
}

beeRight();


And the fiddle for that.


[#89358] Friday, October 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cherish

Total Points: 734
Total Questions: 94
Total Answers: 86

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;