CSS动画-文字从左向右滑动

时间:2019-03-18 17:38:36

标签: css3 css-animations

我正在尝试延迟从左向右滑动一些文本。它会从页面的中间而不是画布上显示。当文本从页面滑出时,我可以很容易地使它工作。

从屏幕中间进行操作有点不同,我缺少了一些东西。文本显示在页面加载中(应隐藏时)。我可以使用jQuery轻松做到这一点,但是我敢肯定,仅使用CSS就能做到。这就是我到目前为止所得到的

HTML:

<div class="slide-right">
  <h2>Text that will slide in from the left</h2>
</div>

CSS:

.slide-right {
   width: 100%;
   overflow: hidden;
   margin-left: 400px;
   max-width: 500px
}

.slide-right h2 {
   animation: 2s slide-right;
   animation-delay: 2s;
}

@keyframes slide-right {
    from {
       margin-left: -500px;
    }

    to {
       margin-left: 0%;
    }
 }

.slide-right {
  width: 100%;
  overflow: hidden;
  margin-left: 400px;
  max-width: 500px
}

.slide-right h2 {
  animation: 2s slide-right;
  animation-delay: 2s;
}

@keyframes slide-right {
  from {
    margin-left: -500px;
  }

  to {
    margin-left: 0%;
  }
}
<div class="slide-right">
  <h2>Text that will slide in from the left</h2>
</div>

Like this

谢谢

2 个答案:

答案 0 :(得分:1)

您可以最初隐藏文本,然后依靠forwards保留最新状态:

.slide-right {
  width: 100%;
  overflow: hidden;
  margin-left: 300px;
  max-width: 500px
}

.slide-right h2 {
  animation: 2s slide-right 2s forwards;
    transform:translateX(-100%);
}

@keyframes slide-right {
  to {
    transform:translateX(0);
  }
}
<div class="slide-right">
  <h2>Text that will slide in from the left</h2>
</div>

答案 1 :(得分:0)

您可以将margin-left的{​​{1}}的初始值设置为-500px

h2
.slide-right {
  width: 100%;
  overflow: hidden;
  margin-left: 400px;
  max-width: 500px
}

.slide-right h2 {
  margin-left: -500px;
  animation: 2s slide-right;
  animation-delay: 2s;
}

@keyframes slide-right {
  from {
    margin-left: -500px;
  }

  to {
    margin-left: 0%;
  }
}