You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决容器hover时内部图片抖动问题?实现容器宽高放大图片稳定

解决容器hover时图片抖动的问题

问题根源:

  • 容器设置了transition: all,会让所有属性都参与过渡动画,包括用来居中的transform: translate(-50%, -50%)——当容器宽高变化时,translate的百分比计算值会同步改变,导致容器位置产生过渡动画,连带图片出现抖动。
  • 容器重复声明了display属性,可能干扰布局逻辑。

修正方案:

  1. 限制容器的过渡属性,只对width和height做动画,避免位置属性参与过渡。
  2. 清理重复的display声明,保持布局逻辑清晰。
  3. 保留图片的固定尺寸设置,确保其不受容器宽高变化影响。

修正后的完整代码:

CSS代码

@font-face {
  font-family: Hanyi;
  src: url(/HanyiSentyBrushSong.ttf);
}

:root {
  --my-font: Hanyi;
}

body {
  height: 100vh;
  margin: 0;
  background-color: #000000;
}

.image-container {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: none;
  border-radius: 50px;
  height: 400px;
  background-color: #262626;
  display: flex;
  justify-content: center;
  align-items: center;
  /* 仅过渡宽高属性,避免位置动画 */
  transition: width .6s ease-in-out, height .6s ease-in-out;
}

.image-container:hover {
  width: 350px;
  height: 450px;
  cursor: pointer;
}

img {
  object-fit: cover;
  width: 300px;
  height: auto;
  border-radius: 50px 50px 0px 0px;
}

HTML代码

<div class="image-container">
  <img src="/Ny mapp/destiny-ayodele-0xdWoqxoF_Q-unsplash.jpg" alt="image">
</div>

说明:修改后,hover容器时仅宽高平滑增大,图片的尺寸和位置保持稳定,不会出现抖动现象。

内容的提问来源于stack exchange,提问作者Chessman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:05:27