Make a rotate animation: start and end slowly, but

2019-04-29 06:29发布

问题:

I want to have a spinning animation applied to an element: the rotation should start slowly and then become faster and faster, then it will reach a point from where it will continue to be very fast and then very slowly go slower and slower until it will stop.

The graph would look like this:

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

How can I apply this path to the jQuery animate function?

Currently I 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 it should have a smoother slowing down. How can I do that?

回答1:

Try using jQuery Easing easeInOutQuart function ; if this.deg : now parameter of step function is less than 6000 or greater than 8000 , set 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>



回答2:

You can add in your desired element class and then make an another class like .effect and write css for it in .css file

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

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

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

Its jquery with some css in your stylesheet



回答3:

Ease-in-out can be used in css transitions and animations , not in transforms. You should use transitions for that desired effect with ease-in-out in transition-timing-function



回答4:

@ Ionică Bizău you can control animation timing by writing

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

or if you are using a transition then

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


回答5:

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 by using a tool like this one: ceaser

For example:

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

Edit: If you want to only use the animate function of jQuery for this, you can try to use the jQuery easing plugin

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

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