首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分别为文本的字符设置动画

分别为文本的字符设置动画
EN

Stack Overflow用户
提问于 2017-07-27 18:16:32
回答 3查看 55关注 0票数 1

我正在尝试将一行文本中的字符分别设置为动画。我想实现的是让它们飞起来,一次一个地显示在屏幕上。我知道我可以使用转换和转换来做到这一点: translateX。但我有几个问题。首先,文本的外部div没有包装所有字符,其次,对其中一个跨度应用transform: translateX不起作用。我已经创建了一个小演示,供您检查。谢谢。

代码语言:javascript
复制
#burger_container {
  background-color: #404041;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 60px;
  z-index: 101;
}

svg {
  margin: 20px auto 0 auto;
  display: block;
}

#logo_text {
  transform: rotate(-90deg);
  margin-top: 350px;
  font-size: 40px;
  color: #ffffff;
  font-weight: 700;
}

#logo_text span {
  font-weight: 400;
}

#logo_text span:nth-child(1){
  transform: translatex(-50px);
}
代码语言:javascript
复制
<div id="burger_container">
  <div>
    <svg id="button" style="height: 26px; width: 26px;">
      <g style="" fill="#f04d43">
        <rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
      </g>
    </svg>
    
      <div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span>&nbsp;<span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>

https://jsfiddle.net/a3x4ykza/

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-27 19:18:17

你已经使用了span标签,默认情况下是inline元素,现在如果我改变它,然后它breaks它如何对齐的形式,你可以改为positionrelative,然后使用CSS animation,你可以使用breaks属性逐个获取每个文本,如下所示,

选择每个span标签,然后如上所述使用CSS动画。

代码语言:javascript
复制
#logo_text span:nth-of-type(8) {
  animation: mv 1s ease forwards;
}

#logo_text span:nth-of-type(9) {
  animation: mv 1s ease forwards 1s;
}

#logo_text span:nth-of-type(10) {
  animation: mv 1s ease forwards 2s;
}

#logo_text span:nth-of-type(11) {
  animation: mv 1s ease forwards 3s;
}

#logo_text span:nth-of-type(12) {
  animation: mv 1s ease forwards 4s;
}

#logo_text span:nth-of-type(13) {
  animation: mv 1s ease forwards 5s;
}

#logo_text span:nth-of-type(14) {
  animation: mv 1s ease forwards 6s;
}

#logo_text span:nth-of-type(15) {
  animation: mv 1s ease forwards 7s;
}

#logo_text span:nth-of-type(16) {
  animation: mv 1s ease forwards 8s;
}

@keyframes mv {
  from {
    top: 0;
  }
  to {
    top: -50px;
  }
}

代码语言:javascript
复制
#burger_container {
  background-color: #404041;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 60px;
  z-index: 101;
}

svg {
  margin: 20px auto 0 auto;
  display: block;
}

.line1,
.line2,
.line3 {
  transition: all 0.3s ease;
}

.open1 {
  transform-origin: top left;
  transform: translatex(3px) translatey(-1px) rotate(45deg);
  width: 33px;
}

.open2 {
  opacity: 0;
}

.open3 {
  transform-origin: bottom left;
  transform: translatex(3px) translatey(1px) rotate(-45deg);
  width: 33px;
}

#trans_overlay {
  display: none;
}

#logo_two {
  display: none;
}

#logo_text {
  transform: rotate(-90deg);
  margin-top: 300px;   /*Change this back to 350px, just to see output I have change it to 300px*/
  font-size: 40px;
  color: #ffffff;
  font-weight: 700;
}

#logo_text span {
  font-weight: 400;
  position: relative;
}

#logo_text span:nth-of-type(8) {
  animation: mv 1s ease forwards;
}

#logo_text span:nth-of-type(9) {
  animation: mv 1s ease forwards 1s;
}

#logo_text span:nth-of-type(10) {
  animation: mv 1s ease forwards 2s;
}

#logo_text span:nth-of-type(11) {
  animation: mv 1s ease forwards 3s;
}

#logo_text span:nth-of-type(12) {
  animation: mv 1s ease forwards 4s;
}

#logo_text span:nth-of-type(13) {
  animation: mv 1s ease forwards 5s;
}

#logo_text span:nth-of-type(14) {
  animation: mv 1s ease forwards 6s;
}

#logo_text span:nth-of-type(15) {
  animation: mv 1s ease forwards 7s;
}

#logo_text span:nth-of-type(16) {
  animation: mv 1s ease forwards 8s;
}

@keyframes mv {
  from {
    top: 0;
  }
  to {
    top: -50px;
  }
}
代码语言:javascript
复制
<div id="burger_container">
  <div>
    <svg id="button" style="height: 26px; width: 26px;">
      <g style="" fill="#f04d43">
        <rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
      </g>
    </svg>
  </div>

  <div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span>&nbsp;<span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2017-07-27 18:56:41

#logo_text做成flexbox,让spandisplay:inline-block就行了。不过,您必须使用Y轴。

代码语言:javascript
复制
#burger_container {
  background-color: #404041;
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 60px;
  z-index: 101;
}

svg {
  margin: 20px auto 0 auto;
  display: block;
}

#logo_text {
  transform: rotate(-90deg);
  margin-top: 350px;
  font-size: 40px;
  color: #ffffff;
  font-weight: 700;
  display: flex;
}

#logo_text span {
  font-weight: 400;
  display: inline-block;
}

#logo_text span:nth-child(1){
  transform: translatex(-50px);
}
代码语言:javascript
复制
<div id="burger_container">
  <div>
    <svg id="button" style="height: 26px; width: 26px;">
      <g style="" fill="#f04d43">
        <rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
        <rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
      </g>
    </svg>
    
      <div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span>&nbsp;<span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2017-07-27 19:16:20

如果JavaScript是你的选择,你可以使用它。仅在此部分更改css:

代码语言:javascript
复制
#logo_text span{
    position:relative;
}

然后在最后将这部分包含在你的身体里。

代码语言:javascript
复制
<script>
       var span_texts=document.getElementById("logo_text").getElementsByTagName("span");
       for(var i=0;i<span_texts.length;i++){
            (function(j){
                var elem=span_texts[j];
                setTimeout(function(){
                elem.setAttribute("style","top:-70px");
                },300*j+300);
            })(i);
       }
</script>

您可以看到文本离开屏幕(向左),一个time.The动画开始于300ms,随后的每个字母在每300ms后离开屏幕。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45347855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档