组件最外层增加一个scroll-view,增加scrolltoupper及scrolltolower事件监听,下拉刷新就需要增加 refresher-enabled="true"
<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll" refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" refresher-background="lightgreen" @refresherpulling="onPulling" @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort"> <view id="demo1" class="scroll-view-item uni-bg-red">A</view> <view id="demo2" class="scroll-view-item uni-bg-green">B</view> <view id="demo3" class="scroll-view-item uni-bg-blue">C</view> </scroll-view>完整代码
<template> <view> <scroll-view style="height: 300px;" scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" refresher-background="lightgreen" @refresherpulling="onPulling" @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort"></scroll-view> <!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 --> <view class="uni-padding-wrap uni-common-mt"> <view class="uni-title uni-common-mt"> Vertical Scroll <text>\n纵向滚动</text> </view> <view> <scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll"> <view id="demo1" class="scroll-view-item uni-bg-red">A</view> <view id="demo2" class="scroll-view-item uni-bg-green">B</view> <view id="demo3" class="scroll-view-item uni-bg-blue">C</view> </scroll-view> </view> <view @tap="goTop" class="uni-link uni-center uni-common-mt"> 点击这里返回顶部 </view> <view class="uni-title uni-common-mt"> Horizontal Scroll <text>\n横向滚动</text> </view> <view> <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="120"> <view id="demo1" class="scroll-view-item_H uni-bg-red">A</view> <view id="demo2" class="scroll-view-item_H uni-bg-green">B</view> <view id="demo3" class="scroll-view-item_H uni-bg-blue">C</view> </scroll-view> </view> <view class="uni-common-pb"></view> </view> </view> </template> <script> export default { data() { return { triggered: false, scrollTop: 0, old: { scrollTop: 0 } } }, onLoad() { this._freshing = false; setTimeout(() => { this.triggered = true; }, 1000) }, methods: { onPulling(e) { console.log("onpulling", e); }, onRefresh() { if (this._freshing) return; this._freshing = true; setTimeout(() => { this.triggered = false; this._freshing = false; }, 3000) }, onRestore() { this.triggered = 'restore'; // 需要重置 console.log("onRestore"); }, onAbort() { console.log("onAbort"); }, upper: function(e) { console.log(e) }, lower: function(e) { console.log(e) }, scroll: function(e) { console.log(e) this.old.scrollTop = e.detail.scrollTop }, goTop: function(e) { // 解决view层不同步的问题 this.scrollTop = this.old.scrollTop this.$nextTick(function() { this.scrollTop = 0 }); uni.showToast({ icon: "none", title: "纵向滚动 scrollTop 值已被修改为 0" }) } } } </script> <style> .scroll-Y { height: 300rpx; } .scroll-view_H { white-space: nowrap; width: 100%; } .scroll-view-item { height: 300rpx; line-height: 300rpx; text-align: center; font-size: 36rpx; } .scroll-view-item_H { display: inline-block; width: 100%; height: 300rpx; line-height: 300rpx; text-align: center; font-size: 36rpx; } </style>
网友回复
为啥所有的照片分辨率提升工具都会修改照片上的图案细节?
js如何在浏览器中将webm视频的声音分离为单独音频?
微信小程序如何播放第三方域名url的mp4视频?
ai多模态大模型能实时识别视频中的手语为文字吗?
如何远程调试别人的chrome浏览器获取调试信息?
为啥js打开新网页window.open设置窗口宽高无效?
浏览器中js的navigator.mediaDevices.getDisplayMedia屏幕录像无法录制SpeechSynthesisUtterance产生的说话声音?
js中mediaRecorder如何录制window.speechSynthesis声音音频并下载?
python如何直接获取抖音短视频的音频文件url?
js在浏览器中如何使用MediaStream与MediaRecorder实现声音音频多轨道混流?