代码之家  ›  专栏  ›  技术社区  ›  teraspora

动画似乎禁用了转换[复制]

  •  0
  • teraspora  · 技术社区  · 7 年前

    我有类似的情况 fiddle ,其中我有一个CSS3动画,它缩放一个元素,该元素绝对位于另一个元素的中心。但是,当动画发生时,它偏离了中心,如示例中相对于蓝色的红色方块所示。我该如何把它放在中心?我尝试了一些配置 transform-origin 属性,但这无法生成正确的结果。

    代码如下:

    @-webkit-keyframes ripple_large {
      0% {-webkit-transform:scale(1);}
      75% {-webkit-transform:scale(3); opacity:0.4;}
      100% {-webkit-transform:scale(4); opacity:0;}
    }
    
    @keyframes ripple_large {
      0% {transform:scale(1); }
      75% {transform:scale(3); opacity:0.4;}
      100% {transform:scale(4); opacity:0;}
    }
    
    .container {
      position: relative;
      display: inline-block;
      margin: 10vmax;
    }
    
    .cat {
      height: 20vmax;
    }
    
    .center-point {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 10px;
      width: 10px;
      background: blue;
    }
    
    .to-animate {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid red;
      height: 5vmax;
      width: 5vmax;
      transform-origin:center;
    }
    
    .one {
      -webkit-animation: ripple_large 2s linear 0s infinite;
      animation: ripple_large 2s linear 0s infinite;
    }
    
    .two {
      -webkit-animation: ripple_large 2s linear 1s infinite;
      animation: ripple_large 2s linear 1s infinite;
    }
    
    0 回复  |  直到 6 年前
        1
  •  7
  •   Temani Afif    6 年前

    问题是你正在删除 translate 转型。

    当您指定一个新的转换(动画中的转换)时, 覆盖 第一个,所以您需要将它们添加到同一个 transform 属性。就你而言 正在删除翻译 那是修复 中心 对齐方式:

    @keyframes ripple_large {
      0% {
        transform: translate(-50%, -50%) scale(1) ;
      }
      75% {
        transform: translate(-50%, -50%) scale(3) ;
        opacity: 0.4;
      }
      100% {
        transform:translate(-50%, -50%)  scale(4) ;
        opacity: 0;
      }
    }
    
    .container {
      position: relative;
      display: inline-block;
      margin: 10vmax;
    }
    
    .cat {
      height: 20vmax;
    }
    
    .center-point {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 10px;
      width: 10px;
      background: blue;
      transform-origin:center;
    }
    
    .to-animate {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid red;
      height: 5vmax;
      width: 5vmax;
    }
    
    .one {
      -webkit-animation: ripple_large 2s linear 0s infinite;
      animation: ripple_large 2s linear 0s infinite;
    }
    
    .two {
      -webkit-animation: ripple_large 2s linear 1s infinite;
      animation: ripple_large 2s linear 1s infinite;
    }
    <div class='container'>
      <img src='http://www.catster.com/wp-content/uploads/2017/08/Pixiebob-cat.jpg' class='cat'>
      <div class='center-point'>
      </div>
      <div class='to-animate one'></div>
      <div class='to-animate two'></div>
    </div>

    更新

    如前所述,最好使用其他方法将元素居中,而不是转换,以避免更改动画,因为这可以与其他元素一起使用。

    推荐文章