【问题标题】:How to keep origin in center of image in scale animation?如何在比例动画中保持图像中心的原点?
【发布时间】:2025-12-17 07:40:01
【问题描述】:

我的情况与fiddle 类似,我有一个 CSS3 动画,可以缩放绝对定位在另一个元素中心的元素。但是,当动画发生时,它会偏离中心,如示例中相对于蓝色的红色方块所示。我如何居中?我已经围绕transform-origin 属性尝试了几种配置,但这并没有产生正确的结果。

@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 {
		animation: ripple_large 2s linear 0s infinite;
}

.two {
		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>

【问题讨论】:

    标签: html css css-animations css-transforms


    【解决方案1】:

    问题是您正在覆盖translate 转换。

    当您指定一个新的转换(动画中的那个)时,它会覆盖第一个转换。在您的情况下,您 正在删除修复 center 对齐的翻译

    您需要将它们添加到相同的 transform 属性中并注意顺序,因为它很重要 (Why does order of transforms matter? rotate/scale doesn't give the same result as scale/rotate)

    @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>

    更新

    正如评论的那样,最好使用其他方法而不是平移来使元素居中,以避免更改动画,因为这可以与其他元素一起使用。

    例子:

    @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;
      transform-origin:center;
    }
    
    .to-animate {
      position: absolute;
      top: 0;
      left: 0;
      bottom:0;
      right:0;
      margin:auto;
      border: 1px solid red;
      height: 5vmax;
      width: 5vmax;
    }
    
    .one {
      animation: ripple_large 2s linear 0s infinite;
    }
    
    .two {
      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>

    【讨论】:

    • 或者使用(在这种特定情况下margin 使元素居中,因为width/height 是固定的。如果他们想保持keyframes 与它所应用的元素无关。
    • @GabyakaG.Petrioli 是的,当然;)我专注于他们覆盖的方式并将它们添加到动画中以显示它。但我同意最好以其他方式修复对齐:)
    • 不确定我的建议是否真的更好,我只想提一个替代解决方案。
    • @GabyakaG.Petrioli 我认为总的来说更好,因为我们可以考虑使用一些外部库在变换上添加动画的情况,在这种情况下我们无法更改动画,最好更改我们元素的 CSS。
    最近更新 更多