WeChat ミニ プログラムの開発: プルダウンして更新し、プルアップしてさらに読み込む

高洛峰
リリース: 2017-02-16 10:15:40
オリジナル
2038 人が閲覧しました

この記事では、WeChat ミニプログラムで更新するプルダウンとさらに読み込むためのプルアップを実装する方法を記録します

まずはインターフェースを見てみましょう

微信小程序开发之 下拉刷新,上拉加载更多 微信小程序开发之 下拉刷新,上拉加载更多

大体こんな感じです。 このデモでは、WeChat のいくつかの API とイベントを使用します。最初にそれらをリストします。

1.wx.request (リモートサーバーからデータを取得します。$.ajax として理解できます)

2. スクロールビューの 2 つのイベント

2.1 bindingscrollto lower (ページの一番下にスライドするとき)

2.2 bindingscroll (ページをスライドするとき)

2.3 bindingscrolltoupper (ページの先頭にスライドするとき)

次に、コードを見て詳しく説明します。

index.js

var url = "http://www.imooc.com/course/ajaxlist";var page =0;var page_size = 20;var sort = "last";var is_easy = 0;var lange_id = 0;var pos_id = 0;var unlearn = 0;// 获取数据的方法,具体怎么获取列表数据大家自行发挥var GetList = function(that){
    that.setData({
        hidden:false
    });
    wx.request({
        url:url,
        data:{
            page : page,
            page_size : page_size,
            sort : sort,
            is_easy : is_easy,
            lange_id : lange_id,
            pos_id : pos_id,
            unlearn : unlearn
        },
        success:function(res){            //console.info(that.data.list);
            var list = that.data.list;            for(var i = 0; i < res.data.list.length; i++){
                list.push(res.data.list[i]);
            }
            that.setData({
                list : list
            });
            page ++;
            that.setData({
                hidden:true
            });
        }
    });
}
Page({
  data:{
    hidden:true,
    list:[],
    scrollTop : 0,
    scrollHeight:0
  },
  onLoad:function(){    //   这里要非常注意,微信的scroll-view必须要设置高度才能监听滚动事件,所以,需要在页面的onLoad事件中给scroll-view的高度赋值
      var that = this;
      wx.getSystemInfo({
          success:function(res){
              console.info(res.windowHeight);
              that.setData({
                  scrollHeight:res.windowHeight
              });
          }
      });
  },
  onShow:function(){    //   在页面展示之后先获取一次数据
    var that = this;
    GetList(that);
  },
  bindDownLoad:function(){    //   该方法绑定了页面滑动到底部的事件
      var that = this;
      GetList(that);
  },
  scroll:function(event){    //   该方法绑定了页面滚动时的事件,我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
     this.setData({
         scrollTop : event.detail.scrollTop
     });
  },
  refresh:function(event){    //   该方法绑定了页面滑动到顶部的事件,然后做上拉刷新
      page = 0;      this.setData({
          list : [],
          scrollTop : 0
      });
      GetList(this)
  }
})
ログイン後にコピー

index.wxml

<view class="container">
    <scroll-view scroll-top="{{scrollTop}}" scroll-y="true" style="height:{{scrollHeight}}px;" 
        class="list" bindscrolltolower="bindDownLoad" bindscroll="scroll" bindscrolltoupper="refresh">
        <view class="item" wx:for="{{list}}">
            <image class="img" src="{{item.pic_url}}"></image>
            <view class="text">
                <text class="title">{{item.name}}</text>
                <text class="description">{{item.short_description}}</text>
            </view>
        </view>
    </scroll-view>
    <view class="body-view">
        <loading hidden="{{hidden}}" bindchange="loadingChange">
            加载中...        </loading>
    </view></view>
ログイン後にコピー

その他のWeChatミニプログラム開発は、プルダウンして更新し、プルアップして関連をロードしてくださいに注意を払うPHPで記事を中国語サイトに!

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!