提交 78996dee 编写于 作者: DCloud-yyl's avatar DCloud-yyl

优化“随滚动折叠的导航栏”对状态栏的适配

上级 f37e643a
<template> <template>
<view class="page"> <scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view" rebound="false">
<scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view" rebound="false"> <view class="height-seat" style="height: 125px;">
<view class="height-seat" style="height: 110px;"> <!-- 垫高专用 -->
<!-- 垫高专用 --> </view>
</view> <view class="content">
<view class="content"> <view class="content-item">
<view class="content-item"> <text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听scrollTop。</text>
<text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听scrollTop。</text> <text class="text">2. 使用 ref 直接获取元素的节点,并在 scroll 事件中通过节点的 setProperty
<text class="text">2. 使用 ref 直接获取元素的节点,并在 scroll 事件中通过节点的 setProperty 方法来修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</text> 方法来修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</text>
<text class="text">3. 请向上\向下滚动页面观察效果。</text> <text class="text">3. 请向上\向下滚动页面观察效果。</text>
</view>
<view class="content-item" v-for="(item,index) in 20" :key="index">
<text class="text">content-{{item}}</text>
</view>
</view> </view>
</scroll-view> <view class="content-item" v-for="(item,index) in 20" :key="index">
<text class="text">content-{{item}}</text>
</view>
</view>
<view ref="top-box" class="top-box"> <view ref="top-box" class="top-box">
<view ref="scroll-fold-nav" class="scroll-fold-nav"> <view ref="scroll-fold-nav" class="scroll-fold-nav">
...@@ -32,34 +31,36 @@ ...@@ -32,34 +31,36 @@
</view> </view>
</view> </view>
</view> </scroll-view>
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
statusBarHeight: 35,
scrollTop: 0, scrollTop: 0,
searchWidth: 700, searchWidth: 700,
searchNode: null as INode | null, searchNode: null as INode | null,
boxNode: null as INode | null, boxNode: null as INode | null,
navNode: null as INode | null navNode: null as INode | null
} }
}, },
onLoad() {}, onLoad() { },
onReady() { onReady() {
this.searchNode = this.$refs['search'] as INode; this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight ?? 35;
this.boxNode = this.$refs['top-box'] as INode; this.searchNode = this.$refs['search'] as INode;
this.navNode = this.$refs['scroll-fold-nav'] as INode; this.boxNode = this.$refs['top-box'] as INode;
}, this.navNode = this.$refs['scroll-fold-nav'] as INode;
},
methods: { methods: {
onScroll(e : ScrollEvent) { onScroll(e : ScrollEvent) {
let scrollTop = e.detail.scrollTop let scrollTop = e.detail.scrollTop
this.boxNode?.style?.setProperty('height', (110 - (scrollTop > 40 ? 40 : scrollTop)) + 'px'); this.boxNode?.style?.setProperty('height', (125 - (scrollTop > this.statusBarHeight ? this.statusBarHeight : scrollTop)) + 'px');
this.boxNode?.style?.setProperty('backgroundColor', 'rgba(255, 255, 255, ' + (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0 + ')'); this.boxNode?.style?.setProperty('backgroundColor', 'rgba(255, 255, 255, ' + (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0 + ')');
this.navNode?.style?.setProperty('opacity', 1 - (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0); this.navNode?.style?.setProperty('opacity', 1 - (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0);
this.searchNode?.style?.setProperty('width', 700 - (scrollTop > 40 ? 40 : scrollTop) + 'rpx'); this.searchNode?.style?.setProperty('width', 700 - (scrollTop > 40 ? 40 : scrollTop) + 'rpx');
this.searchNode?.style?.setProperty('top', 0 - (scrollTop > 40 ? 40 : scrollTop) + 'px'); this.searchNode?.style?.setProperty('top', 0 - (scrollTop > 40 ? 40 : scrollTop) + 'px');
this.scrollTop = scrollTop; this.scrollTop = scrollTop;
}, },
back() { back() {
...@@ -89,7 +90,7 @@ ...@@ -89,7 +90,7 @@
}); });
} }
}, },
} }
</script> </script>
...@@ -101,60 +102,63 @@ ...@@ -101,60 +102,63 @@
.scroll-view { .scroll-view {
flex: 1; flex: 1;
} }
.height-seat { .height-seat {
height: 110px; height: 110px;
/* background-color: #fbdf0d; */ /* background-color: #fbdf0d; */
} }
.content {
padding: 5px 15px; .content {
background-color: #f5f5f5; padding: 5px 15px;
} background-color: #f5f5f5;
.content-item { }
padding: 15px;
margin: 5px 0; .content-item {
background-color: #fff; padding: 15px;
border-radius: 5px; margin: 5px 0;
background-color: #fff;
} border-radius: 5px;
.text { }
font-size: 14px;
color: #666; .text {
line-height: 20px; font-size: 14px;
} color: #666;
line-height: 20px;
}
.top-box { .top-box {
position: fixed; position: fixed;
top: 0; top: 0;
padding-top: 25px; padding-top: 35px;
align-items: flex-end; align-items: flex-end;
border-bottom: 1px solid #efefef; border-bottom: 1px solid #efefef;
height:110px; height: 125px;
background-color:rgba(255,255,255,0) background-color: rgba(255, 255, 255, 0);
background-color: aliceblue;
} }
.scroll-fold-nav { .scroll-fold-nav {
height: 44px; height: 44px;
width: 750rpx; width: 750rpx;
justify-content: center; justify-content: center;
} }
.nav-title { .nav-title {
margin-left: 30px; margin-left: 30px;
} }
.nav-back { .nav-back {
position: absolute; position: absolute;
top: 35px; top: 44px;
left: 8px; left: 8px;
} }
.nav-back .back-img { .nav-back .back-img {
width: 18px; width: 18px;
margin-top: 2px; margin-top: 2px;
} }
.search { .search {
background-color: #FFFFFF; background-color: #FFFFFF;
border: 1px solid #fbdf0d; border: 1px solid #fbdf0d;
...@@ -164,19 +168,19 @@ ...@@ -164,19 +168,19 @@
padding: 8px; padding: 8px;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
width:700rpx; width: 700rpx;
top:0px top: 0px;
} }
.search-inner { .search-inner {
margin-top: 2px; margin-top: 2px;
flex-direction: row; flex-direction: row;
} }
.search-inner .search-img { .search-inner .search-img {
width: 15px; width: 15px;
} }
.search-tip-text { .search-tip-text {
font-size: 12px; font-size: 12px;
...@@ -190,4 +194,4 @@ ...@@ -190,4 +194,4 @@
padding: 5px 8px; padding: 5px 8px;
border-radius: 100px; border-radius: 100px;
} }
</style> </style>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册