简体   繁体   English

为什么 CSS 过渡不能很好地与基于 jQuery 的 animate 方法的另一个动画配合使用?

[英]Why does a CSS transition not work well with another animation based on jQuery's animate method?

I have a button that I wish to move when it is clicked, and while moving it should also flip.我有一个按钮,我希望在单击时移动它,并且在移动时它也应该翻转。 So there are two animations that take place at the same time, and have the same duration.所以有两个动画同时发生,并且具有相同的持续时间。 I also have a CSS transition set up for all the properties of the button.我还为按钮的所有属性设置了 CSS transition It seems to me that only the CSS scale transform works when I use both jQuery's animate method and the CSS transition effect.在我看来,当我同时使用 jQuery 的animate方法和 CSS 过渡效果时,只有 CSS 缩放转换有效。 When I remove one of the animations, the other one works well.当我删除其中一个动画时,另一个动画效果很好。

 // find elements var button = $("button"); // handle click button.on("click", function() { button.css({ "transform": "scaleX(0)" }); // the bug: the following animation is not visible button.animate({ "top": "-100px" // I tried without "px" too }, 0.15 * 1000); });
 body { background: #20262E; padding: 20px; font-family: Helvetica; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; position: relative; transition: all 0.15s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <p>Hello World</p> <button>Click me</button> </div>

The JSFiddle is here . JSFiddle 在这里

Thank you.谢谢你。

Use only the required property in transition ie transform 0.15s instead of all 0.15stransition仅使用所需的属性,即transform 0.15s而不是all 0.15s

 // find elements var button = $("button"); // handle click button.on("click", function() { button.css({ "transform": "scaleX(0)" }); // the bug: the following animation is not visible button.animate({ "top": "-100px" // I tried without "px" too }, 0.15 * 1000); });
 body { background: #20262E; padding: 20px; font-family: Helvetica; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; position: absolute; transition: transform 0.15s; /* change here*/ }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <p>Hello World</p> <button>Click me</button> </div>

When you declare your button style as transition: all 0.15s;当您将按钮样式声明为transition: all 0.15s; that is affecting the top property too.这也影响了top财产。 Therefore, in your flow:因此,在您的流程中:

  1. The top animation does happen a few ms after the css manipulation在 css 操作后的几毫秒内,顶部动画确实发生了
  2. Every step of the top animation is subject to a transition顶级动画的每一步都经过过渡

Due to the first, there's already a racing condition that may cause the top animation to go unnnoticed, but due to the second, there's also an easing over every step of jQuery's animation.由于第一个,已经存在可能导致顶部动画不被注意的竞争条件,但是由于第二个,jQuery 动画的每一步也都得到了缓和。

If not provided, $.fn.animate easing function defaults to swing which is defined as:如果没有提供, $.fn.animate缓动函数默认为swing ,定义为:

 function( p ) {
    return 0.5 - Math.cos( p * Math.PI ) / 2;
 }

Where p is between 0 and 1.其中p介于 0 和 1 之间。

In this case, you're asking jQuery to change top from 0 to -100 in 150ms.在这种情况下,您要求 jQuery 在 150 毫秒内将top从 0 更改为 -100。 Usually the animation interval is around 13ms , but let's say it's exactly 15ms so we have 10 animation steps .通常动画间隔约为 13 毫秒,但假设正好是 15 毫秒,因此我们有10 个动画步骤 Due to the swing easing, the first step amounts for:由于swing宽松,第一步相当于:

 100 * (0.5 -  Math.cos( (0.1 * Math.PI ) / 2) = 2.45

In other words, due to transition: all 0.15s and leaving the race condition apart, clicking the button would scale it horizontally to 0 and move it 2.45px upwards in 0.15s .换句话说,由于transition: all 0.15s并且将竞争条件分开,单击按钮会将其水平缩放为 0 并在 0.15s 内向上移动 2.45px

If, instead, you only transition the transform property, and leave the top transition to jQuery, you could see both of them happening in parallel.相反,如果您只转换transform属性,而将top转换留给 jQuery,您可以看到它们同时发生。

In the following snippet I slowed things down to take 1s, added console statements on each step and included the regular button, another one that only transitions transform and a third one that transitions transform and top which doesn't use jQuery animations.在下面的代码片段中,我把事情放慢了 1 秒,在每个步骤上添加了控制台语句并包括常规按钮,另一个只转换transform ,第三个转换transform和不使用 jQuery 动画的top

 // find elements var button = $("button.animate"); // handle click button.on("click", function() { console.clear(); $(this).css( "transform", (index, value) => { console.log(`top: transform was ${value}`); return "scaleX(0)"; } ); console.log('before $.fn.animate'); $(this).animate({ "top": "-100px" }, 1000, 'swing', () => { console.log('finish animation'); window.setTimeout(() => { console.log('restore initial state') $(this).css({ transform: "unset", top: 0 }); }, 2000); }); }); // handle click $("button.no_animate").on("click", function() { console.clear(); $(this).css({ "transform": "scaleX(0)", "top": "-100px" }); window.setTimeout(() => { console.log('restore everything'); $(this).css({ transform: "unset", top: 0 }); }, 2000); });
 body { background: #20262E; padding: 20px; font-family: Helvetica; } p { color: white; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; position: relative; } button.animate.all { transition: all 1s; } button.animate.transform { transition: transform 1s; } button.no_animate { top: 0; transition: top 1s, transform 1s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <p id="check">Hello World</p> <button class="animate all">I Transition all</button> <button class="animate transform">I Transition "transform" </button> <button class="no_animate transform">I don't use animate </button> </div>

Please see the code on JS fiddle for smooth animation:

https://jsfiddle.net/harshsetia/tvcb4mso/8/

your button position should bet set to absolute!您的按钮位置应该下注设置为绝对!

 var button = $("button"); // handle click button.on("click", function() { button.css({ "transform": "scaleX(0)" }); // the bug: the following animation is not visible button.animate({top: "-100px"}, 0.15 * 1000); });
 button{ position:absolute; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <p>Hello World</p> <button>Click me</button> </div>

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

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