uni-app 滚动条无法回到顶部解决办法

"当在uni-app中使用click事件设置scroll-top=0无法使滚动条回到顶部时,可以通过监听@scroll事件并结合old.scrollTop实现。在getActive方法中,先将scrollTop设为old.scrollTop,然后在$nextTick中将scrollTop设为0。同时添加@scroll="scrollChange"监听滚动,保存每次滚动的位置,以便后续使用。"

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

问题:在click事件中设置了scroll-top = 0后,uni-app 滚动条还是无法回到顶部.

<view :class="active == index? 'active' : ''"  @click="getActive(index)" v-for="(item,index) in categories" :key="item.cat_id">{{ item.cat_name }}</view>
<scroll-view class="right" scroll-y :scroll-top="scrollTop" >
			<view class="data" v-for="(list,index) in rightData" :key="list.cat_id">
				<image @click="showImage(list.cat_icon)" :src="list.cat_icon"></image>
				<view>{{ list.cat_name}}</view>
			</view>
			<view class="none" v-if="show">暂无数据</view>
</scroll-view>

 

解决办法:

添加 @scroll="scrollChange"

<view :class="active == index? 'active' : ''"  @click="getActive(index)" v-for="(item,index) in categories" :key="item.cat_id">{{ item.cat_name }}</view>
		<scroll-view class="right" scroll-y :scroll-top="scrollTop" @scroll="scrollChange">
			<view class="data" v-for="(list,index) in rightData" :key="list.cat_id">
				<image @click="showImage(list.cat_icon)" :src="list.cat_icon"></image>
				<view>{{ list.cat_name}}</view>
			</view>
			<view class="none" v-if="show">暂无数据</view>
</scroll-view>
		data() {
			return {
				categories: [],
				active: 0,
				rightData: [],
				index: 0,
				scrollTop: 0,
				old: {
					scrollTop: 0
				}
			}
		},
getActive(index) {
	this.scrollTop = this.old.scrollTop
	this.$nextTick(()=>{
	this.scrollTop = 0 //赋值为0即代表返回顶部
	});
},

scrollChange(e) {
	// console.log(e)
	this.old.scrollTop = e.detail.scrollTop
},

 

 
### 实现页面滚动条监听的方法 在 `uni-app` 中实现页面滚动条的监听主要依赖于 `<scroll-view>` 组件以及其提供的事件处理机制。具体来说,在需要监听滚动的页面或组件中,添加一个带有 `@scroll` 事件绑定的 `<scroll-view>` 元素可以用来触发自定义函数。 #### HTML 结构 ```html <template> <view class="container"> <!-- 使用 scroll-y 属性允许纵向滚动 --> <scroll-view scroll-y @scroll="handleScroll" class="scroll-content"> <!-- 这里放置可滚动的内容 --> </scroll-view> </view> </template> <style> .container { height: 100vh; } .scroll-content { height: 100%; } </style> ``` 当页面发生滚动时会调用 `handleScroll` 函数[^1]。 #### JavaScript 处理逻辑 为了能够获取到具体的滚动位置和其他相关信息,可以在 Vue 的 methods 或者 setup 钩子内定义相应的处理器: 如果是在普通的页面环境中,则可以直接编写如下代码: ```javascript export default { name: 'YourPageName', methods:{ handleScroll(event){ const scrollTop = event.detail.scrollTop; console.log(`当前滚动距离顶部的距离为 ${scrollTop}px`); // 可在此处加入更多业务逻辑, 如加载更多数据等操作. } } }; ``` 对于作为独立组件的情况,除了上述方式外还可以利用生命周期钩子中的 `mounted()` 来订阅全局广播的消息,从而达到同样的目的: ```javascript import { defineComponent } from "vue"; export default defineComponent({ name:"your-component-name", mounted(){ let that=this; uni.$on('onPageScroll',(data)=>{ console.log("页面滚动了"); console.log(data); that.scrollTop=data.scrollTop; }); }, }); ``` 这种方式适用于希望在整个应用范围内共享滚动状态的情形下使用[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值