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

如何在Safari浏览器中实现文本滚动时背景图片固定效果

Safari兼容的固定背景替代方案

直接替换原有背景相关CSS代码即可,不会锁定页面滚动,适配当前使用的w3schools模板结构:

/* 初始化基础容器属性 */
html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

/* 固定背景伪元素,替代background-attachment: fixed */
body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  /* 替换为你的树叶背景图地址 */
  background: url("./leaf-background.jpg") center / cover no-repeat;
  z-index: -1;
  /* 触发Safari硬件加速,避免滚动卡顿闪烁 */
  will-change: transform;
}

注意事项

  • 删除原有body样式中设置的background-attachment: fixed以及对应的背景属性,避免样式冲突
  • 若出现页面内容被背景遮挡的情况,给页面内容的外层容器添加position: relative; z-index: 2;即可
  • 该方案兼容所有Safari版本,不会出现背景重复、无法铺满全屏的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 04:45:02