uni-app微信小程序页面滑动,副标题固定到顶部

本文描述了如何在uni-app开发的微信小程序中,通过监听页面滑动事件,使副标题在上滑至顶部时锁定并保持在视口上方。使用uni.createSelectorQuery获取初始位置,当滑动超过该位置时应用fixed定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uni-app微信小程序页面滑动,副标题固定到顶部

页面上滑副标题到达顶部后,标题锁定在顶部

请添加图片描述

1、首先加载时通过uni.createSelectorQuery的方法获取需要被锁定在顶部样式距离顶部的距离

onLoad() {
	uni.createSelectorQuery().select('.box').boundingClientRect(res=>{
		this.myScroll =res.top;
	}).exec();
},

2、利用页面滚动的onPageScroll函数获取页面滑动的距离

onPageScroll(e) {
	console.log(e.scrollTop)
}

3、当滑动的距离大于(锁定框)初始化时距离顶部的高度时,修改样式为“position: fixed;”固定定位样式

onPageScroll(e) {
	if(e.scrollTop > this.myScroll){
		this.temp= 1
	}else{
		this.temp= 0
	}
}

4、(个人处理方式可忽略)为了防止样式变化后页面闪动,加一个和头部相同样式用于缓冲。

<view v-if="temp==1" class="box">用于缓冲 fixed 导致的瞬间闪动</view>

这里是使用微信自带的title样式,如果是自动定义的title就需要对自定义的title的高度进行相应的计算就可以了,然后动态设置锁定的top。相关的计算这里就不做过多的赘述了。

完整相关代码

<template>
	<view>
		<view class="list" v-for="(item,index) in listDataA">
			<view class="list-item" :style="index == listDataA.length-1 ?'margin: 20rpx' : ''">{{item.name}}</view>
		</view>
		<view class="box" :class="temp==1?'boxStyle':''">上拉顶部固定 Title</view>
		<view v-if="temp==1" class="box">用于缓冲 fixed 导致的瞬间闪动</view>
		<view class="list" v-for="(item,index) in listDataB">
			<view class="list-item" :style="index == listDataB.length-1 ?'margin: 20rpx' : ''">{{item.name}}</view>
		</view>
	</view>
</template>
 
<script>
	export default {
	  data() {
	    return {
	      temp:0,
	      myScroll:0,
				listDataA: [],
				listDataB: []
	    }
	  },
		created() {
			for (let i = 0; i < 3; i++) {
				this.listDataA.push({ id: i+1, name: '测试数据A - ' + (i + 1) })
			}
			for (let j = 0; j < 10; j++) {
				this.listDataB.push({ id: j+1, name: '测试数据B - ' + (j + 1) })
			}
		},
	  onLoad(){
	   uni.createSelectorQuery().select('.box').boundingClientRect(res=>{
	      this.myScroll =res.top;
	    }).exec();
	  },
	  onPageScroll(e){
			
	    if(e.scrollTop > this.myScroll){
	      this.temp= 1
	    }else{
	      this.temp= 0
	    }
	  }
	}
</script>
 
<style>
	.box{
		height: 100rpx;
		width: 100wv;
		text-align: left;
		line-height: 100rpx;
		background: #f7fbff;
		padding-left: 25rpx;
	}
	.boxStyle{
		position: fixed;
		top: 0;
		left: 0;
		height: 100rpx;
		width: 100%;
		text-align: left;
		line-height: 100rpx;
		background: #f7fbff;
		padding-left: 25rpx;
	}
	.list{
		width: 100%; 
		background: #eee; 
		display: flex;
	}
	.list-item{
		margin: 20rpx 20rpx 0 20rpx; 
		width: 100%; 
		height: 200rpx; 
		background: #FFF; 
		padding: 10rpx; 
		border-radius: 10rpx;
	}

</style>

以上为个人工作过程中的使用及记录,如对您有所帮助不胜荣幸。

### 实现微信小程序中搜索框的吸顶效果 为了实现在滚动过程中保持搜索框始终位于页面顶部的效果,在 `uni-app` 开发环境中可以采用 CSS 的定位属性来达成这一目标。具体来说,通过设置 `.fixedTop` 类样式可使元素固定于屏幕顶端[^3]。 #### HTML 结构定义 首先需构建好包含搜索栏在内的基础布局结构: ```html <template> <view class="container"> <!-- 搜索区域 --> <view :class="[isFixed ? 'search-bar fixedTop' : 'search-bar']"> <input type="text" placeholder="请输入关键字..." /> </view> <!-- 列表展示区 --> <scroll-view scroll-y @scroll="handleScroll" class="list-container"> <!-- 这里放置列表项内容 --> </scroll-view> </view> </template> ``` #### 关键CSS样式配置 接着为上述模板中的组件添加必要的样式规则,特别是用于控制固定的`.fixedTop`类以及默认状态下搜索条的基础外观: ```css <style scoped> .search-bar { background-color: white; padding: 10px; } .fixedTop{ position: fixed !important; width: 100%; top: 0; left: 0; z-index: 999; } .list-container { height: calc(100vh - env(safe-area-inset-top)); } </style> ``` #### JavaScript逻辑处理 最后编写Vue实例方法监听用户的滚动行为并据此调整变量状态从而触发样式的动态切换: ```javascript <script> export default { data() { return { isFixed: false, scrollTop: 0 }; }, methods: { handleScroll(e) { this.scrollTop = e.detail.scrollTop; if (this.scrollTop >= 50 && !this.isFixed || this.scrollTop < 50 && this.isFixed){ this.isFixed = !this.isFixed; } } } }; </script> ``` 当用户向下滚动超过设定阈值(此处设为50像素),则激活 `isFixed` 布尔型数据绑定至视图层,进而应用带有绝对定位特性的额外样式;反之亦然。这样就能确保即使页面主体部分发生位移变化时,指定区域内的重要控件仍能维持可见性不变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值