【问题标题】:background-attachment : fixed; not working with background-position背景附件:固定;不使用背景位置
【发布时间】:2017-06-02 10:13:17
【问题描述】:

我已经发了codepen 来解释我的问题:

  • 当用户滚动时,蓝色图像应跟随用户滚动
  • 蓝色图像应粘贴在侧面部分的另一侧(右侧为左侧 | 左侧为右侧)

pb就是那个

background-attachment : fixed;

这不符合 css 规则

background-position: left 0px;

有人可以通过分叉 codepen 来帮助我向我展示一个有效的实现吗?

.wrapper {
  display: flex;
}

main {
  background-color: red;
  height: 1000px;
  max-width: 992px;
  width: 100%;
}

aside {
  min-width: 150px;
  background-color: green;
}

.left {
  background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
  background-repeat: no-repeat;
  background-position: right 0px;
  /*background-attachment: fixed; Doesn't work*/
}

.right {
  background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
  background-repeat: no-repeat;
  background-position: left 0px;
  /*background-attachment: fixed; Doesn't work*/
}
<div class="wrapper">
  <aside class="left"></aside>
  <main></main>
  <aside class="right"></aside>
</div>

【问题讨论】:

    标签: html css background-image fixed


    【解决方案1】:

    为什么会这样?

    这是按预期工作的,当您使用background-position: fixed; 时,背景是相对于视口定位的。这意味着在您的示例中,背景现在在 .right 元素之外的视口的最左侧对齐。

    您可以通过将.right 定位在下方 sn-p 中视口的左边缘来看到这一点。

    .wrapper {
      display: flex;
    }
    
    main {
      background-color: red;
      height: 1000px;
      max-width: 992px;
      width: 100%;
    }
    
    aside {
      min-width: 150px;
      background-color: green;
    }
    
    .left {
      background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
      background-repeat: no-repeat;
      background-position: right 0px;
      /*background-attachment: fixed; Doesn't work*/
    }
    
    .right {
      background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
      background-repeat: no-repeat;
      background-position: left 0px;
      background-attachment: fixed;
      order: -1;
    }
    <div class="wrapper">
      <aside class="left"></aside>
      <main></main>
      <aside class="right"></aside>
    </div>

    你能做什么?

    使用background-position: fixed; 时,无法相对于元素定位背景,但您可以使用position: fixed; 伪元素获得类似的预期结果:

    • 使用以下规则添加新选择器.left:before, .right:before
      • background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png); - 背景图片
      • background-repeat: no-repeat; - 停止背景重复
      • content: ""; - 显示伪元素所必需的
      • position: fixed; - 将伪元素设置为相对于视口固定
      • height: 100%; - 让伪元素填满整个高度
      • width: 100px; - 与背景图片的宽度相同

    .wrapper {
      display: flex;
    }
    
    main {
      background-color: red;
      height: 1000px;
      max-width: 992px;
      width: 100%;
    }
    
    aside {
      min-width: 150px;
      background-color: green;
    }
    
    .left {
      direction: rtl;
    }
    
    .left:before, .right:before {
      background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
      background-repeat: no-repeat;
      content: "";
      position: fixed;
      height: 100%;
      width: 100%;
    }
    
    .left:before {
      background-position: right top;
    }
    
    .right:before {
      background-position: left top;
    }
    
    .right div {
      position: relative;
    }
    <div class="wrapper">
      <aside class="left"></aside>
      <main></main>
      <aside class="right">
        <div>content</div>
      </aside>
    </div>

    请注意,如果您打算将其他内容放入.right,则需要在元素中添加position: relative;,以在伪元素上方设置堆叠上下文(参见sn-中的div p).

    为什么会这样?

    position: fixed; 将元素固定到相对于视口的设定位置。通过不设置bottomleftrighttop 位置,伪元素将停留在它原来的位置。背景可以以通常的方式应用于元素。

    【讨论】:

    • 非常感谢!! - 我可以设置 .right:before 的width: 100%; 吗(我不知道图像的大小)? - 我可以删除 .right:before 的background-position: 0 top; 吗(与您的解释不同,似乎没有)
    • 抱歉,.right 上的 background 规则可以删除,您应该可以更改为 width: 100%;,尽管当您开始添加额外内容时,您可能需要注意堆叠上下文。
    • 我很抱歉,但它似乎不适用于左侧:/ codepen.io/anon/pen/PjYvqm
    • 是的,这种方法不适用于左侧。我会考虑一下可以为左侧做些什么。背景图的width总是未知的吗?
    • @FrédéricGRATI 获得相同结果的一种方法是在.left - codepen.io/anon/pen/NgWqXz 中使用direction: rtl;
    【解决方案2】:

    问题是您不滚动aside,因为您滚动了body
    你应该避免这种情况,因为它没有响应,但你可以理解它

    .wrapper {
      width: 558px;
      background-color: green;
      background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png), url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
      background-repeat: no-repeat, no-repeat;
      background-position: left 47px top 0px, right 104px top 0px;
      background-attachment: fixed;
    }
    
    main {
      background-color: red;
      width: 280px;
      height: 1000px;
      margin: 0px auto;
    }
    <div class="wrapper">
      <aside class="left"></aside>
      <main></main>
      <aside class="right"></aside>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多