前言
H5的标题栏置顶的实现方式有多种,但是我们的置顶还需要满足下面两种情况:
1、滚动时标题栏也要置顶
2、标题栏处不应该显示滚动条
如图所示:
简单的置顶四要素,不能实现滚动时标题栏也能置顶:
position: fixed;
top: 0;
left: 0;
z-index: 998;
fixed:相对于浏览器窗口的绝对定位
top:
距离窗口顶部距离- left:距离窗口左边的距离
- z-index:层叠顺序,数值越大就越高,页面滚动的时候就不会被其他内容所遮挡。
实现滚动时标题栏也能置顶,标题栏处不应该显示滚动条
- body标签的样式:overflow: hidden; 使整个网页不能滚动。
-
title-bg ,标题背景的样式: position: relative; 使内容与标题分离。
-
content ,标题下的内容样式:
.content {
height: 100%;
width: 100%;
font-size: 0.15rem;
/* 页面能滚动 */
overflow: scroll;
}
通用的标题栏代码:
1、滚动时标题栏也要置顶
2、标题栏处不应该显示滚动条
3、0.44rem = 44px (使用rem适配各种移动设备)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
}
html {
height: 100%;
width: 100%;
/* 根元素 */
font-size: 100px;
color: #333333;
}
body {
height: 100%;
width: 100%;
font-size: 0.13rem;
margin: 0;
background: #f6f6f6;
display: flex;
flex-direction: column;
/* 页面不能滚动 */
overflow: hidden;
}
#app {
height: 100%;
width: 100%;
background: #f6f6f6;
display: flex;
flex-direction: column;
}
.title-bg {
width: 100%;
min-height: 0.44rem;
position: relative;
top: 0;
left: 0;
z-index: 998;
background-color: white;
display: flex;
align-items: center;
justify-content: center;
color: black;
font-size: 0.16rem;
font-weight: bold;
border-bottom: 0.01rem #f6f6f6 solid;
}
.title-arrow {
position: absolute;
left: 0.10rem;
display: flex;
align-items: center;
justify-content: center;
}
.title-img {
width: 0.26rem;
}
.content {
height: 100%;
width: 100%;
font-size: 0.15rem;
/* 页面能滚动 */
overflow: scroll;
}
</style>
</head>
<body>
<div id="app">
<div class="title-bg">
<div class="title-arrow">
<img src="images/ic_back.png" class='title-img'/>
</div>
<div class="title-tx">标题</div>
</div>
<div class="content">
</div>
</div>
</body>
</html>