我想使div的背景固定,这样切换div会产生像手机屏幕上切换图片的效果。这是一个更糟糕的版本,类似于这个链接: https://www.android.com/versions/nougat-7-0/
用这种方式尝试中:链接。
但是,由于图片始终比div大,并且不在我需要的div内部,所以没有运气,而且更麻烦的是,当浏览器大小改变时,背景位置也会改变。 我知道这是因为背景附件被固定,因此它看起来像一个整个视口,而不仅仅是一个div,但是否有任何解决方法可以实现这一点?因此,目标是使背景图像在母div中心位置上得到很好的修饰,但在滚动和切换效果时保持固定位置。 更改浏览器视口时,图片始终以相同的大小位于母div的中心位置。
我知道有关"scroll magic"的信息,但我只想要通过CSS或最少量的JS代码获得更“自然”的东西。
用这种方式尝试中:链接。
.main {
min-height: 1000px;
}
.col1 {
width: 29.9%;
min-height: 800px;
display: inline-block;
float: left;
}
.col2 {
width: 70%;
min-height: 800px;
display: inline-block;
}
.row1 .col1 {
background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/24-hours-phone-icon.png") !important;
background-position: left !important;
background-repeat: no-repeat !important;
background-size: contain !important;
background-attachment: fixed !important;
}
.row2 .col1 {
background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/abs-icon.png") !important;
background-position: left !important;
background-repeat: no-repeat !important;
background-size: contain !important;
background-attachment: fixed !important;
}
.row3 .col1 {
background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/arrow-down-icon.png") !important;
background-position: left !important;
background-repeat: no-repeat !important;
background-size: contain !important;
background-attachment: fixed !important;
}
<div class="main">
<div class="row1">
<div class="col1">
Col1
</div>
<div class="col2">
Col2
</div>
</div>
<div class="row2">
<div class="col1">
Col1
</div>
<div class="col2">
Col2
</div>
</div>
<div class="row3">
<div class="col1">
Col1
</div>
<div class="col2">
Col2
</div>
</div>
</div>
我知道有关"scroll magic"的信息,但我只想要通过CSS或最少量的JS代码获得更“自然”的东西。