简体   繁体   English

CSS 过渡在切换时没有动画

[英]CSS transition does not animate on toggle

I made a little accordion that I would like to animate.我做了一个小手风琴,我想制作动画。 I have the following code but the animation as written is not working and I don't know why.我有以下代码,但所写的 animation 不起作用,我不知道为什么。

I know it's somehow complicated to make the height to work, but I managed to find a work around using max-height .我知道让高度起作用有点复杂,但我设法使用max-height找到了解决方法。

I would like my div to have an animated width and height to make it feel like you unfold it.我希望我的div有一个动画宽度和高度,让它感觉就像你展开它一样。

The weird behaviour is that if you resize the window, everything seems animated...奇怪的行为是,如果你调整 window 的大小,一切似乎都是动画的......

 document.getElementById("ping").onclick = function () { console.log(this); this.children[1].classList.toggle("active"); };
 .content.active { display: block; max-height: 200px; transition-property: width, max-height; transition-duration: 400ms, 100ms; transition-timing-function: cubic-bezier(0.305, 0, 0, 1.015); transition-delay: 400ms, 100ms; width: 40vw; background: orange; }.content { width: 0px; max-height: 0px; transition-property: width, max-height; transition-timing-function: cubic-bezier(0.305, 0.000, 0.000, 1.015); transition-duration: 400ms, 100ms; transition-delay: 100ms, 100ms; background: orange; display: none; }.cat.active { background: orange; }
 <section id="item0"> <div id="ping" class="cat"> <div class="title active" style=""> <a>Open</a> </div> <div class="content" style=""> <div> <div class="images"> first test <br> another </div> </div> </div> </section>

1.) Only apply the transition parameters to the initial state, not to the .active state/class. 1.) 仅将transition参数应用于初始 state,而不应用于.active状态/类。

2.) Don't use display:none and block , but instead transition between max-height: 0 and width: 0 and their ".active" values. 2.) 不要使用display:noneblock ,而是在max-height: 0width: 0及其“.active”值之间转换。

3.) To hide the overflowing contents (which would still be visible), apply overflow: hidden to the initial state 3.) 要隐藏溢出的内容(仍然可见),请将overflow: hidden应用于初始 state

 document.getElementById("ping").onclick = function () { console.log(this); this.children[1].classList.toggle("active"); };
 .content.active { max-height: 200px; width: 40vw; }.content { max-height: 0; width: 0px; transition-property: width, max-height; transition-duration: 400ms, 100ms; transition-timing-function: cubic-bezier(0.305, 0, 0, 1.015); transition-delay: 400ms, 100ms; background: orange; overflow: hidden; }.cat.active { background: orange; }
 <section id="item0"> <div id="ping" class="cat"> <div class="title active" style=""> <a>Open</a> </div> <div class="content" style=""> <div> <div class="images"> first test <br> another </div> </div> </div> </section>

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

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