业务场景
主页要做一个搜索框,滑动主页页面的时候,搜索框始终位于导航栏下面,位置不变,不随页面的滑动而滑动,这种效果被称为”吸顶“效果。
点击搜索框,弹出上层搜索详情的视图层,搜索详情的整个页面覆盖在主页面之上,并且也覆盖住主要搜索框。
实现
主页搜搜框设置 position: sticky
和 top: 0
即可实现吸顶效果。
.searchbarView {
position: sticky;
top: 0;
width: 100%;
height: 40px;
display: flex;
align-items: center;
background-color: white;
/* background-color: aqua; */
}
点击搜索框弹出上层视图时发现,搜索框不能被覆盖:
需要在搜搜详情这个页面上也添加 top: 0
,就会遮住搜索框
.searchView {
display: flex;
position: absolute;
top: 0;
height: 100%;
width: 100%;
background-color: rgb(0, 0, 0, 0.3);
}
效果: