我已经codepen解释了我的问题:
pb就是那个
background-attachment : fixed;
这不符合css规则
background-position: left 0px;
有人可以通过分配代码栈来向我展示工作实现吗?
.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>
答案 0 :(得分:1)
问题是您不能滚动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;
}
&#13;
<div class="wrapper">
<aside class="left"></aside>
<main></main>
<aside class="right"></aside>
</div>
&#13;
答案 1 :(得分:1)
当您使用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;
以将堆叠上下文设置为伪元素(请参阅{{1在代码段中。)
div
将元素固定到相对于视口的设定位置。如果未设置position: fixed;
,bottom
,left
或right
位置,则伪元素将保留原始位置。背景可以通常的方式应用于元素。