简体   繁体   English

如何在jquery中使用animate添加缓动效果?

[英]How to add easing effect with animate in jquery?

I am animating a div but i would like to give some effect to that animate so i tried like this我正在为 div 设置动画,但我想为该动画添加一些效果,所以我尝试这样

$('#slider').stop().animate({"margin-right": '0'}, 'slow','easeOutBounce');

easeOutBounce is not working for me.am i doing wrongly? easeOutBounce对我不起作用。我做错了吗? But other than that all working.但除此之外,一切正常。

I also tried jquery effect like below我也试过像下面这样的jquery效果

$('#slider').stop().animate({"margin-right": '0'});
$('#slider').effect( "bounce", "slow" );

but,Here not even first line animate function working if i use effect但是,如果我使用效果,这里甚至没有第一行动画功能工作

How to achieve bounce effect with animate?如何用animate实现反弹效果?

I know the answer has been accepted, but I find jQuery UI too bulky just for just increased easing functions.我知道答案已被接受,但我发现 jQuery UI 过于庞大,只是为了增加缓动功能。 I'd recommend using the smaller easings.net script at https://github.com/ai/easings.net .我建议使用https://github.com/ai/easings.net 上较小的 easings.net 脚本。 All you do is set the default easing function before calling jQuery's animate() (see the example).您所做的就是在调用 jQuery 的 animate() 之前设置默认的缓动函数(参见示例)。 Exclude the easing parameter from the animate() method.从 animate() 方法中排除 easing 参数。

 jQuery.easing.def = 'easeOutBounce'; $('#slider').animate({"margin-left": '200'}, 'slow');
 #slider { width:100px; height:100px; background-color:#ff0000; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script> <div id="slider"></div>

easeOutBounce effect is part of jquery UI plugin. easeOutBounce效果是 jquery UI 插件的一部分。

You have to include jquery UI too, or find an other plugin:您也必须包含 jquery UI,或者找到其他插件:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

include the following libraries on your html page在您的 html 页面上包含以下库

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

Learn more on jquery UI了解有关jquery UI 的更多信息

Use this fragment of code, and place it after jQuery script.使用这段代码,并将其放在 jQuery 脚本之后。 It is taken from official jQuery UI script.它取自官方jQuery UI脚本。 It is linked to this breaking change .它与这一重大变化有关

Do not use the complete library if you only need easing.如果您只需要缓动,请不要使用完整的库。 The minified is still >250KB缩小后仍然> 250KB

( function() {

// Based on easing equations from Robert Penner (http://www.robertpenner.com/easing)

var baseEasings = {};

$.each( [ "Quad", "Cubic", "Quart", "Quint", "Expo" ], function( i, name ) {
    baseEasings[ name ] = function( p ) {
        return Math.pow( p, i + 2 );
    };
} );

$.extend( baseEasings, {
    Sine: function( p ) {
        return 1 - Math.cos( p * Math.PI / 2 );
    },
    Circ: function( p ) {
        return 1 - Math.sqrt( 1 - p * p );
    },
    Elastic: function( p ) {
        return p === 0 || p === 1 ? p :
            -Math.pow( 2, 8 * ( p - 1 ) ) * Math.sin( ( ( p - 1 ) * 80 - 7.5 ) * Math.PI / 15 );
    },
    Back: function( p ) {
        return p * p * ( 3 * p - 2 );
    },
    Bounce: function( p ) {
        var pow2,
            bounce = 4;

        while ( p < ( ( pow2 = Math.pow( 2, --bounce ) ) - 1 ) / 11 ) {}
        return 1 / Math.pow( 4, 3 - bounce ) - 7.5625 * Math.pow( ( pow2 * 3 - 2 ) / 22 - p, 2 );
    }
} );

$.each( baseEasings, function( name, easeIn ) {
    $.easing[ "easeIn" + name ] = easeIn;
    $.easing[ "easeOut" + name ] = function( p ) {
        return 1 - easeIn( 1 - p );
    };
    $.easing[ "easeInOut" + name ] = function( p ) {
        return p < 0.5 ?
            easeIn( p * 2 ) / 2 :
            1 - easeIn( p * -2 + 2 ) / 2;
    };
} );

} )();

For additional info, this error might occur if using this legacy plugin with jQuery 3+ I'm not sure but I think it was in many Bootstrap templates.有关其他信息,如果将此旧插件与 jQuery 3+ 一起使用,可能会发生此错误我不确定,但我认为它在许多 Bootstrap 模板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM