Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the jquery animate function work internally?

Tags:

jquery

here is small code

<div id="clickme">
Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123"
style="position: relative; left: 10px;" />

$('#clickme').click(function() {
$('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
}, 5000, function() {
    // Animation complete.
   });
});

it is clear from the code left is increasing and opacity will be .25. how jquery manage to do so...does jquery internally execute a loop to increase the left and change the opacity until it becomes .25. need guidance. thanks

like image 804
Keith Costa Avatar asked Nov 29 '11 17:11

Keith Costa


People also ask

How does jQuery animate work?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

Which jQuery function is used to add animation to an element?

The . animate() method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties.

How you can use jQuery to animate a flash to the button?

The solution to Jquery To Animate A Flash To The Button Selected will be demonstrated using examples in this article. $("#someElement"). fadeOut(100). fadeIn(100).


2 Answers

It's gradually increases (or decreases) values at set periods using timer. It can't use a loop because if that was the case it would block/freeze the main js thread while doing that and you wouldn't see the animation. Everything in js is (or should be) asynchronous, via events.

like image 101
Andrey Avatar answered Oct 17 '22 12:10

Andrey


To know how the animate code looks and works have a look at the source:

https://github.com/jquery/jquery/blob/master/src/effects.js

like image 25
abcde123483 Avatar answered Oct 17 '22 12:10

abcde123483