uniapp實(shí)現(xiàn)下拉刷新與上拉觸底加載功能的示例代碼
下拉刷新
這個(gè)用于監(jiān)聽該頁面用戶下拉刷新事件。
首先在pages.json中需要下拉刷新的頁面,在style配置enablePullDownRefresh為true就可以了(要在 pages.json 里,找到的當(dāng)前頁面的pages節(jié)點(diǎn),并在 style 選項(xiàng)中開啟 enablePullDownRefresh),然后在具體頁面使用onPullDownRefresh回調(diào)函數(shù)就可以了。最后在當(dāng)處理完數(shù)據(jù)刷新后,uni.stopPullDownRefresh 可以停止當(dāng)前頁面的下拉刷新。
官網(wǎng):https://uniapp.dcloud.net.cn/api/ui/pulldown.html#
pages.json:
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "PULLANDREACHDEMO",
"enablePullDownRefresh": true // 開啟下拉刷新
}
}
// 下拉刷新
async onPullDownRefresh() {
console.log('下拉刷新-->>')
this.dataListAll = await this.loadmore()
this.getPageData()
uni.stopPullDownRefresh() // 停止當(dāng)前頁面刷新
},
上拉觸底加載
只需要在style配置onReachBottomDistance 就可以了。頁面上拉觸底事件觸發(fā)時(shí)距頁面底部距離,單位只支持px
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "PULLANDREACHDEMO",
"enablePullDownRefresh": true, // 下拉刷新
"onReachBottomDistance": 50 // 頁面上拉觸底事件觸發(fā)時(shí)距頁面底部距離 觸底加載
}
}
然后頁面中的回調(diào)
// 觸底加載
onReachBottom() {
console.log('觸底加載-->>')
this.status = 'loading'
setTimeout(() => {
this.status = 'nomore'
this.pageNo++
this.getPageData()
}, 500)
},
完整demo
<template>
<view class="index">
<view v-for="(item, index) in dataList" :key="index">
<image :src="item.url" mode=""></image>
<view>列表長度--{{ index + 1 }}</view>
</view>
<u-loadmore :status="status" />
</view>
</template>
<script>
export default {
data() {
return {
pageNo: 1,
pageSize: 20,
dataListAll: [],
dataList: [],
urls: [
'https://cdn.uviewui.com/uview/album/1.jpg',
'https://cdn.uviewui.com/uview/album/2.jpg',
'https://cdn.uviewui.com/uview/album/3.jpg',
'https://cdn.uviewui.com/uview/album/4.jpg',
'https://cdn.uviewui.com/uview/album/5.jpg',
'https://cdn.uviewui.com/uview/album/6.jpg',
'https://cdn.uviewui.com/uview/album/7.jpg',
'https://cdn.uviewui.com/uview/album/8.jpg',
'https://cdn.uviewui.com/uview/album/9.jpg',
'https://cdn.uviewui.com/uview/album/10.jpg',
],
status: 'nomore'
}
},
async onLoad() {
this.dataListAll = await this.loadmore()
this.getPageData()
},
// 下拉刷新
async onPullDownRefresh() {
this.dataListAll = await this.loadmore()
this.pageNo = 1
this.getPageData()
uni.stopPullDownRefresh()
},
// 觸底加載
onReachBottom() {
this.status = 'loading'
setTimeout(() => {
this.status = 'nomore'
this.pageNo++
this.getPageData()
}, 500)
},
methods: {
// 獲取分頁數(shù)據(jù)
getPageData() {
let curDataList = this.dataListAll.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
if(curDataList.length) {
this.dataList.push(...curDataList)
}
},
// 模擬請求 獲取所有數(shù)據(jù)
loadmore() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const dataList = []
for (let i = 0; i < 120; i++) {
dataList.push({
url: this.urls[uni.$u.random(0, this.urls.length - 1)]
})
}
resolve(dataList)
}, 500)
})
}
}
}
</script>
<style scoped>
.index {
width: 100%;
height: 100%;
/* overflow: auto;
overflow-x: hidden; */
}
.index > view {
width: 100%;
height: 120rpx;
border-bottom: 1px solid #ccc;
padding-left: 15rpx;
box-sizing: border-box;
display: flex;
align-items: center;
}
.index > view > image {
width: 100rpx;
height: 100rpx;
border-radius: 12rpx;
margin-right: 10rpx;
}
.index > view > view {
line-height: 120rpx;
font-size: 22rpx;
color: #000000;
}
</style>效果

到此這篇關(guān)于uniapp實(shí)現(xiàn)下拉刷新與上拉觸底加載功能的示例代碼的文章就介紹到這了,更多相關(guān)uniapp下拉刷新 上拉觸底加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序module.exports模塊化操作實(shí)例淺析
這篇文章主要介紹了微信小程序module.exports模塊化操作,結(jié)合實(shí)例形式簡單分析了module.exports模塊化的定義與引用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
javascript實(shí)現(xiàn)博客園頁面右下角返回頂部按鈕
這篇文章主要介紹了使用javascript實(shí)現(xiàn)博客園頁面右下角返回頂部按鈕的思路及源碼,非常不錯(cuò),這里推薦給小伙伴們2015-02-02
信息頁文內(nèi)畫中畫廣告js實(shí)現(xiàn)代碼(文中加載廣告方式)
一般來說文章內(nèi)容中的廣告點(diǎn)擊率更好,也更容易被訪客看到,如果直接將廣告放到頁面頭部會影響網(wǎng)站的速度,所以一般都比較喜歡這種方法,這里分享下實(shí)現(xiàn)方法2016-01-01
微信小程序使用同聲傳譯實(shí)現(xiàn)語音識別功能
語音識別可以將語音精準(zhǔn)識別為文字,在很多場景中都可以使用,本文主要介紹了微信小程序使用同聲傳譯實(shí)現(xiàn)語音識別功能,分享給大家,感興趣的可以了解一下2021-06-06
關(guān)于JavaScript奇怪又實(shí)用的六個(gè)姿勢
這篇文章主要給大家介紹了關(guān)于JavaScript奇怪又實(shí)用的六個(gè)姿勢,這些技巧和建議是我平常在開發(fā)項(xiàng)目上會用到的,希望能讓大家學(xué)到知識,需要的朋友可以參考下2021-10-10
jQuery實(shí)現(xiàn)為控件添加水印文字效果(附源碼)
這篇文章主要介紹了jQuery實(shí)現(xiàn)為控件添加水印文字效果的方法,涉及jQuery插件jquery.tinywatermark.js的使用技巧,并提供了源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
純Javascript實(shí)現(xiàn)Windows 8 Metro風(fēng)格實(shí)現(xiàn)
Windows 8 Metro風(fēng)格設(shè)計(jì),實(shí)現(xiàn)網(wǎng)站或系統(tǒng)功能的導(dǎo)航,在本文將為大家介紹下如何用純Javascript實(shí)現(xiàn)Windows 8 Metro風(fēng)格,感興趣的朋友可以參考下2013-10-10
JavaScript加入收藏夾功能(兼容IE、firefox、chrome)
這篇文章主要介紹了JavaScript加入收藏夾功能,兼容IE、firefox、chrome,并解決了window.sidebar.addPanel is not a function問題,需要的朋友可以參考下2014-05-05

