weex scroller滚动列表实践

5 篇文章 0 订阅
5 篇文章 0 订阅

weex 提供了<scroller>滚动组件,可以容纳子组件进行横向或竖向滚动。如果组件需要进行滚动,可以将 <scroller>当作根元素或者父元素使用,否则页面无法滚动( <list> 组件除外, <list> 默认可以滚动)。

使用<scroller>滚动组件,需要注意点的:

  • 不允许相同方向的 <list> 或者 <scroller> 互相嵌套,换句话说就是嵌套的 <list> / <scroller> 必须是不同的方向。
  •  <scroller>需要显式的设置其宽高,可使用 position: absolute; 定位或 widthheight 设置其宽高值。

子组件支持上,<scroller>支持任意类型的 Weex 组件作为其子组件。 其中,还支持以下两个特殊组件作为子组件:

  • <refresh>: 用于添加下拉刷新的功能。
  • <loading>: 用于添加上拉加载更多的功能

使用滚动组件<scroller>具体需要注意的事项见scroller 简介

直接贴代码,利用<scroller>支持的<refresh>、<loading>组件实现,上拉加载下一页,下拉刷新的列表页。

<template>
  <scroller class="scroller"  @loadmore="onloadmore" loadmoreoffset="0">
    <refresh class="refresh" @refresh="onrefresh" @pullingdown="onpullingdown" :display="refreshing ? 'show' : 'hide'">
        <text class="indicator-text">{{pullDownTipTxt}}</text>
        <loading-indicator class="indicator"></loading-indicator>
    </refresh>
    <div class="cell" v-for="num in lists" :key="num">
      <div class="panel">
        <text class="text">{{num}}</text>
      </div>
    </div>
    <loading class="loading" @loading="onloading" :display="loadinging ? 'show' : 'hide'">
      <text class="indicator-text">Loading ...</text>
      <loading-indicator class="indicator"></loading-indicator>
    </loading>
   
  </scroller>
</template>

<script>
  const modal = weex.requireModule('modal')

  export default {
    data () {
      return {
        loadinging: false,       
        refreshing: false,
        lists: [1, 2, 3, 4, 5],
        pullDownTipTxt: '下来刷新'                  // 下拉加载默认文案
      }
    },
    methods: {
      onloading (event) {
        console.log('--------onloading--------');
        modal.toast({ message: 'Loading', duration: 1 })
        this.loadinging = true
        setTimeout(() => {
          this.loadinging = false
        }, 2000)
      },
      onrefresh (event) {
        console.log('---------onrefresh------');
        modal.toast({ message: 'Refreshing', duration: 1 })
        this.refreshing = true;
        this.pullDownTipTxt = '刷新中...';
        this.lists = [];
        setTimeout(() => {
          this.refreshing = false
        }, 2000)
        this.getDataList();
      },
      onpullingdown (event) {    
        console.log('---------onpullingdown------');  
        if (event.pullingDistance > 50) {
          this.pullDownTipTxt = '松开立即刷新';
        } else {
          this.pullDownTipTxt = '下来刷新';
        }        
      },
      onloadmore: function (e) {
        const self = this;        
        console.log('---------onloadmore------');   
        this.getDataList();       
      },
      getDataList() {
        let data_list = Object.keys(Array.apply(null,{length: 5}));  // 随机生成5个数
        this.lists = this.lists.concat(data_list);
      }
    }
  }
</script>

<style scoped>
  .loading {
    width: 750;
    display: -ms-flex;
    display: -webkit-flex;
    display: flex;
    -ms-flex-align: center;
    -webkit-align-items: center;
    -webkit-box-align: center;
    align-items: center;
  }
  .indicator-text {
    color: #888888;
    font-size: 42px;
    text-align: center;
  }
  .indicator {
    margin-top: 16px;
    height: 40px;
    width: 40px;
    color: blue;
  }
  .panel {
    width: 600px;
    height: 250px;
    margin-left: 75px;
    margin-top: 35px;
    margin-bottom: 35px;
    flex-direction: column;
    justify-content: center;
    border-width: 2px;
    border-style: solid;
    border-color: #DDDDDD;
    background-color: #F5F5F5;
  }
  .text {
    font-size: 50px;
    text-align: center;
    color: #41B883;
  }
</style>

 

参考资料:

scroller简介

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值