scroll-view-props.uvue 8.9 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
<template>
H
hdx 已提交
2 3
  <view class="page-scroll-view">
    <page-head title="非下拉刷新的scroll-view属性示例"></page-head>
4 5 6 7 8 9 10

    <!-- 暂时分成两个方向不同的滚动视图,原因为:scroll-view组件不支持动态改变direction。 -->
    <scroll-view v-if="scrollX" direction="horizontal" :scroll-top="scrollTop" :scroll-left="scrollLeft"
      :upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
      :enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation"
      style="flex-direction: row;" class="uni-margin-wrap" :show-scrollbar="showScrollbar" :bounces="bounces"
      @scrolltoupper="scrolltoupper" @scrolltolower="scrolltolower" @scroll="scroll" @scrollend="scrollend"
11 12 13
      ref="scrollViewX" id="scrollViewX">
      <!-- h-margin-right末尾元素添加margin-right 测试bug #3866-->
      <view class="item" :id="'horizontal_'+item.id" v-for="(item,index) in items" :class="index==9 ? 'h-margin-right': ''">
14 15 16 17 18 19 20 21
        <text class="uni-text">{{item.label}}</text>
      </view>
    </scroll-view>
    <scroll-view v-else direction="vertical" :scroll-top="scrollTop" :scroll-left="scrollLeft"
      :upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
      :enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation"
      :show-scrollbar="showScrollbar" :bounces="bounces" @scrolltoupper="scrolltoupper" @touchmove="onTouchMove"
      @scrolltolower="scrolltolower" @scroll="scroll" @scrollend="scrollend" ref="scrollViewY" id="scrollViewY"
22 23 24
      class="uni-margin-wrap">
      <!-- v-margin-bottom末尾元素添加margin-bottom 测试bug #3866-->
      <view class="item" :id="item.id" v-for="(item, index) in items" :class="index==9 ? 'v-margin-bottom': ''">
25 26 27
        <text class="uni-text">{{item.label}}</text>
      </view>
    </scroll-view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
28

29 30 31 32 33
    <scroll-view class="uni-list" :showScrollbar="true" :scroll-y="true">
      <view class="uni-list-cell uni-list-cell-padding">
        <text>点击状态栏回到顶部(仅iOS)</text>
        <switch :checked="enableBackToTop" @change="enableBackToTop=!enableBackToTop"></switch>
      </view>
H
hdx 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      <view class="uni-list-cell uni-list-cell-padding">
        <text>是否显示滚动条</text>
        <switch :checked="showScrollbar" @change="showScrollbar=!showScrollbar"></switch>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>是否有反弹效果</text>
        <switch :checked="bounces" @change="bounces=!bounces"></switch>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>是否开启滚动时使用动画过渡</text>
        <switch :checked="scrollWithAnimation" @change="scrollWithAnimation=!scrollWithAnimation"></switch>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>是否横向滚动</text>
        <switch :checked="scrollX" @change="changeDirectionX"></switch>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>是否竖向滚动</text>
        <switch :checked="scrollY" @change="changeDirectionY"></switch>
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <text>拖动设置scroll-top</text>
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <slider :max="1000" :min="0" :step="10" :value="scrollTop" :show-value="true" @change="handleChangeScrollTop" />
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <text>拖动设置scroll-left</text>
      </view>
      <view class="uni-slider uni-list-cell-padding">
        <slider :max="1000" :min="0" :step="10" :value="scrollLeft" :show-value="true"
          @change="handleChangeScrollLeft" />
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>设置触发scrolltoupper的距离</text>
        <input class="uni-list-cell-input" type="number" :value="upperThreshold" @input="handleUpperThresholdInput" />
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <text>设置触发scrolltolower的距离</text>
        <input class="uni-list-cell-input" type="number" :value="lowerThreshold" @input="handleLowerThresholdInput" />
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <button type="primary" class="button" @click="handleScrollIntoView">
          滚动到id为`item3`的子视图
        </button>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
      </view>
    </scroll-view>
  </view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
84 85 86
</template>

