再移动端,头部是作为一个可定制化的组件,例如一下导航
再一般情况下,我们头部是设置一个颜色,但是如果头部的相邻兄弟原生是一个背景图片,头部需要有一种“透明”的效果,如果把头部背景设置成透明,由于吸顶的效果滚动的时候就不行了,此时需要通过头部设置背景图片,将背景图片“截取”一部分,这部分和相邻的兄弟原生的上部分相同,由于吸顶,头部组件脱离文档流,下面相邻的兄弟元素占据头部所在的位置,此时就可以达到头部背景覆盖下面兄弟元素的一部分的效果,从而达成是看起来是一张“图片”
例如:为滚动前
滚动后
全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
padding: 0;
margin: 0;
}
.content{
width: 100%;
overflow: auto;
position: relative;
}
.head-main{
height: 60px;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
.head-content{
height: 100%;
width: 100%;
background: rgb(247, 215, 215);
background: url(./th.jpg);
background-repeat: no-repeat;
text-align: center;
line-height: 60px;
}
.content-main{
height: 235px;
width: 100%;
background: url(./th.jpg);
background-repeat: no-repeat;
background-size: 100%;
}
.list2{
height: 100px;
width: 100%;
background-color: rgb(3, 67, 85);
}
.list3{
height: 100px;
width: 100%;
background-color: rgb(218, 46, 233);
}
.list4{
height: 100px;
width: 100%;
background-color: rgb(27, 41, 53);
}
.list5{
height: 100px;
width: 100%;
background-color: rgb(210, 228, 40);
}
.list6{
height: 100px;
width: 100%;
background-color: rgb(30, 53, 58);
}
.list7{
height: 100px;
width: 100%;
background-color: rgb(227, 28, 221);
}
.list8{
height: 100px;
width: 100%;
background-color: rgb(39, 221, 67);
}
.list9{
height: 100px;
width: 100%;
background-color: rgb(203, 158, 21);
}
.list10{
height: 100px;
width: 100%;
background-color: rgb(31, 142, 239);
}
</style>
</head>
<body>
<div class="content">
<div class="head-main">
<div class="head-content">我是头部导航</div>
</div>
<div class="content-main"></div>
<div class="list2"></div>
<div class="list3"></div>
<div class="list4"></div>
<div class="list5"></div>
<div class="list6"></div>
<div class="list7"></div>
<div class="list8"></div>
<div class="list9"></div>
<div class="list10"></div>
</div>
</body>
</html>