Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery animate function equivalent in pure JavaScript

Tags:

What is the equivalent of the following jQuery animate in pure JavaScript?

function animate(element, position, speed) {    $(element).animate({      "top": position    }, speed);  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
like image 696
Rizwan Mumtaz Avatar asked Mar 20 '13 10:03

Rizwan Mumtaz


People also ask

Can you animate using JavaScript?

JavaScript animations can use any timing function. We covered a lot of examples and transformations to make them even more versatile. Unlike CSS, we are not limited to Bezier curves here. The same is about draw : we can animate anything, not just CSS properties.

What is animation function in jQuery?

Definition and Usage. 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").

Is jQuery used for animation?

With jQuery, you can create custom animations.


1 Answers

You can acheive complex animations with pure javascript by using setTimeout and setInterval methods.

Please check here.

Here is the key part of moving an element:

function move(elem) {     var left = 0     function frame() {         left++  // update parameters         elem.style.left = left + 'px' // show frame         if (left == 100)  // check finish condition             clearInterval(id)     }     var id = setInterval(frame, 10) // draw every 10ms } 
like image 122
Suresh Atta Avatar answered Sep 27 '22 20:09

Suresh Atta