上拉加载下拉刷新 Pull

基本用法

**注意:因为仅支持触摸事件,所以请打开chrome手机模拟查看demo

下拉 Pull - 图1

  1. <template lang="html">
  2. <main>
  3. <div class="demo-hidden">
  4. <img src='https://static.zhongan.com/website/health/zarm/images/icons/state.png' :style='{width:0,height:0}' />
  5. <za-panel>
  6. <za-panel-header title="基本"></za-panel-header>
  7. <za-panel-body>
  8. <za-pull :on-refresh='refresh(1)' :refreshing='refreshing1'>
  9. <za-cell v-for='(i, index) in myData1' :key='index'>ID号 {{i}} </za-cell>
  10. </za-pull>
  11. </za-panel-body>
  12. </za-panel>
  13. <za-panel>
  14. <za-panel-header title="上拉加载下拉刷新 Pull"></za-panel-header>
  15. <za-panel-body>
  16. <za-pull :on-refresh='refresh(2)' :refreshing='refreshing2' :loading='loading' :on-load='loadData'>
  17. <za-cell v-for='(i, index) in myData2' :key='index'>ID号 {{i}} </za-cell>
  18. <!-- 此处的几个slot用来覆盖默认样式,定义的会覆盖,不定义的依然使用默认样式 -->
  19. <template slot-scope='props' slot='refreshPull'>
  20. <div class='custom-control' :style='{
  21. transform: `scale(${props.percent / 100})`
  22. }'>
  23. <img src='https://static.zhongan.com/website/health/zarm/images/icons/state.png' alt="" />
  24. </div>
  25. </template>
  26. <template slot-scope='props' slot='refreshDrop'>
  27. <div class='custom-control'>
  28. 释放加载
  29. </div>
  30. </template>
  31. <template slot-scope='props' slot='refreshLoading'>
  32. <div class='custom-control'>
  33. <za-spinner class="rotate360" />
  34. </div>
  35. </template>
  36. <template slot-scope='props' slot='refreshSuccess'>
  37. <div class='custom-control'>
  38. 加载成功
  39. </div>
  40. </template>
  41. </za-pull>
  42. </za-panel-body>
  43. </za-panel>
  44. </div>
  45. </main>
  46. </template>
  47. <script>
  48. let times = 0;
  49. export default {
  50. mounted() {
  51. this.refreshing2 = true
  52. setTimeout(() => {
  53. this.myData2 = [1,2,3,4,5,6,7,8,9,10]
  54. this.refreshing2 = false
  55. }, 1500)
  56. },
  57. data() {
  58. return {
  59. myData1: [1,2,3,4],
  60. myData2: [],
  61. refreshing1: false,
  62. refreshing2: false,
  63. loading: false,
  64. }
  65. },
  66. methods: {
  67. random(length){
  68. const newData = [];
  69. for(let i = 0; i < length; i++){
  70. newData.push(Math.round(Math.random() * 100))
  71. }
  72. return newData;
  73. },
  74. refresh(index) {
  75. return () => new Promise((resolve, reject) => {
  76. this[`refreshing${index}`] = true;
  77. setTimeout(() => {
  78. let length = index == 1 ? this.myData1.length : this.myData2.length
  79. this[`myData${index}`] = this.random(length);
  80. resolve(true);
  81. this[`refreshing${index}`] = false;
  82. }, 1000)
  83. })
  84. },
  85. loadData() {
  86. this.loading = true
  87. return new Promise((resolve, reject) => {
  88. setTimeout(() => {
  89. if (Math.random() > 0.8) {
  90. return reject(false); // capture error and reject it
  91. };
  92. if(times < 2){
  93. const length = this.myData2.length + 1
  94. for(let i = 0; i < 10; i++) {
  95. this.myData2.push( length + i);
  96. }
  97. resolve(true) // has more
  98. }else{
  99. resolve(false) // no more
  100. }
  101. this.loading = false
  102. times++;
  103. }, 1200)
  104. })
  105. }
  106. }
  107. }
  108. </script>

上拉加载下拉刷新(自定义提示内容)

使用 scopedSlots 来覆盖默认的样式

上拉刷新的几个状态分别为 refreshPull, refreshDrop, refreshLoading, refreshSuccess, refreshFailure 分别对应各自的 scopedSlots。

下拉加载的几个状态分别为 loadComplete, loadLoading, loadFailure 分别对应各自的 scopedSlots。

scopedSlots 用来覆盖默认样式,定义的会覆盖,不定义的默认使用内定样式

<template>
<za-panel-body>
  <za-pull :on-refresh='refresh' :refreshing='refreshing' :loading='loading' :on-load='loadData'>
    <za-cell v-for='i in myData' :key='i'>第 {{i}} 行</za-cell>

    <template scope='props' slot='refreshPull'>
      <div class='custom-control' :style='{
        transform: `scale(${props.percent / 100})`
        }'>
        <img src='https://static.zhongan.com/website/health/zarm/images/icons/state.png' alt="" />
      </div>
    </template>

    <template scope='props' slot='refreshDrop'>
      <div class='custom-control'>
        释放加载
      </div>
    </template>

    <template scope='props' slot='refreshLoading'>
      <div class='custom-control'>
        <za-spinner class="rotate360" />
      </div>
    </template>

    <template scope='props' slot='refreshSuccess'>
      <div class='custom-control'>
        加载成功
      </div>
    </template>

  </za-pull>
</za-panel-body>
</template>
<script>
export default{
    data() {
      return {
        myData: [1,2,3,4],
        refreshing: false,
        loading: false,
      }
    },
    methods: {
      random(length){
        const newData = [];
        for(let i = 0; i < length; i++){
          newData.push(Math.round(Math.random() * 100))
        }
        return newData;
      },
      refresh() {
        this.refreshing = true;
        return new Promise((resolve, reject) => {
          fetch().then(res => {
            this.refreshing = false;
            resolve(true);
          }).catch(e => {
            this.refreshing = false;
            reject(false)
          })
        })
      },
      loadData() {
        this.loading = true
        return new Promise((resolve, reject) => {
          fetch().then(res => {
            this.loading = false;
            resolve(true);
            // or resolve(false);
          }).catch(e => {
            this.loading = false;
            reject(false)
          })
        })
      }
    },
}
</script>

on-refresh, on-load 这两个回调函数要求返回一个 Promise 对象。对于on-refresh来说,resolve时表示刷新成功,reject则表示刷新失败。而对于on-load来说,resolve(true) 表示加载成功,并且还有更多数据,resolve(false) 表示加载成功,但后续没有数据了。reject则表示加载失败。

注意: zarm-vue 本身并不带有 Promise 库,请您根据自己的运行环境决定是否需要用引入 Promise Pollyfill.

API

Pull Attributes

属性类型默认值可选值/参数说明
prefixClsstringza-pull类名前缀
refreshingboolfalse是否正在刷新
loadingboolfalse是否正在加载
refreshInitDistancenumber20下拉助跑距离
refreshDistancenumber60下拉距离阀值
on-refreshfunc达到阀值后释放触发刷新的回调函数
on-loadfunc下拉加载的回调函数
durationnumber300动画执行时间,单位:ms
stayTimenumber1500加载成功停留时间