Create a rotation animation: start and end slowly but quickly in the middle

I want a rotating animation to be applied to an element: the rotation should start slowly, and then become faster and faster, then it will reach the point from which it will continue very quickly, and then very slowly go slower and slower until it stops.

The graph will look like this:

^ Speed | ******** | ** *** | * **** | * *** | * *** +*-------------------------***-> Time 

How can I apply this path to jQuery animate function?

I currently have this:

 function spin() { var $myElm = $(".myClass"); function rotate(degrees) { $myElm.css({ '-webkit-transform': 'rotate(' + degrees + 'deg)', '-moz-transform': 'rotate(' + degrees + 'deg)', '-ms-transform': 'rotate(' + degrees + 'deg)', 'transform': 'rotate(' + degrees + 'deg)' }); } $({ deg: 0 }).animate({ deg: 360 * 40 }, { duration: 7000, step: function() { var deg = this.deg; rotate(deg); } }); } spin(); 
 .myClass { position: fixed; top: 30px; left: 30px; height: 200px; width: 200px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myClass"></div> 

This works, but should have a smoother slowdown. How can i do this?

+6
source share
5 answers

Try using jQuery Easing easeInOutQuart function; if the parameter this.deg : now the step function is less than 6000 or more than 8000 , set the variable deg to now divided by 8 , which is an even divisor of 14400 >: 360 * 40

  $({ deg: 0 }).animate({ deg: 360 * 40 }, { duration: 7000, easing: "easeInOutQuart", step: function(now) { var deg = now < 6000 || now > 8000 ? now / 8 : now; rotate(deg); } }); 

 /* * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ * * Uses the built in easing capabilities added In jQuery 1.1 * to offer multiple easing options * * TERMS OF USE - jQuery Easing * * Open source under the BSD License. * * Copyright © 2008 George McGinley Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * Neither the name of the author nor the names of contributors may be used to endorse * or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ // t: current time, b: begInnIng value, c: change In value, d: duration jQuery.easing['jswing'] = jQuery.easing['swing']; jQuery.extend( jQuery.easing, { easeInOutQuart: function (x, t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t*t*t + b; return -c/2 * ((t-=2)*t*t*t - 2) + b; } }); /* * * TERMS OF USE - EASING EQUATIONS * * Open source under the BSD License. * * Copyright © 2001 Robert Penner * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * Neither the name of the author nor the names of contributors may be used to endorse * or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ function spin() { var $myElm = $(".myClass"); function rotate(degrees) { $myElm.css({ '-webkit-transform': 'rotate(' + degrees + 'deg)', '-moz-transform': 'rotate(' + degrees + 'deg)', '-ms-transform': 'rotate(' + degrees + 'deg)', 'transform': 'rotate(' + degrees + 'deg)' }); } $({ deg: 0 }).animate({ deg: 360 * 40 }, { duration: 7000, easing: "easeInOutQuart", step: function(now) { var deg = now < 6000 || now > 8000 ? now / 8 : now; rotate(deg); } }); } spin(); 
 .myClass { position: fixed; top: 30px; left: 30px; height: 200px; width: 200px; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myClass"></div> 
+5
source

You can add an element to the desired class, and then create another class, for example .effect, and write css for it in the .css file

  .effect { transform: rotate(360deg); -webkit..... -moz.... } 

and then in your .js file, write in the action or event call function

  $("class name you want to add effect to").addClass("effect"); 

Its jquery with some css in your stylesheet

+2
source

Ease of use can be used in css transitions and animations, rather than transformations. You should use transitions for this desired effect with ease in output as a function of transition time

+1
source

You can try using ease-in-out in css transitions. If you want to define a more specific animation, you can try to define your own using a tool like this: ceaser

For instance:

 transition: all 500ms cubic-bezier(0.870, 0.025, 0.130, 0.985); 

Edit: if you want to use only jQuery animation function for this, you can try using jQuery easing plugin

 animate({deg: 360 * 40}, {duration: 1000, easing: 'easeInOutCubic'}) 

See the available attenuation animation in this cheat sheet: http://easings.net/

+1
source

@ Ionică Bizău you can control the animation time by writing

  animation-timing-function: ease-in-out; 

or if you use a transition, then

  transition-timing-function: ease-in-out; 
+1
source

All Articles