简体   繁体   English

为什么动画结束后元素的宽度会发生变化?

[英]Why does an element's width change after animation ends?

I'm learning jQuery, testing its functionality, and have some problem with an example I made.我正在学习 jQuery,测试其功能,并且对我制作的示例有一些问题。

https://codepen.io/MaxVelichkin/pen/pBJeZy https://codepen.io/MaxVelichkin/pen/pBJeZy

 /*remove animate2 class and assign animate1 class to target*/ $(function() { $('#trigger').on('click', function() { $('#target').removeClass('animate2'); $('#target').addClass('animate1'); }); }); /*remove animate1 class and assign animate2 class to target*/ $(function() { $('#trigger2').on('click', function() { $('#target').removeClass('animate1'); $('#target').addClass('animate2'); }); });
 /*just a container around*/ #container { margin: 10px; width: 350px; height: 350px; border: solid green 1px; } /*green button*/ #trigger { width: 50px; height: 50px; border-radius: 100%; background-color: green; margin: 20px auto; } /*red button*/ #trigger2 { width: 50px; height: 50px; border-radius: 100%; background-color: red; margin: 20px auto; } /*Target div which will be changing*/ #target { width: 100px; height: 100px; border: solid blue 1px; margin: 10px auto; } /*Keyframes for green button*/ @keyframes myfirst { 0% { background: white; width: 100px; height: 100px; border-radius: 0%; } 100% { background: blue; width: 150px; border-radius: 100%; } } /*Keyframes for red button*/ @keyframes mysecond { 0% { background: blue; width: 150px; border-radius: 100%; } 100% { background: white; width: 100px; height: 100px; border-radius: 0%; } } /*cusstom class to be assigned by green button*/ .animate1 { -webkit-animation: myfirst 3s; animation: myfirst 3s; height: 100px; background: blue; width: 150px; border-radius: 100%; margin: 10px auto; } /*cusstom class to be assigned by red button*/ .animate2 { -webkit-animation: mysecond 3s; animation: mysecond 3s; height: 100px; width: 150px; border: solid red 1px; border-radius: 0%; margin: 10px auto; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="trigger"></div> <div id="trigger2"></div> <div id="target"></div> </div>

There are:有:

  • 2 buttons, green and red. 2 个按钮,绿色和红色。
  • 2 sets of keyframes. 2组关键帧。
  • 2 custom classes which start the animation of corresponding keyframes and applies the styles from the last keyframe. 2 个自定义类,它们启动相应关键帧的动画并应用最后一个关键帧的样式。
  • target div, which style I want to change on button click.目标 div,我想在单击按钮时更改哪种样式。

When I click on the button, it assigns a class to the target div and removes the class, assigned by another button.当我单击按钮时,它会为目标 div 分配一个类并删除由另一个按钮分配的类。

Question 1: Why the width of the target div is changing back to 100px after the green button animation ends (why it doesn't remain 150px)?问题1:为什么绿色按钮动画结束后目标div的宽度变回100px(为什么没有保持150px)?

Question 2: Do I do everything right or there is a better jQuery approach?问题 2:我做的一切都正确还是有更好的 jQuery 方法?

Your #target is taking precedence since it is a id selector.您的#target优先,因为它是一个 id 选择器。 This has a width of 100px .它的宽度为100px Your class animate1 is getting overwritten, so that's why you aren't seeing 150px .你的类animate1被覆盖了,所以这就是为什么你没有看到150px

css 控制台

你可以像这样的动画编码:myfirst 3s forwords;

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

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