有下拉事件:
onPullDownRefresh: function () {}
监听屏幕距离顶部高度
onPageScroll(res) {
console.log("高度", res.scrollTop); //距离页面顶部距离
},
滚动到距离屏幕高度的距离
uni.pageScrollTo({
duration: 0, // 毫秒
scrollTop: height, // 位置
});
获取内容高度
uni.getSystemInfo({
success(res) {
_this.phoneHeight = res.windowHeight;
console.log(res.windowHeight);
// 计算组件的高度
let view = uni.createSelectorQuery().select(".i");//这标签是需要拿的标签
view
.boundingClientRect((data) => {
_this.navHeight = data.height;
_this.newh = data.height;
_this.oldh = _this.newh;
console.log(_this.oldh, _this.newh);
})
.exec();
},
});
然后直接上代码
需要在page.json设置下拉"enablePullDownRefresh":true
<template>
<view class="i">
<view class="item" v-for="(i, index) in list" :key="index">
{{ i }}
</view>
</view>
</template>
<script>
export default {
data() {
return {
bannerdata: [],
tabBar: [
{
name: "新歌速递",
},
{
name: "热门歌单",
},
{
name: "排行榜",
},
{
name: "MV",
},
],
list: ["A", "B", "C", "D", "E", "f"],
oldh: "",
newh: "",
};
},
onPullDownRefresh: function () {
let arrl = ["1", "2", "3", "4", "5"];
let _this = this;
this.list = [...arrl, ...this.list];
setTimeout(function () {
uni.getSystemInfo({
success(res) {
_this.phoneHeight = res.windowHeight;
console.log(res.windowHeight);
// 计算组件的高度
let view = uni.createSelectorQuery().select(".i");
view
.boundingClientRect((data) => {
_this.navHeight = data.height;
_this.newh = data.height;
let height = _this.newh - _this.oldh;
uni.pageScrollTo({
duration: 0, // 毫秒
scrollTop: height, // 位置
});
_this.oldh = _this.newh;
console.log(_this.oldh, _this.newh);
})
.exec();
},
});
}, 10);
uni.stopPullDownRefresh();
},
onPageScroll(res) {
console.log("高度", res.scrollTop); //距离页面顶部距离
},
onLoad() {
let _this = this;
uni.getSystemInfo({
success(res) {
_this.phoneHeight = res.windowHeight;
console.log(res.windowHeight);
// 计算组件的高度
let view = uni.createSelectorQuery().select(".i");
view
.boundingClientRect((data) => {
console.log(data);
// _this.navHeight = data.height;
_this.oldh = data.height;
console.log("旧", _this.oldh);
_this.scrollviewHigh = _this.phoneHeight - _this.navHeight;
})
.exec();
},
});
},
methods: {},
};
</script>
<style lang="less">
.banner {
height: 300rpx;
}
.banner-image {
width: 110%;
}
.i {
background-color: red;
.item {
height: 200px;
background-color: skyblue;
}
}
</style>
如果需要兼容app的话,把onload中的数据放到mounted
本文介绍了如何在uni-app中实现类似微信聊天的下拉加载效果。通过监听屏幕距离顶部高度、滚动到特定位置以及获取内容高度,配合在page.json中设置`enablePullDownRefresh`: true,并处理App端的兼容问题,可以完成下拉刷新功能。
2913






