提交 f5e2d9b0 编写于 作者: M mehaotian

perf: 优化滚动折叠示例的样式,调整部分代码结构

上级 711ac6c4
<template> <template>
<view class="top-box" :style="{height:(110 - (scrollTop>40?40:scrollTop) )+'px','background-color': 'rgba(255, 255, 255, '+(scrollTop*3>100?100:scrollTop*3)/100+')'}"> <view class="top-box" :style="headBoxStyle">
<view class="scroll-fold-nav" <view class="scroll-fold-nav" :style="titleStyle">
:style="{'opacity': 1 -(scrollTop*3>100?100:scrollTop*3)/100}" <view class="nav-title">DCloud 为开发者而生</view>
> </view>
<view style="margin-left: 30px;">DCloud 为开发者而生</view> <view @click="back" class="nav-back">
</view> <image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
<view @click="back" class="back" style="position: absolute;top:35px;left: 8px;"> </view>
<image src="/static/template/scroll-fold-nav/back.png" style="width: 20px;" mode="widthFix"></image> <view class="search" @click="toSearchPage" :style="{
</view> 'width':maxSearchWidth - (scrollTop>40?40:scrollTop) +'rpx',
<view class="search" @click="toSearchPage" :style="{ 'top':0 - (scrollTop>40?40:scrollTop) +'px'
'width':maxSearchWidth - (scrollTop>40?40:scrollTop) +'rpx', }">
'top':0 - (scrollTop>40?40:scrollTop) +'px' <view class="search-inner">
}"> <image class="search-img" src="/static/template/scroll-fold-nav/search.png" mode="widthFix"></image>
<view style="flex-direction: row;"> <text class="search-tip-text">请输入你要搜索的内容</text>
<image src="/static/template/scroll-fold-nav/search.png" style="width: 15px;" mode="widthFix"></image> </view>
<text class="search-tip-text">请输入你要搜索的内容</text> <text class="search-btn">搜索</text>
</view> </view>
<text class="search-btn">搜索</text> </view>
</view> </template>
</view>
</template>
<script>
export default {
data() {
return {
scrollTop: 0,
maxSearchWidth: 700
}
},
methods: {
toSearchPage(){
this.$emit('toSearchPage')
},
back(){
this.$emit('back')
}
}
}
</script>
<style>
.top-box {
position: fixed;
top: 0;
padding-top: 25px;
align-items: flex-end;
border-bottom: 1px solid #efefef;
}
.scroll-fold-nav { <script>
height: 44px; export default {
width: 750rpx; data() {
justify-content: center; return {
} scrollTop: 0,
maxSearchWidth: 700
}
},
computed: {
// 修理导航栏头部高度和背景颜色变化
headBoxStyle() : string {
const scrollTop = this.scrollTop
let style = `height: ${110 - (scrollTop > 40 ? 40 : scrollTop)}px;background-color: rgba(255, 255, 255, ${(scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100})`
return style
},
// 处理导航栏标题透明度变化
titleStyle() : string {
const scrollTop = this.scrollTop
let style = `opacity: ${1 - (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100}`
return style
}
},
methods: {
toSearchPage() {
this.$emit('toSearchPage')
},
back() {
this.$emit('back')
}
}
}
</script>
.search { <style>
background-color: #FFFFFF; .top-box {
border: 1px solid #fbdf0d; position: fixed;
height: 35px; top: 0;
border-radius: 100px; padding-top: 25px;
margin: 0 25rpx; align-items: flex-end;
padding: 8px; border-bottom: 1px solid #efefef;
flex-direction: row; }
align-items: center;
justify-content: space-between;
}
.search-tip-text { .scroll-fold-nav {
font-size: 12px; height: 44px;
color: #666; width: 750rpx;
} justify-content: center;
}
.search-btn { .nav-title {
font-size: 12px; margin-left: 30px;
background-color: #ff6900; }
color: #FFF;
padding: 5px 8px; .nav-back {
border-radius: 100px; position: absolute;
} top: 35px;
</style> left: 8px;
}
.nav-back .back-img {
width: 18px;
margin-top: 2px;
}
.search {
background-color: #FFFFFF;
border: 1px solid #fbdf0d;
height: 35px;
border-radius: 100px;
margin: 0 25rpx;
padding: 8px;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.search-inner {
margin-top: 2px;
flex-direction: row;
}
.search-inner .search-img {
width: 15px;
}
.search-tip-text {
font-size: 12px;
color: #666;
}
.search-btn {
font-size: 12px;
background-color: #ff6900;
color: #FFF;
padding: 5px 8px;
border-radius: 100px;
}
</style>
\ No newline at end of file
<template> <template>
<view class="page"> <view class="page">
<scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view"> <scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view">
<view class="content"> <view class="height-seat">
<view style="height: 110px;"> <!-- 垫高专用 -->
<!-- 垫高专用 --> </view>
</view> <view class="content">
<view v-for="(item,index) in 20" :key="index" style="height: 100px;background-color: #FFF;"> <view class="content-item">
content-{{item}} <text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听 scrollTop。</text>
</view> <text class="text">2. 在模板中根据 scrollTop 的变化,通过 style 修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</text>
</view> <text class="text">3. 请向上\向下滚动页面观察效果。</text>
</scroll-view> </view>
<scroll-fold-header ref="header" @back="back" @toSearchPage="toSearchPage()"/> <view class="content-item" v-for="(item,index) in 20" :key="index">
</view> <text class="text">content-{{item}}</text>
</view>
</view>
</scroll-view>
<scroll-fold-header ref="header" @back="back" @toSearchPage="toSearchPage()" />
</view>
</template> </template>
<script> <script>
import ScrollEvent from 'io.dcloud.uniapp.runtime.ScrollEvent'; import ScrollEvent from 'io.dcloud.uniapp.runtime.ScrollEvent';
import scrollFoldHeader from './scroll-fold-header/scroll-fold-header.uvue'; import scrollFoldHeader from './scroll-fold-header/scroll-fold-header.uvue';
export default { export default {
components:{scrollFoldHeader}, components: { scrollFoldHeader },
data() { data() {
return {} return {}
}, },
methods: { methods: {
onScroll(e : ScrollEvent) { onScroll(e : ScrollEvent) {
(this.$refs['header'] as ComponentPublicInstance).$data['scrollTop'] = (e.detail.scrollTop<0?0:e.detail.scrollTop) (this.$refs['header'] as ComponentPublicInstance).$data['scrollTop'] = (e.detail.scrollTop < 0 ? 0 : e.detail.scrollTop)
}, },
back() { back() {
// uni.navigateBack() // 这么写用不了 // uni.navigateBack() // 这么写用不了
// 这么写可以用 // 这么写可以用
uni.navigateBack({ uni.navigateBack({
success(result) { success(result) {
console.log('navigateBack success', result.errMsg) console.log('navigateBack success', result.errMsg)
}, },
fail(error) { fail(error) {
console.log('navigateBack fail', error.errMsg) console.log('navigateBack fail', error.errMsg)
}, },
complete(result) { complete(result) {
console.log('navigateBack complete', result.errMsg) console.log('navigateBack complete', result.errMsg)
}, },
}) })
}, },
toSearchPage() { toSearchPage() {
uni.showToast({ uni.showToast({
title: '暂不支持', title: '暂不支持',
icon: 'none' icon: 'none'
}); });
} }
}, },
onLoad() {} onLoad() { }
} }
</script> </script>
<style> <style>
.page { .page {
flex: 1; flex: 1;
background-color: #fbdf0d; background-color: #fbdf0d;
} /* background-color: #f5f5f5; */
}
.scroll-view { .scroll-view {
flex: 1; flex: 1;
} }
.height-seat {
height: 110px;
/* background-color: #fbdf0d; */
}
.content {
padding: 5px 15px;
background-color: #f5f5f5;
}
.content-item {
padding: 15px;
margin: 5px 0;
background-color: #fff;
border-radius: 5px;
}
.text {
font-size: 14px;
color: #666;
line-height: 20px;
}
</style> </style>
\ No newline at end of file
<template> <template>
<view class="page"> <view class="page">
<scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view" bounces="false"> <scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view" bounces="false">
<view class="content"> <view class="height-seat" style="height: 110px;">
<view style="height: 110px;"> <!-- 垫高专用 -->
<!-- 垫高专用 --> </view>
</view> <view class="content">
<view v-for="(item,index) in 20" :key="index" style="height: 100px;background-color: #FFF;"> <view class="content-item">
content-{{item}} <text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听scrollTop。</text>
<text class="text">2. 与上一个示例不同的是,此示例使用 ref 直接获取了元素的节点,并在 scroll 事件中通过节点的 setProperty 方法来修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</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> </view>
</scroll-view> </scroll-view>
<view ref="top-box" class="top-box" style="height:110px;background-color:rgba(255,255,255,0)"> <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">
<view style="margin-left: 30px;">DCloud 为开发者而生</view> <view class="nav-title">DCloud 为开发者而生</view>
</view> </view>
<view @click="back" class="back" style="position: absolute;top:35px;left: 8px;"> <view @click="back" class="nav-back">
<image src="/static/template/scroll-fold-nav/back.png" style="width: 20px;" mode="widthFix"></image> <image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
</view> </view>
<view ref="search" class="search" @click="toSearchPage" style="width:700rpx;top:0px"> <view ref="search" class="search" @click="toSearchPage">
<view style="flex-direction: row;"> <view class="search-inner">
<image src="/static/template/scroll-fold-nav/search.png" style="width: 15px;" mode="widthFix"></image> <image class="search-img" src="/static/template/scroll-fold-nav/search.png" mode="widthFix"></image>
<text class="search-tip-text">请输入你要搜索的内容</text> <text class="search-tip-text">请输入你要搜索的内容</text>
</view> </view>
<text class="search-btn">搜索</text> <text class="search-btn">搜索</text>
...@@ -41,7 +46,13 @@ ...@@ -41,7 +46,13 @@
boxNode: null as INode | null, boxNode: null as INode | null,
navNode: null as INode | null navNode: null as INode | null
} }
}, },
onLoad() {},
onReady() {
this.searchNode = this.$refs['search'] 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
...@@ -79,13 +90,7 @@ ...@@ -79,13 +90,7 @@
}); });
} }
}, },
onLoad() {
},
onReady() {
this.searchNode = this.$refs['search'] as INode;
this.boxNode = this.$refs['top-box'] as INode;
this.navNode = this.$refs['scroll-fold-nav'] as INode;
}
} }
</script> </script>
...@@ -97,22 +102,60 @@ ...@@ -97,22 +102,60 @@
.scroll-view { .scroll-view {
flex: 1; flex: 1;
} }
.height-seat {
height: 110px;
/* background-color: #fbdf0d; */
}
.content {
padding: 5px 15px;
background-color: #f5f5f5;
}
.content-item {
padding: 15px;
margin: 5px 0;
background-color: #fff;
border-radius: 5px;
}
.text {
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: 25px;
align-items: flex-end; align-items: flex-end;
border-bottom: 1px solid #efefef; border-bottom: 1px solid #efefef;
height:110px;
background-color:rgba(255,255,255,0)
} }
.scroll-fold-nav { .scroll-fold-nav {
height: 44px; height: 44px;
width: 750rpx; width: 750rpx;
justify-content: center; justify-content: center;
} }
.nav-title {
margin-left: 30px;
}
.nav-back {
position: absolute;
top: 35px;
left: 8px;
}
.nav-back .back-img {
width: 18px;
margin-top: 2px;
}
.search { .search {
background-color: #FFFFFF; background-color: #FFFFFF;
border: 1px solid #fbdf0d; border: 1px solid #fbdf0d;
...@@ -122,8 +165,19 @@ ...@@ -122,8 +165,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;
top:0px
}
.search-inner {
margin-top: 2px;
flex-direction: row;
}
.search-inner .search-img {
width: 15px;
}
.search-tip-text { .search-tip-text {
font-size: 12px; font-size: 12px;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册