背景附件:已修复;不使用背景位置
background-attachment : fixed; not working with background-position
我做了一个codepen来解释我的问题:
- 当用户滚动时,蓝色图像应跟随用户滚动
- 蓝色图片要贴在旁边部分的反面(右为左|左为右)
pb 是
background-attachment : fixed;
这个 css 规则不起作用
background-position: left 0px;
有人可以通过 fork 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>
问题是你不滚动 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>
为什么会这样?
这是按预期工作的,当您使用 background-position: fixed;
背景是相对于视口定位的。这意味着在您的示例中,背景现在在 .right
元素之外的视口最左侧对齐。
您可以通过将 .right
定位在下面代码段中视口的左边缘来看到这一点。
.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;
以设置伪元素上方的堆叠上下文(请参阅代码段中的 div
)。
为什么这样做?
position: fixed;
将元素固定到相对于视口的设定位置。通过不设置 bottom
、left
、right
或 top
位置,伪元素将停留在它最初定位的位置。背景可以按照通常的方式应用于元素。
我做了一个codepen来解释我的问题:
- 当用户滚动时,蓝色图像应跟随用户滚动
- 蓝色图片要贴在旁边部分的反面(右为左|左为右)
pb 是
background-attachment : fixed;
这个 css 规则不起作用
background-position: left 0px;
有人可以通过 fork 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>
问题是你不滚动 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>
为什么会这样?
这是按预期工作的,当您使用 background-position: fixed;
背景是相对于视口定位的。这意味着在您的示例中,背景现在在 .right
元素之外的视口最左侧对齐。
您可以通过将 .right
定位在下面代码段中视口的左边缘来看到这一点。
.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;
以设置伪元素上方的堆叠上下文(请参阅代码段中的 div
)。
为什么这样做?
position: fixed;
将元素固定到相对于视口的设定位置。通过不设置 bottom
、left
、right
或 top
位置,伪元素将停留在它最初定位的位置。背景可以按照通常的方式应用于元素。