h5网站在页面滚动时,背景图片保持不变,而不是跟随滚动。

使用背景固定定位,只需要在CSS中增加一个
background-attachment: fixed;属性即可。
<div class="item_right">
<img src="images/image001_about@2x.png">
div>
<style>
.item_right {
width: 60.75%;
height: 500px;
position: absolute;
right: 0;
background-image: url(../images/image001_about@2x.png);
background-repeat: no-repeat;
background-size: 60.75% 100%;
background-attachment: fixed; /* 这一行是重点 */
background-position: top right;
}
.item_right img {
max-width: 100%;
height: auto;
visibility: hidden;
}
style>
在某些苹果设备上,background-attachment: fixed可能无法正常工作。这是因为在某些苹果设备上,当页面滚动时,固定定位的背景图像会出现问题。
使用一下方式来处理:
<div class="aboutus">
<div class="item_outer">
<div class="item itemOne">
<div class="item_left">左边内容div>
<div class="item_right">
<img src="__INDEX_IMAGES__/image001_about@2x.png"/>
div>
div>
div>
<div class="item_outer">
<div class="item itemTwo">
<div class="item_left">左边内容div>
<div class="item_right">
<img src="__INDEX_IMAGES__/image002_about@2x.png"/>
div>
div>
div>
div>
<style>
/*pc端显示的样式*/
.aboutus .item .item_right {
width: 60.75%;
height: 500px;
background-image: url(../images/image001_about@2x.png);
background-repeat: no-repeat;
background-size: 60.75% 100%;
background-attachment: fixed;
background-position: top right;
position: absolute;
right: 0;
}
.aboutus .item_right img {
max-width: 100%;
height: auto;
visibility: hidden;
}
.aboutus .itemTwo .item_right {
background-image: url(../images/image002_about@2x.png) !important;
}
/*移动端显示的样式*/
@media only screen and (max-width:760px) {
.aboutus .item .item_right {
display: none;
}
/*最外层盒子*/
.aboutus{
width: 100%;
height: 190vw; /*固定的高度*/
position: relative;
}
.aboutus .item_outer{
width: 100%;
height: 85vw;
position: relative;
overflow: hidden; /*必须写,否则ios会失效*/
}
.aboutus .item {
height: 85vw; /*固定的高度*/
margin-bottom: 4px;
position: absolute !important;
}
/*第1个盒子*/
.aboutus .itemOne{
z-index: 1;
clip: rect(0, auto, auto, 0);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.aboutus .itemOne:before {
content: "";
position: fixed;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
-webkit-transform: translateZ(0);
transform: translateZ(0);
will-change: transform;
background-image: url(../images/sj_image01_about@2x.png);
}
/*第2个盒子*/
.aboutus .itemTwo{
z-index: 1;
clip: rect(0, auto, auto, 0);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.aboutus .itemTwo:before {
content: "";
position: fixed;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
-webkit-transform: translateZ(0);
transform: translateZ(0);
will-change: transform;
background-image: url(../images/sj_image02_about@2x.png);
}
}
style>
注:在手机端 外层盒子设置一个固定的高度height: 190vw; 定位在上面的其他元素也用vw的单位。
————————————————————————————————————————————
// 检测是否为苹果设备
var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
// 如果是苹果设备,将背景图像改为固定定位
if (isIOS) {
var background = document.querySelector('.background');
background.style.backgroundAttachment = 'scroll';
}