CSS动画延迟不会在动画开始前延迟

时间:2020-08-07 04:02:22

标签: html css css-animations

我的网站上有一个简单的CSS动画,我希望动画在两秒钟后开始。我尝试使用动画延迟,但无法正常工作。请让我知道我在做什么错。

.type-animation {
  box-shadow: .6em 0 0 #00CCC7;
  overflow: hidden;
  white-space: nowrap;
}

h1.type-animation {
  width: 10ch;
  animation-delay: 2s;
  animation: cursor .5s step-end infinite alternate,
             type 1.5s steps(10, end)
}


@keyframes type {
  0% {
    width: 0;
  }
}

@keyframes cursor {
  50% {
    box-shadow: .6em 0 0 transparent;
  }
}
<body>
  <h1 class="type-animation">A Website.</h1>
</body>

2 个答案:

答案 0 :(得分:0)

您正在使用下面的animation-delay: 2s;速记规则覆盖animation

animation-delay规则之后将animation 移动

h1.type-animation {
  width: 10ch;
  animation: cursor .5s step-end infinite alternate,
             type 1.5s steps(10, end);
  animation-delay: 2s;
}

,如下面的代码片段所示,延迟将起作用:

.type-animation {
  box-shadow: .6em 0 0 #00CCC7;
  overflow: hidden; 
  white-space: nowrap;
}

h1.type-animation {
  width: 10ch;
  animation: cursor .5s step-end infinite alternate,
             type 1.5s steps(10, end);
  animation-delay: 2s;
}


@keyframes type {
  0% {
    width: 0;
  }
}

@keyframes cursor {
  50% {
    box-shadow: .6em 0 0 transparent;
  }
}
<body>
  <h1 class="type-animation">A Website.</h1>
</body>

但是我的猜测不是您想要的结果!我想您还希望在动画开始之前隐藏元素。

这些是您需要在元素本身中添加的行:

h1.type-animation {
  /* 1. Start with the element hidden */
  visibility: hidden;  
   /* 2. This keeps it visible after the animation ends (when visibility is on in the last keyframe) */
  animation-fill-mode: forwards;
}

...以及关键帧中:

@keyframes type {
  0% { 
    /* 3. Turn visibility on when animation starts */
    visibility: visible;
  }
  100% {
    /* 4. This along animation-fill-mode will keep visibility after animation ENDS */
    visibility: visible;    
  }
}

看到它正常工作:

.type-animation {
  box-shadow: .6em 0 0 #00CCC7;
  overflow: hidden; 
  white-space: nowrap;
}

h1.type-animation {
  visibility: hidden;
  width: 10ch;
  animation: cursor .5s step-end infinite alternate,
             type 3s steps(10, end);
  animation-delay: 2s;
  animation-fill-mode: forwards;
}

@keyframes type {
  0% {
    width: 0;
    visibility: visible;
  }
  100%{
    visibility: visible;
  }
}

@keyframes cursor {
  50% {
    box-shadow: .6em 0 0 transparent;
  }
}
<body>
  <h1 class="type-animation"><span>A Website.</span></h1>
</body>

答案 1 :(得分:0)

如果要创建键入效果,则您上面提到的代码段方法不是理想且高效的方法。

这是一个打字效果代码段,动画延迟为2秒。 我在javascript中插入了一个简单的setTimeout函数,使用该函数会在加载DOM时触发该函数,因此该文本将不会事先可见,而仅在预定动画开始2秒后可见。

window.addEventListener('load',(event)=>{
    
  const timer = document.querySelector('.type');
    setTimeout(function(){
      timer.style.opacity = 1;
  },2000);
  

})
/* GLOBAL STYLES */
body {
  background: #333;
  padding-top: 5em;
  display: flex;
  justify-content: center;
}

/* DEMO-SPECIFIC STYLES */
.type{
  color: #fff;
  font-family: monospace;
  overflow: hidden; /* Ensures the content is not revealed until the animation */
  border-right: .15em solid orange; /* The typwriter cursor */
  white-space: nowrap; /* Keeps the content on a single line */
  margin: 0 auto; /* Gives that scrolling effect as the typing happens */
  letter-spacing: .15em; /* Adjust as needed */
  animation: 
    typing 3.5s steps(30, end),
    blink-caret .5s step-end infinite;
 animation-delay: 2s;
 opacity:0;
}

/* The typing effect */
@keyframes typing {
  from { width: 0 }
  to { width: 100% }
}

/* The typewriter cursor effect */
@keyframes blink-caret {
  from, to { border-color: transparent }
  50% { border-color: orange }
}
<div class="typewriter">
  <h1 class="type">A website.</h1>
</div>