css - 动画到过渡 css 属性

标签 css css-transitions css-animations

下面链接中的代码假设制作一个动画,在 5 秒后(这只是一个模拟 - 在现实世界场景中,触发器将在内容加载时),应该结束动画并继续过渡属性(点应将其大小更改为 0 并到达初始位置)。

JsFiddle code

#loading-icon {
    display: flex;
    width: 100px;
    height: 50px;
    align-items: center;
  justify-content: center;
}

#shell.done .dot {
    width: 0px;
    height: 0px;
    top: 0;
    animation: none;
    transition: 2s all;
}

.dot {
    position: relative;
    display: inline-block;
    width: 20px;
    height: 20px;
    margin: 5px;
    border-radius: 100%;
    background-color: red;
    animation-timing-function: linear;
    animation-play-state: running;
}

@keyframes bounce {
    0% {
        top: 0%;
        width: 12px;
        height: 12px;
        animation-timing-function: eas-out;
  }
  25% {
    top: -50%;
        animation-timing-function: ease-in-out;
  }
    50% {
        width: 20px;
        height: 20px;
    }
  75% {
        top: 50%;
        animation-timing-function: ease-in;
  }
    100% {
        width: 12px;
        height: 12px;
        top: 0%;
  }
}

.dot:nth-child(1) {
    animation: bounce 2s infinite;
}

.dot:nth-child(2) {
    animation: bounce 2s infinite;
    animation-delay: 0.2s;
}

.dot:nth-child(3) {
    animation: bounce 2s infinite;
    animation-delay: 0.4s;
}

知道如何实现这种行为吗?

谢谢!

最佳答案

您可以尝试使用迭代和 animationend 事件:

var x=document.querySelectorAll('.dot');
for(var i=0;i<x.length;i++) {
  x[i].addEventListener("animationend", function(e){
     setTimeout(function(a = e.target) {
        a.classList.add('done');
     },500);
  })
}
#loading-icon {
  display: flex;
  width: 100px;
  height: 50px;
  align-items: center;
  justify-content: center;
}



.dot {
  position: relative;
  display: inline-block;
  width: 12px;
  height: 12px;
  margin: 5px;
  border-radius: 100%;
  background-color: red;
  transition:0.5s;
}

.done {
  width: 0px;
  height: 0px;
}

@keyframes bounce {
  0% {
    top: 0%;
    width: 12px;
    height: 12px;
    animation-timing-function: eas-out;
  }
  25% {
    top: -50%;
    animation-timing-function: ease-in-out;
  }
  50% {
    width: 20px;
    height: 20px;
  }
  75% {
    top: 50%;
    animation-timing-function: ease-in;
  }
  100% {
    width: 12px;
    height: 12px;
    top: 0%;
  }
}

.dot:nth-child(1) {
  animation: bounce 2s;
  animation-iteration-count: 3;
}

.dot:nth-child(2) {
  animation: bounce 2s;
  animation-delay: 0.2s;
  animation-iteration-count: 3;
}

.dot:nth-child(3) {
  animation: bounce 2s;
  animation-delay: 0.4s;
  animation-iteration-count: 3;
}
<div id="shell">
  <div id="loading-icon">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>

关于css - 动画到过渡 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588788/

相关文章:

javascript - 从 JS 以编程方式使用 CSS 转换的简洁方法?

jquery - 使用jquery删除类后如何使css不忽略转换函数

html - 绝对定位的元素在动画完成后移动

css - 如何 FadeIn 父元素,然后它的子元素有轻微的延迟(仅限 CSS)

css - 如何在 HTML5 canvas 元素中放置一些输入元素?

javascript - 具有行详细信息的自适应网格

jquery - bootstrap 下拉菜单重叠包装 div

css - webkit 转换规则在 Safari 中根本不起作用

jquery - CSS 动画 : fire css animation on class active

javascript - 使用 fixed_toolbar_container 和绝对位置时,TinyMce 子菜单不会粘在工具栏上