scroll-view-refresher-props.uvue 5.2 KB
Newer Older
1
<template>
H
hdx 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
  <view class="page-scroll-view">
    <page-head title="下拉刷新的scroll-view属性示例"></page-head>
    <view class="uni-margin-wrap">
      <scroll-view direction="vertical" :refresher-enabled="refresherEnabled" :refresher-threshold="refresherThreshold"
        :refresher-default-style="refresherDefaultStyle" :refresher-background="refresherBackground"
        :refresher-triggered="refresherTriggered" @refresherpulling="refresherpulling"
        @refresherrefresh="refresherrefresh" @refresherrestore="refresherrestore" @refresherabort="refresherabort"
        style="width: 100%;height: 100%;">
        <view class="item" :id="item.id" v-for="(item,_) in items">
          <text class="uni-text">{{item.label}}</text>
        </view>
      </scroll-view>
    </view>
15

16
    <scroll-view class="uni-list" style="padding-top: 16px;" :showScrollbar="true">
H
hdx 已提交
17 18 19 20
      <view class="uni-list-cell-padding">
        <text class="refresher-tips">**下拉刷新的属性设置需要先打开下拉刷新开关**</text>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
H
hdx 已提交
21 22 23
        <text>是否开启下拉刷新</text>
        <switch :checked="refresherEnabled" @change="handleTrunOnRefresher"></switch>
      </view>
H
hdx 已提交
24
      <view class="uni-list-cell uni-list-cell-padding">
H
hdx 已提交
25 26 27 28
        <text>设置下拉刷新状态</text>
        <switch :disabled="!refresherEnabled" :checked="refresherTriggered"
          @change="refresherTriggered=!refresherTriggered"></switch>
      </view>
29

H
hdx 已提交
30
      <view class="uni-list-cell uni-list-cell-padding">
H
hdx 已提交
31
        <text>设置下拉刷新阈值</text>
H
hdx 已提交
32
        <input class="uni-list-cell-input" :disabled="!refresherEnabled" :value="refresherThreshold" type="number"
H
hdx 已提交
33 34
          @input="handleRefresherThresholdInput" />
      </view>
35

H
hdx 已提交
36
      <view class="uni-list-cell uni-list-cell-padding">
H
hdx 已提交
37
        <text>设置下拉刷新区域背景颜色</text>
H
hdx 已提交
38 39
        <input class="uni-list-cell-input" :disabled="!refresherEnabled" :value="refresherBackground"
          @input="handleRefresherBackground" />
H
hdx 已提交
40
      </view>
41

H
hdx 已提交
42
      <view class="uni-list-cell-padding">
H
hdx 已提交
43
        <text>设置下拉刷新默认样式</text>
H
hdx 已提交
44 45
        <view class="switch-refresher-group">
          <button class="switch-refresher-style" type="primary" size="mini"
H
hdx 已提交
46
            @click="refresherDefaultStyle = `none`">none</button>
H
hdx 已提交
47
          <button class="switch-refresher-style" type="primary" size="mini"
H
hdx 已提交
48
            @click="refresherDefaultStyle = `black`">black</button>
H
hdx 已提交
49
          <button class="switch-refresher-style" type="primary" size="mini"
H
hdx 已提交
50 51 52 53 54
            @click="refresherDefaultStyle = `white`">white</button>
        </view>
      </view>
    </scroll-view>
  </view>
55 56 57
</template>

<script>
H
hdx 已提交
58 59 60 61
  type Item = {
    id : string,
    label : string,
  }
62

H
hdx 已提交
63 64 65 66
  export default {
    data() {
      return {
        items: [] as Item[],
67
        refresherEnabled: true,
H
hdx 已提交
68 69
        refresherTriggered: false,
        refresherThreshold: 45,
70
        refresherDefaultStyle: "black",
H
hdx 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        refresherBackground: "transparent",
      }
    },
    onLoad() {
      for (let i = 0; i < 10; i++) {
        const item = {
          id: "item" + i,
          label: "item" + i,
        } as Item;
        this.items.push(item);
      }
    },
    methods: {
      handleTrunOnRefresher() {
        this.refresherTriggered = false;
        //不能同时关闭下拉状态和关闭下拉刷新。
        setTimeout(() => {
          this.refresherEnabled = !this.refresherEnabled;
        }, 0)
      },
      handleRefresherThresholdInput(e : InputEvent) {
        const value = e.detail.value;
        if (value == "") {
          this.refresherThreshold = 45;
        } else {
          this.refresherThreshold = parseInt(e.detail.value);
        }
      },
      handleRefresherBackground(e : InputEvent) {
        const value = e.detail.value;
        this.refresherBackground = value;
      },
      //响应事件
      refresherpulling() {
        console.log("下拉刷新控件被下拉");
      },
      refresherrefresh() {
        console.log("下拉刷新被触发");
        this.refresherTriggered = true;
        //不能同时关闭下拉状态和关闭下拉刷新。
        setTimeout(() => {
          this.refresherTriggered = false;
        }, 1500)
      },
      refresherrestore() {
        console.log("下拉刷新被复位");
      },
      refresherabort() {
        console.log("下拉刷新被中止");
      }
    }
  }
123 124 125
</script>

<style>
H
hdx 已提交
126 127
  .uni-margin-wrap {
    height: 200px;
H
hdx 已提交
128
    margin: 0 25px 25px 25px;
H
hdx 已提交
129
  }
130

H
hdx 已提交
131 132 133 134 135 136 137
  .item {
    justify-content: center;
    align-items: center;
    height: 200px;
    width: 100%;
    background-color: azure;
    border-width: 1px;
xuty73419315's avatar
xuty73419315 已提交
138
    border-style: solid;
H
hdx 已提交
139 140
    border-color: chocolate;
  }
141

H
hdx 已提交
142 143 144 145 146 147
  .refresher-tips {
    font-size: 12px;
    text-align: center;
    color: red;
  }

H
hdx 已提交
148 149 150 151
  .uni-text {
    color: black;
    font-size: 50px;
  }
152

H
hdx 已提交
153 154 155
  .uni-list {
    flex: 1;
  }
156

H
hdx 已提交
157 158 159 160
  .uni-list-cell-input {
    max-width: 100px;
    border: 1px solid #ccc;
    text-align: center;
H
hdx 已提交
161
  }
162

H
hdx 已提交
163 164 165 166 167
  .picker-view {
    width: 100%;
    height: 320px;
    margin-top: 10px;
  }
H
hdx 已提交
168 169 170 171 172 173 174 175 176 177

  .switch-refresher-group {
    flex-direction: row;
    margin-top: 5px;
  }

  .switch-refresher-style {
    padding: 2px 10px;
    margin-right: 5px;
  }
178
</style>