微信小程序---仿今日头条

源码详见:
git源码

效果图:
效果图

weixinddemo

本demo主要调用了https://api.tianapi.com/social/的api接口,需要申请apikey,将其定义在app.js中,请求中的url格式为https://api.tianapi.com/social/?key=yourKey&num=10&currentPage=X

//api读取数据数据

loadDataFromServer: function(){
    var that = this;
    that.changeLoadingStatus(false);
    app.req(URL,{
        key:app.globalData.apikey,
        num:10,
        page:currentPage,
    },{
        success:function(resp){
            console.log(resp);
            console.log("成功加载页数" + currentPage);
            var tempData = resp.data.newslist;
            var toViewId = currentPage % 2 ==0?         "top-id":"top-id2";
            that.setData({
                renderData: tempData,
                toView: toViewId,
            });
            that.changeLoadingStatus(true);
        }
    });
},

每次上拉调用refresh函数:当前页面减一,下拉调用loadMore函数:当前页面加一,定义在index.js中:
refresh: function(e){
    currentPage = currentPage>1? --currentPage:1;
    this.loadDataFromServer();
},

loadMore: function(e){
    ++currentPage;
    this.loadDataFromServer();
},

除此之外,需要注意的还有flash页面跳转到首页时的setTimeout函数:因为跳转到tabBar的页面中,所以不能用redirectTo和navigateTo,只能用swithTab:

var timestamp = new Date().getTime();
    setTimeout(function(){
        wx.switchTab({
          url: '../index/index'
        });
      },duration*2.5);

index.wxml的view中:

<scroll-view scroll-y="true" style="height:100vh;" scroll-into-view="{{toView}}" upper-threshold="5" 
      lower-threshold="5" bindscrolltoupper="refresh" bindscrolltolower="loadMore">

其中style=“height:100vh;” 不能用100%,后者没有refresh和loadMore的效果。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frank Lin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值