简体   繁体   English

如何设置比例淡入淡出反向?

[英]How to set scale fade in fade out in the reverse direction?

Greeting.问候。 I already have the fade in animation.我已经有 animation 的淡入淡出。 When my dialog or box opens the animation is good.当我的对话框或框打开时,animation 很好。 However, the problem is that this does not happen when the animation is turned off at the click of the X button.但是,问题是,当 animation 在单击 X 按钮时关闭时,不会发生这种情况。 I want what happens now on fade in, happens on fade out too.我想要现在在淡入时发生的事情,在淡出时也发生。 In short when my dialog box pops up, animation pops up good But I want to exit the dialog so that the animation is also scale down.简而言之,当我的对话框弹出时,animation 弹出好但是我想退出对话框,以便 animation 也按比例缩小。 Code:代码:

html html

  <div class="modal-dialog">
       any content
       // button which close animation
    <button type="button" class="close" (click)="closeDialog()">
       x
   </button>
 </div>

css css

.modal.fade .modal-dialog {
  animation-name: fade-in-scale-animation;
  animation-duration: 0.3s;
}

@keyframes fade-in-scale-animation {
  0%   {transform: scale(0.5); opacity : 0 }
  100% {transform: scale(1); opacity : 1}
}

EDIT:编辑:

Check this https://stackblitz.com/edit/angular-66n3nl I want on close animation same as when a open?检查这个https://stackblitz.com/edit/angular-66n3nl我想在关闭时 animation 与打开时相同吗?

You may apply a second class which has the animation, but dynamically.您可以应用第二个 class,它具有 animation,但是是动态的。

 const fadeInOutContainer = document.querySelector('#modal'); const fadeOut = () => { fadeInOutContainer.classList.add('fade-out-container'); }
 .fade-in-container { background: black; width: 200px; height: 200px; animation: fade-in 0.3s; }.fade-out-container { animation: fade-out 0.3s; } @keyframes fade-in { 0% { opacity: 0; } 100% { opacity: 1; } } @keyframes fade-out { 0% { opacity: 1; } 100% { opacity: 0; } }
 <div class="fade-in-container" id="modal"> <.-- Content here... --> </div> <button onclick="fadeOut()">Fade out</button>

This is just a sample for the sake of example.这只是一个示例。 The idea is there, adding a new class to the container itself.想法就在那里,将新的 class 添加到容器本身。

I suppose the modal will not be displayed once the animation ends, that's why I leave this with the class on it.我想一旦 animation 结束,模态将不会显示,这就是为什么我留下 class 的原因。 If this is not the case and the HTML element will be recycled for some reason, you should remove the dynamic class later.如果不是这种情况,并且 HTML 元素将由于某种原因被回收,您应该稍后删除动态 class。

Edit on demand If you define an HTML Element with a class, the styles whitin that class will be applied by the time the element is just rendered.按需编辑如果您使用 class 定义 HTML 元素,则 styles 会在 ZA2F2ED4F8EBC02CBB4C21A29 时应用元素4AB4C21Z。

*.html *.html

<div class="test">Just for the sake of example</div>

*.css *.css

.test { border: 1px solid red; }

Here, the container above will have a red border by the time the page loads (or the container displays).在这里,当页面加载(或容器显示)时,上面的容器将有一个红色边框。
Talking about animations, if we have谈论动画,如果我们有

.test {
   border: 1px solid red;
   animation: anim 1s;
}

@keyframes anim {
   0% { opacity: 0 }
   100% { opacity: 1 }
}

The animation anim will be triggered at the same time the styles whitin the test class are being applied. animation anim将在 styles 应用test class 的同时被触发。
That is why you can't apply fade-in-container and fade-out-container to the container in your HTML file.这就是为什么您不能将fade-in-containerfade-out-container容器应用于 HTML 文件中的容器的原因。

What you want is to trigger the fade-in animation first (whenever the container is rendered), and that is why the container is defined with that class.您想要的是首先触发fade-in animation(无论何时渲染容器),这就是为什么使用该 class 定义容器的原因。
However, you want the fade-out animation to trigger whenever you want, with an event.但是,您希望fade-out animation 随时触发事件。 Like you said, it must trigger whenever the button X is clicked.就像你说的,它必须在点击按钮X时触发。
So, you need to listen the click event of that button, then trigger the fade-out animation.所以,你需要监听那个按钮的点击事件,然后触发fade-out animation。

When the fade-out animation will be triggered?什么时候会触发fade-out animation? Whenever you apply the fade-out-container class to the container.每当您将fade-out-container class 应用于容器时。

So, to summerize, in the *.html因此,总结一下,在 *.html

<div class="fade-in-container id="modal">
   <!-- Only the fade-in-container class -->
</div>
<button id="btn">Fade out</button>

and the script should look like something like *.js并且脚本应该看起来像 *.js

const container = document.querySelector('#modal');
const btn = document.querySelector('#btn');

btn.addEventListener(() => {
   // By adding the class, the animation will be triggered
   container.classList.add('fade-out-container');
});

Hope it clarifies a bit.希望它澄清一点。

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

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