小程序 下拉刷新 上滑加載更多

1致份、首先在項目的app.json文件中注冊一個demo頁面

{
"pages": ["pages/demo/demo"]
  ...
}

2津坑、直接上代碼
(1) demo.wxml

<view class='order-lists'>
    <view>
        <view class="result-item" wx:for="{{requestOrderList}}" wx:key="unique" data-data="{{item}}">
            <text class="title">{{item.name}}</text>
        </view>
        <view class="loading" hidden="{{!requestLoading}}">正在載入更多...</view>
        <view class="loading complete" hidden="{{!requestLoadingComplete}}">已加載全部</view>
    </view>
</view>

(2) demo.wxss 有寫樣式為了方便沒有用,但不會影響效果

/* pages/demo/demo.wxss */

page {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/*滾動列表*/

.order-lists {
    flex: auto;
    position: relative;
    border: 1px solid #000;
}

.order-lists scroll-view {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    top: 0;
}

.result-item {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 300rpx;
    align-items: center;
    justify-content: center;
    /* padding: 20rpx 0 20rpx 110rpx; */
    overflow: hidden;
    border-bottom: 2rpx solid #e5e5e5;
    background-color: #fff;
}

.result-item .media {
    position: absolute;
    left: 16rpx;
    top: 16rpx;
    width: 80rpx;
    height: 80rpx;
    border-radius: 999rpx;
}

.result-item .title, .result-item .subtitle {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 36rpx;
}

.result-item .title {
    margin-bottom: 4rpx;
    color: #000;
}

.result-item .subtitle {
    color: #808080;
    font-size: 24rpx;
}

.result-item:first-child .subtitle text {
    margin-right: 20rpx;
}

.result-item:not(:first-child) .subtitle text:not(:first-child):before {
    content: '/';
    margin: 0 8rpx;
}

.loading {
    padding: 10rpx;
    text-align: center;
}

.loading:before {
    display: inline-block;
    margin-right: 5rpx;
    vertical-align: middle;
    content: '';
    width: 40rpx;
    height: 40rpx;
    background-color: #ddd;
    animation: rotate 1s linear infinite;
}

.loading.complete:before {
    display: none;
}

.back-top-button {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50rpx;
    position: absolute;
    right: 40rpx;
    bottom: 200rpx;
    background-color: rgba(0, 0, 0, 0.5)
}

(3) demo.js

// pages/demo/demo.js
let Mock = require('../../lib/dist/mock.js');
/*數(shù)據(jù)源來自于mock.js數(shù)據(jù)蝇摸,調(diào)用url: http://rapapi.org/mockjs/17332/api/list?accessToken=l
// mock.js =>> 自己去mock.js官網(wǎng)下載并項目導入橘洞,這里只是解析數(shù)據(jù)
// 因為是 http協(xié)議傳輸捌袜,小程序是https , 你需要先小程序開發(fā)工具勾選不校驗安全域名才能獲取到數(shù)據(jù)
*/
Page({
    data: {
        screenHeight: 0,
        requestOrderList: [], // 獲取訂單的數(shù)據(jù)
        requestPageNum: 1,   // 設置加載的第幾次,默認是第一次  
        callbackcount: 10,      //返回數(shù)據(jù)的個數(shù) 震檩,前端自己可以定制返回數(shù)據(jù)的個數(shù)
        requestLoading: false, //"上拉加載"的變量琢蛤,默認false蜓堕,隱藏  
        requestLoadingComplete: false  //“沒有數(shù)據(jù)”的變量,默認false博其,隱藏  
    },

    /**
     * 請求數(shù)據(jù)封裝
     */
    fetchOrderList: function () {
        let that = this;
        let requestPageNum = this.data.requestPageNum, // 第幾次加載數(shù)據(jù)(第幾頁)
            callbackcount = this.data.callbackcount; //返回數(shù)據(jù)的個數(shù)(一次性取數(shù)據(jù)的個數(shù))
        wx.request({
            url: 'http://rapapi.org/mockjs/17332/api/list?accessToken=l',
            data: {
                pageNum: requestPageNum,
                pageSize: callbackcount
            },
            success: function (res) {
                let resData = Mock.mock(res.data.data);
                let pageNum = Math.ceil(resData.info.total / resData.info.pageSize); //rap數(shù)據(jù)總頁碼
                console.log(resData, 'rap數(shù)據(jù)');
                console.log(pageNum, 'rap數(shù)據(jù)總頁碼');
                console.log(that.data.requestPageNum, '當前第幾頁')

                let screenHeight = that.data.screenHeight;
                let screenOrderNum = parseInt(screenHeight / 150);
                console.log(screenOrderNum, '滿屏最多能放幾個卡片')
                // 9 >= 1, 2, 3 知道 currentPage 大于 rap數(shù)據(jù)總頁碼
                if (pageNum >= that.data.requestPageNum && (resData.results.length > screenOrderNum)) {  
                    that.setData({
                        requestLoading: true,
                        requestOrderList: that.data.requestOrderList.concat(resData.results)
                    });
                } else if (pageNum >= that.data.requestPageNum && (resData.results.length <= screenOrderNum)) {
                    that.setData({
                        requestLoadingComplete: false,
                        requestLoading: false,
                        requestOrderList: that.data.requestOrderList.concat(resData.results)
                    })
                } else {
                    that.setData({
                        requestLoadingComplete: true,
                        requestLoading: false
                    })
                }
                wx.hideLoading();
                wx.stopPullDownRefresh();
            }
        })
    },
    /**
     * 生命周期函數(shù)--監(jiān)聽頁面加載
     */
    onLoad: function (options) {
        // 獲取系統(tǒng)高度套才,判斷正在加載中是否顯示, 每個卡片的高度是300rpx;
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                console.log(res.windowHeight, 'screenHeight')
                that.setData({
                    screenHeight: res.windowHeight
                })
            },
        })

        this.fetchOrderList(); // 第一次請求數(shù)據(jù)
    },

    /**
     * 頁面相關事件處理函數(shù)--監(jiān)聽用戶下拉動作
     */
    onPullDownRefresh: function () {
        console.log("page下拉動作")
        wx.showLoading({
            title: '努力加載中',
        });
        this.setData({
            requestPageNum: 1,
            requestOrderList: [],
            requestLoading: false,
            requestLoadingComplete: false
        });
        this.fetchOrderList();
    },

    /**
     * 頁面上拉觸底事件的處理函數(shù)
     */
    onReachBottom: function () {
        console.log("page上拉觸底")
        let that = this;
        if (that.data.requestLoading && !that.data.requestLoadingComplete) {
            that.setData({
                requestPageNum: that.data.requestPageNum + 1,  //每次觸發(fā)上拉事件,把requestPageNum+1
            })
            that.fetchOrderList();
        }
    }
})