<script>
H
hdx 已提交
87 88 89 90 91 92 93 94 95 96 97 98
  type Item = {
    id : string,
    label : string,
  }
  export default {
    data() {
      return {
        items: [] as Item[],
        scrollX: false,
        scrollY: true,
        bounces: false,
        scrollTop: 0,
99 100
        scrollLeft: 0,
        scrollChangeTop: 0,
H
hdx 已提交
101 102 103 104 105 106 107 108
        scrollIntoView: "",
        enableBackToTop: false,
        scrollWithAnimation: false,
        showScrollbar: true,
        upperThreshold: 50,
        lowerThreshold: 50,
      }
    },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
109

H
hdx 已提交
110 111 112 113 114 115 116 117 118 119
    onLoad() {
      for (let i = 0; i < 10; i++) {
        const item = {
          id: "item" + i,
          label: "item" + i,
        } as Item;
        this.items.push(item);
      }
    },
    methods: {
H
hdx 已提交
120
      handleChangeScrollLeft(e : UniSliderChangeEvent) {
H
hdx 已提交
121 122
        this.scrollLeft = e.detail.value;
      },
H
hdx 已提交
123
      handleChangeScrollTop(e : UniSliderChangeEvent) {
H
hdx 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        this.scrollTop = e.detail.value;
      },
      changeDirectionX() {
        this.scrollX = !this.scrollX;
        if (this.scrollX) {
          this.scrollY = false
        }
        this.scrollTop = 0;
        this.scrollLeft = 0;
      },
      changeDirectionY() {
        this.scrollY = !this.scrollY;
        if (this.scrollY) {
          this.scrollX = false
        }
        this.scrollTop = 0;
        this.scrollLeft = 0;
      },
      handleScrollIntoView() {
        if (this.scrollX) {
          this.scrollIntoView = "horizontal_item3";
        } else {
          this.scrollIntoView = "item3";
        }
        //重置状态,由于响应式的原因,设置的值与当前值相同,会导致再次设置无效果。
        setTimeout(() => {
          this.scrollIntoView = "";
        }, 0);
      },
      handleUpperThresholdInput(e : InputEvent) {
        const value = e.detail.value;
        if (value == "") {
          this.upperThreshold = 50;
        } else {
          this.upperThreshold = parseInt(e.detail.value);
        }
      },
      handleLowerThresholdInput(e : InputEvent) {
        const value = e.detail.value;
        if (value == "") {
          this.lowerThreshold = 50;
        } else {
          this.lowerThreshold = parseInt(e.detail.value);
        }
      },
      //事件监听
      scrolltoupper() {
        console.log("滚动到顶部");
      },
      scrolltolower() {
        console.log("滚动到底部");
      },
176 177
      scroll(e : ScrollEvent) {
        this.scrollChangeTop = e.detail.scrollTop
H
hdx 已提交
178 179 180 181 182 183 184 185
        console.log("scroll-top : " + e.detail.scrollTop + " scroll-left : " + e.detail.scrollLeft);
      },
      scrollend() {
        console.log("滚动停止");
      },
      onTouchMove() {
        console.log("TouchMove");
      },
雪洛's avatar
雪洛 已提交
186
      //自动化测试专用
H
hdx 已提交
187
      checkScrollHeight() : Boolean {
雪洛's avatar
雪洛 已提交
188
        var element = this.$refs["scrollViewY"]
H
hdx 已提交
189
        if (element != null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
190
          var scrollHeight = (element as UniElement).scrollHeight
H
hdx 已提交
191 192 193 194
          console.log("checkScrollHeight" + scrollHeight)
          if (scrollHeight > 1900) {
            return true
          }
雪洛's avatar
雪洛 已提交
195 196 197 198
        }
        return false
      },
      //自动化测试专用
H
hdx 已提交
199
      checkScrollWidth() : Boolean {
雪洛's avatar
雪洛 已提交
200
        var element = this.$refs["scrollViewX"]
H
hdx 已提交
201
        if (element != null) {
DCloud-yyl's avatar
DCloud-yyl 已提交
202
          var scrollWidth = (element as UniElement).scrollWidth
H
hdx 已提交
203 204 205 206
          console.log("checkScrollWidth---" + scrollWidth)
          if (scrollWidth > 1900) {
            return true
          }
雪洛's avatar
雪洛 已提交
207 208
        }
        return false
shutao-dc's avatar
shutao-dc 已提交
209
      }
H
hdx 已提交
210 211
    }
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
212 213 214
</script>

<style>
H
hdx 已提交
215
  .uni-margin-wrap {
216
    height: 250px;
H
hdx 已提交
217 218
    margin: 0 25px 25px 25px;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
219

H
hdx 已提交
220 221 222
  .item {
    justify-content: center;
    align-items: center;
223
    height: 250px;
H
hdx 已提交
224 225 226
    width: 100%;
    background-color: azure;
    border-width: 1px;
xuty73419315's avatar
xuty73419315 已提交
227
    border-style: solid;
H
hdx 已提交
228
    border-color: chocolate;
H
hdx 已提交
229 230 231 232
  }

  .uni-list {
    flex: 1;
H
hdx 已提交
233
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
234

H
hdx 已提交
235 236 237 238
  .uni-text {
    color: black;
    font-size: 50px;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
239

H
hdx 已提交
240 241 242
  .uni-slider {
    justify-content: center;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
243

H
hdx 已提交
244 245 246 247 248
  .uni-list-cell-input {
    width: 50px;
    border: 1px solid #ccc;
    text-align: center;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
249

H
hdx 已提交
250 251 252
  .button {
    flex: 1;
  }
253 254 255 256 257 258 259 260 261

  /*自动化测试专用*/
  .v-margin-bottom {
    margin-bottom: 50px;
  }
  /*自动化测试专用*/
  .h-margin-right {
    margin-right: 50px;
  }
H
hdx 已提交
262
</style>