<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrapper {
height: 100wh;
}
.parallax-img {
/* ! */
background-attachment: fixed;
/* ! */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
background-image: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg");
}
p {
font-size: 20px;
padding: 1.5rem 3rem;
min-height: 1000px;
/* 当你的文字内容不够,也能撑出足够的高度来看到效果,当然如果你文字足够多,就不需要了 */
}
.head {
height: 500px;
background: #ccc;
font-size: 20px;
padding: 1.5rem 3rem;
background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_9jl3a3bpyja8h.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.foot {
height: 500px;
background: #ccc;
font-size: 20px;
padding: 1.5rem 3rem;
background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_jsss7i9zf2yf7.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="head">
afgsdgsdg
</div>
<div class="parallax-img"></img>
<p>sdfsdfdsf</p>
</div>
<div class="foot">
adsdasdasd
</div>
</div>
</body>
</html>
上面那个在手机上浏览没有效果,查了后写了新的👇
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrapper {
height: 100wh;
}
.parallax-img:before {
content: ' ';
position: fixed;
z-index: -1;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg") center 0 no-repeat;
background-size: cover;
}
p {
font-size: 20px;
padding: 1.5rem 3rem;
min-height: 200px;
/* 当你的文字内容不够,也能撑出足够的高度来看到效果,当然如果你文字足够多,就不需要了 */
}
.head {
height: 500px;
background: #ccc;
font-size: 20px;
padding: 1.5rem 3rem;
background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_9jl3a3bpyja8h.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.foot {
height: 500px;
background: #ccc;
font-size: 20px;
padding: 1.5rem 3rem;
background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_jsss7i9zf2yf7.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="head">
afgsdgsdg
</div>
<div class="parallax-img"></img>
<p>sdfsdfdsf</p>
</div>
<div class="foot">
adsdasdasd
</div>
</div>
</body>
</html>
修改后主要的代码是加了伪类用fixed来当作固定背景图:
.parallax-img:before {
content: ' ';
position: fixed;
z-index: -1;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg") center 0 no-repeat;
background-size: cover;
}