(3) wx.json 中開啟下拉屬性,否則不能調(diào)用頁面下拉刷新

{
    "enablePullDownRefresh": true
}

3慕淡、效果圖


gifeditor_20180303_135924.gif

初次編寫如有意見請指點

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末背伴,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子峰髓,更是在濱河造成了極大的恐慌傻寂,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件携兵,死亡現(xiàn)場離奇詭異疾掰,居然都是意外死亡,警方通過查閱死者的電腦和手機徐紧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門静檬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人并级,你說我怎么就攤上這事拂檩。” “怎么了嘲碧?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵稻励,是天一觀的道長。 經(jīng)常有香客問我愈涩,道長望抽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任钠署,我火速辦了婚禮糠聪,結果婚禮上,老公的妹妹穿的比我還像新娘谐鼎。我一直安慰自己,他們只是感情好趣惠,可當我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布狸棍。 她就那樣靜靜地躺著,像睡著了一般味悄。 火紅的嫁衣襯著肌膚如雪草戈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天侍瑟,我揣著相機與錄音唐片,去河邊找鬼丙猬。 笑死,一個胖子當著我的面吹牛费韭,可吹牛的內(nèi)容都是我干的茧球。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼星持,長吁一口氣:“原來是場噩夢啊……” “哼抢埋!你這毒婦竟也來了?” 一聲冷哼從身側響起督暂,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤揪垄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后逻翁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饥努,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年八回,在試婚紗的時候發(fā)現(xiàn)自己被綠了肪凛。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡辽社,死狀恐怖伟墙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情滴铅,我是刑警寧澤戳葵,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站汉匙,受9級特大地震影響拱烁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜噩翠,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一戏自、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伤锚,春花似錦擅笔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狞洋,卻和暖如春弯淘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吉懊。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工庐橙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留假勿,地道東北人。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓态鳖,卻偏偏與公主長得像转培,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子郁惜,可洞房花燭夜當晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容