css动画移动元素位置

时间:2017-11-09 17:52:33

标签: css animation css-position positioning

我的CSS动画有这个问题。我有一个元素位置绝对居中在页面中间,当我打开动画时,它向右移动,当动画结束时,它移回到页面的中间。这是代码:

@keyframes motto
 from
  opacity: 0
  transform: translate3d(0, -100%, 0)
 to
  opacity: 1
  transform: none

#home
 .motto
  position: absolute
  top: 50%
  left: 50%
  margin-right: -50%
  transform: translate(-50%, -50%)
  animation-name: motto
  animation-duration: 2s
  h1
   margin: 0
   font-size: 42px
   font-weight: 100
   opacity: .5
   -webkit-animation-duration: 2s
提前谢谢!

     @keyframes motto {
       from {
         opacity: 0;
         transform: translate3d(0, -100%, 0);
       }
       to {
         opacity: 1;
         transform: none;
       }
    }
    .motto {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-right: -50%;
      transform: translate(-50%, -50%);
      animation-name: motto;
      animation-duration: 2s;
    }
    .motto h1 {
      margin: 0;
      font-size: 42px;
      font-weight: 100;
      opacity: .5
      -webkit-animation-duration: 2s;
      }
<div class="motto"><h1>css <span>animation</span></h1></div>

1 个答案:

答案 0 :(得分:0)

.motto的CSS中使用以下行:

animation-fill-mode: forwards;

这会将元素设置为在动画的最后一帧中显示。

&#13;
&#13;
@keyframes motto {
       from {
         opacity: 0;
         transform: translate3d(0, -100%, 0);
       }
       to {
         opacity: 1;
         transform: none;
       }
    }
    .motto {
      position: absolute;
      width: 400px;
      left: 50%;
      top: 50%;
      margin-left: -200px;
      animation-name: motto;
      animation-duration: 2s;
      animation-fill-mode: forwards;
    }
    .motto h1 {
      margin: 0;
      font-size: 42px;
      font-weight: 100;
      opacity: .5;
      text-align: center;
      }
&#13;
<div class="motto"><h1>css <span>animation</span></h1></div>
&#13;
&#13;
&#13;