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

如何用CSS实现默认固定、遇上方元素才下移的交互型图片?

实现图片的自适应底部定位需求

使用CSS的position: sticky属性可以完美实现你要的效果,具体实现如下:

核心样式

.sticky-bottom-img {
  position: sticky;
  /* 距离底部30vh,等价于距离顶部70%视口高度的位置 */
  bottom: 30vh;
  /* 避免图片被拉伸或错位 */
  align-self: flex-start;
}

完整测试示例

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      margin: 0;
      padding: 20px;
      /* 给页面足够高度方便测试滚动效果 */
      min-height: 200vh;
    }
    .top-section {
      /* 可修改这个高度测试:小于30vh时图片固定在70%位置;大于则被挤下移 */
      height: 25vh;
      background: #f0f0f0;
      margin-bottom: 10px;
    }
    .sticky-bottom-img {
      position: sticky;
      bottom: 30vh;
      align-self: flex-start;
      width: 300px;
      height: auto;
      border: 1px solid #ddd;
    }
  </style>
</head>
<body>
  <div class="top-section">上方测试内容</div>
  <img src="你的图片URL" class="sticky-bottom-img" alt="需要定位的图片">
</body>
</html>

效果说明

  • 当上方内容高度不足30vh时,图片会稳定在距离视口顶部70%的位置,不会随上方内容增加而移动;
  • 当上方内容高度超过30vh、接触到图片时,图片会跟随内容向下滚动,不会被上方内容覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 09:25:01