提问者:小点点

添加多个可变字并将文本居中


在这个动画中,我模拟打字机(只有HTML/CSS),我想添加4个更多的变量书写,但我不能,因为我不明白如何工作与关键帧。 我试过了,但他同时写了几个字。

第二个问题是,我不能确切地把“嗨,我是A”放在中间,当它必须写变量词的时候,让它移到左边。 (示例:https://codepen.io/sheikh_ishaan/pen/lyeoqjd)

null

.box_type{
  margin-left: auto;
  margin-right: auto;
  width: 30em;
}

h1.type {
  font-size: 30px;
  display:flex;
  align-items: baseline;
}

.text_1 {
  animation: text1;
}

.text_2 {
  animation: text2;
}

.text_1, .text_2 {
  overflow: hidden;
  white-space: nowrap;
  display: inline-block;
  position: relative;
  animation-duration: 20s;
  animation-timing-function: steps(25, end);
  animation-iteration-count: infinite;
}

.text_1::after, .text_2::after {
  content: "|";
  position: absolute;
  right: 0;
  animation: caret infinite;
  animation-duration: 1s;
  animation-timing-function: steps(5, end);
}

@keyframes text2 {
  0%, 50%, 100% {
    width: 0;
  }
  
  60%, 90% {
    width: 6.50em;
  }
}

@keyframes text1 {
  0%, 50%, 100% {
    width: 0;
  }
  10%, 40% {
    width: 8em;
  }
}

@keyframes caret {
  0%, 100% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
}
<div class="box_type">
  <h1 class="type">Hi, i'm a <span class="text_1">&nbsp;Graphic Designer</span><span class="text_2">&nbsp;Photographer</span></h1>
</div>

null


共2个答案

匿名用户

null

.box_type{
  margin-left: auto;
  margin-right: auto;
  width: 30em;
}

h1.type {
  font-size: 30px;
  display:flex;
  align-items: baseline;
  justify-content: center;
}

.text_1 {
  animation: text1;
}

.text_2 {
  animation: text2;
}

.text_3 {
  animation: text3;
}

.text_4 {
  animation: text4;
}

.text_1, .text_2, .text_3, .text_4 {
  overflow: hidden;
  white-space: nowrap;
  display: inline-block;
  position: relative;
  animation-duration: 20s;
  animation-timing-function: steps(25, end);
  animation-iteration-count: infinite;
}

.text_1::after, .text_2::after, .text_3::after, .text_4::after {
  content: "|";
  position: absolute;
  right: 0;
  animation: caret infinite;
  animation-duration: 1s;
  animation-timing-function: steps(5, end);
}

@keyframes text1 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }
  5%, 20% {
    width: 8em;
  }
}

@keyframes text2 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }
  30%, 45% {
    width: 6.5em;
  }
}

@keyframes text3 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }
  
  55%, 70% {
    width: 6.75em;
  }
}

@keyframes text4 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }
  80%, 95% {
    width: 3em;
  }
}

@keyframes caret {
  0%, 100% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
}
<div class="box_type">
  <h1 class="type">Hi, i'm a 
  <span class="text_1">&nbsp;Graphic Designer</span>
  <span class="text_2">&nbsp;Photographer</span>
  <span class="text_3">&nbsp;Web developer</span>
  <span class="text_4">&nbsp;Artist</span>
  </h1>
</div>

匿名用户

步骤2详细说明

Usage-要加减的%值定义光标改变单词的时间

(i)正如你已将你的持续时间除以编号。 你需要4个单词,然后你必须为每个类分配一个范围,就像我给.text-1分配了0%-25%。

~步骤(i)的含义-您正在指定该特定单词动画的时间限制。

ii)现在在这个范围内添加和减去一个固定值,就像在@keyframes.text_1的下一行中,我在下限上添加5%,在上限上减去5%,ex-(0+5)%,(25-5)%

前-

@keyframes text1 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }
  5%, 20% {
    width: 8em;
  }
}

~步骤(ii)的含义-你的动画将在总时间的5%-20%的范围内发生。。。 为了使动画流畅运行,需要这样的加减法。

(iii)类似地,在为每个类指定一个指定的持续时间范围后,对每个类执行同样的操作。

text_3为例,其用量为50%-75%,因此其平稳运行的极限为-(50+5)%-(75-5)%=55%-70%-

@keyframes text3 {
  0%, 25%, 50%, 75%, 100% {
    width: 0;
  }

  55%, 70% {
    width: 6.75em;
  }
}

现在,最后8个元素的代码。。。 希望你现在能更好地理解它!!

null

.box_type{
  margin-left: auto;
  margin-right: auto;
  width: 30em;
}

h1.type {
  font-size: 30px;
  display:flex;
  align-items: baseline;
  justify-content: center;
}

.text_1 {
  animation: text1;
}

.text_2 {
  animation: text2;
}

.text_3 {
  animation: text3;
}

.text_4 {
  animation: text4;
}

.text_5 {
  animation: text5;
}

.text_6 {
  animation: text6;
}

.text_7 {
  animation: text7;
}

.text_8 {
  animation: text8;
}

.text_1, .text_2, .text_3, .text_4, 
.text_5, .text_6, .text_7, .text_8 {
  overflow: hidden;
  white-space: nowrap;
  display: inline-block;
  position: relative;
  animation-duration: 20s;
  animation-timing-function: steps(25, end);
  animation-iteration-count: infinite;
}

.text_1::after, .text_2::after, .text_3::after, .text_4::after, 
.text_5::after, .text_6::after, .text_7::after, .text_8::after {
  content: "|";
  position: absolute;
  right: 0;
  animation: caret infinite;
  animation-duration: 1s;
  animation-timing-function: steps(5, end);
}

@keyframes text1 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  2.5%, 10% {
    width: 8em;
  }
}

@keyframes text2 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  15%, 22.5% {
    width: 6.5em;
  }
}

@keyframes text3 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  
  27.5%, 35% {
    width: 6.75em;
  }
}

@keyframes text4 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  40%, 47.5% {
    width: 3em;
  }
}

@keyframes text5 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  52.5%, 60% {
    width: 4em;
  }
}

@keyframes text6 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  65%, 72.5% {
    width: 4em;
  }
}

@keyframes text7 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  77.5%, 85% {
    width: 4em;
  }
}

@keyframes text8 {
  0%, 12.5%, 25%, 37.5%, 50%, 62.5%, 75%, 87.5%, 100% {
    width: 0;
  }
  90%, 97.5% {
    width: 4em;
  }
}

@keyframes caret {
  0%, 100% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
}
<div class="box_type">
  <h1 class="type">Hi, i'm a 
  <span class="text_1">&nbsp;Graphic Designer</span>
  <span class="text_2">&nbsp;Photographer</span>
  <span class="text_3">&nbsp;Web developer</span>
  <span class="text_4">&nbsp;Artist</span>
  <span class="text_5">&nbsp;Word 5</span>
  <span class="text_6">&nbsp;Word 6</span>
  <span class="text_7">&nbsp;Word 7</span>
  <span class="text_8">&nbsp;Word 8</span>
  </h1>
</div>

相关问题