【问题标题】:Simple line animation with keyframes带有关键帧的简单线条动画
【发布时间】:2019-04-02 10:06:14
【问题描述】:

我试图制作一个流畅的动画,但动画中间有一种“cut bug”。

我该如何解决?

div,
div:after {
  width: 0vw;
  height: 3px;
  position: fixed;
  top: 1vw; bottom: 0;
  left: 40vw; right: 40vw;
  margin: auto;
/*  margin-top: -16px;*/

  z-index: 600;
  background-color: rgba(0, 0, 0, 1);
}

div {
  /*background-color: transparent;*/
/*  border-top: 3px solid rgba(0, 0, 0, 0.1);
  border-right: 3px solid rgba(0, 0, 0, 0.1);
  border-bottom: 3px solid rgba(0, 0, 0, 0.1);
  border-left: 3px solid black;
  -webkit-transform: translateZ(0);
          transform: translateZ(0);*/
  -webkit-animation-iteration-count:infinite;
          animation-iteration-count:infinite;
  -webkit-animation-timing-function: ease-in-out;
          animation-timing-function: ease-in-out;
  -webkit-animation-direction: alternate;
          animation-direction: alternate;
  -webkit-animation-duration: 1s;
          animation-duration: 1s;
  -webkit-animation-name: animsition-loading;
          animation-name: animsition-loading;
}

@-webkit-keyframes animsition-loading {
  0% {
    /*width: 0vw;*/
    transform:translate(0vw);
    width :0vw;
      margin-left: 0;
  }

  50% {
    /*width: 0vw;*/
    /*transform:translate(5vw);*/
    width :10vw;
    
  }

  100% {
    /*width: 0vw;*/
    transform:translate(1vw);
    width :0vw;
    margin-right: 0;
  }
}
<div> </div>

【问题讨论】:

    标签: html css css-animations keyframe


    【解决方案1】:

    这是另一种用更少代码实现相同目标的方法:

    .loading {
      height: 3px;
      position: fixed;
      top: 2vw;
      left: 40vw;
      right: 40vw;
      height: 3px;
      background: linear-gradient(#000, #000) left/0% 100% no-repeat;
      animation: anime 2s ease-in-out infinite alternate;
    }
    
    @keyframes anime {
      0% {
        background-size: 0% 100%;
        background-position: left;
      }
      50% {
        background-size: 70% 100%;
      }
      100% {
        background-size: 0% 100%;
        background-position: right;
      }
    }
    <div class="loading"></div>

    【讨论】:

    • 我不想有 100% 宽度的背景,而是 70%
    • @user9018937368 我们可以轻松更改,检查更新
    【解决方案2】:

    试试这个,你就完成了...... 不要使用transform translate,只使用width。

    div,
    div:after {
      width: 0vw;
      height: 3px;
      position: fixed;
      top: 1vw; bottom: 0;
      left: 40vw; right: 40vw;
      margin: auto;
    /*  margin-top: -16px;*/
    
      z-index: 600;
      background-color: rgba(0, 0, 0, 1);
    }
    
    div {
      /*background-color: transparent;*/
    /*  border-top: 3px solid rgba(0, 0, 0, 0.1);
      border-right: 3px solid rgba(0, 0, 0, 0.1);
      border-bottom: 3px solid rgba(0, 0, 0, 0.1);
      border-left: 3px solid black;
      -webkit-transform: translateZ(0);
              transform: translateZ(0);*/
      -webkit-animation-iteration-count:infinite;
              animation-iteration-count:infinite;
      -webkit-animation-timing-function: ease-in-out;
              animation-timing-function: ease-in-out;
      -webkit-animation-direction: alternate;
              animation-direction: alternate;
      -webkit-animation-duration: 1s;
              animation-duration: 1s;
      -webkit-animation-name: animsition-loading;
              animation-name: animsition-loading;
    }
    
    @-webkit-keyframes animsition-loading {
      
    
    0% {
    
        width :0;
        left: 0;
      }
    
      50% {   
        width :10vw;
    
      }
    
      100% {    
        width :0;
        right: 0;
      }
    }
    <div> </div>

    【讨论】:

      【解决方案3】:

      尝试这样设置你的动画:

      @-webkit-keyframes animsition-loading {
        0% {
      
          width :0;
          left: 0;
        }
      
        50% {   
          width :10vw;
      
        }
      
        100% {    
          width :0;
          right: 0;
        }
      

      这就是你想要的效果吗?

      【讨论】:

        猜你喜欢
        • 2015-07-12
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多