简体   繁体   English

如何使 classList.toggle 遵循类的 CSS

[英]How to make classList.toggle follow class's CSS

I have a CSS class:我有一个 CSS 类:

.do-flip {
  transform: rotateX(180deg);
  transform-style: preserve-3d;
  transition: transform 0.8s, -webkit-transform 0.8s;
}

When I click the button that results in this class being added to the element, the card flips, and when I click the button the back side that results in the classList.toggle, it unflips.当我单击导致此类被添加到元素的按钮时,卡片翻转,当我单击导致 classList.toggle 的背面按钮时,它会取消翻转。

What it does not do is unflip (flip back) using the same transform-style and transition effect as the original flip.它不做的是使用与原始翻转相同的变换样式和过渡效果来取消翻转(向后翻转)。 It simply presents the front instantaneously.它只是瞬间呈现正面。

That's because you have removed the class containing the transition coding.那是因为您已经删除了包含转换编码的类。 You'll need to either set the default class to have the transition you want on returning or create a new undo-flip class and classList.replace('do-flip', 'undo-flip') and vice versa.您需要设置默认类以在返回时具有您想要的过渡,或者创建一个新的撤销翻转类和classList.replace('do-flip', 'undo-flip') ,反之亦然。

 const card = document.querySelector('.card').addEventListener('click', (e) => { e.target.classList.toggle('do-flip'); });
 .card { transform-style: preserve-3d; transition: transform 0.8s, -webkit-transform 0.8s; } .do-flip { transform: rotateX(180deg); }
 <div class="card">card-flip</div>

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

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