scroll-view.uvue 6.2 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1
<template>
H
hdx 已提交
2 3 4 5 6 7 8 9 10 11 12 13
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view>
      <page-head title="scroll-view,区域滚动视图"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-title uni-common-mt">
          <text class="uni-title-text">Vertical Scroll</text>
          <text class="uni-subtitle-text">纵向滚动</text>
        </view>
        <view>
          <scroll-view :scroll-top="scrollTop" direction="vertical" class="scroll-Y" scroll-with-animation="true"
14
            @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll" @scrollend="end" :show-scrollbar="showScrollbar">
H
hdx 已提交
15 16 17 18 19 20 21 22
            <view class="scroll-view-item uni-bg-red"><text class="text">A</text></view>
            <view class="scroll-view-item uni-bg-green"><text class="text">B</text></view>
            <view class="scroll-view-item uni-bg-blue"><text class="text">C</text></view>
          </scroll-view>
        </view>
        <view @tap="goTop" class="uni-center uni-common-mt">
          <text class="uni-link">点击这里返回顶部</text>
        </view>
shutao-dc's avatar
shutao-dc 已提交
23

H
hdx 已提交
24 25 26 27 28
        <view class="uni-title uni-common-mt">
          <text class="uni-title-text">Horizontal Scroll</text>
          <text class="uni-subtitle-text">横向滚动</text>
        </view>
        <view>
Anne_LXM's avatar
Anne_LXM 已提交
29
          <scroll-view class="scroll-view_H" direction="horizontal" @scroll="scroll" @scrollend="end" :scroll-left="scrollLeft" :show-scrollbar="showScrollbar">
H
hdx 已提交
30 31 32 33 34
            <view class="scroll-view-item_H uni-bg-red"><text class="text">A</text></view>
            <view class="scroll-view-item_H uni-bg-green"><text class="text">B</text></view>
            <view class="scroll-view-item_H uni-bg-blue"><text class="text">C</text></view>
          </scroll-view>
        </view>
shutao-dc's avatar
shutao-dc 已提交
35

H
hdx 已提交
36 37 38 39 40 41
        <navigator url="/pages/component/scroll-view/scroll-view-props" hover-class="none">
          <button type="primary" class="button">
            非下拉刷新的属性示例
          </button>
        </navigator>
        <view class="uni-common-pb"></view>
雪洛's avatar
雪洛 已提交
42

H
hdx 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        <navigator url="/pages/component/scroll-view/scroll-view-refresher-props" hover-class="none">
          <button type="primary" class="button">
            下拉刷新的属性示例
          </button>
        </navigator>
        <view class="uni-common-pb"></view>
        <navigator url="/pages/component/scroll-view/scroll-view-refresher" hover-class="none">
          <button type="primary" class="button"> 默认下拉刷新示例 </button>
        </navigator>
        <view class="uni-common-pb"></view>
        <navigator url="/pages/component/scroll-view/scroll-view-custom-refresher-props" hover-class="none">
          <button type="primary" class="button">
            自定义下拉刷新示例
          </button>
        </navigator>
        <view class="uni-common-pb"></view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
shutao-dc's avatar
shutao-dc 已提交
64 65
</template>
<script lang="uts">
66 67 68 69 70 71
  type ScrollEventTest = {
    type: string;
    target: UniElement | null;
    currentTarget: UniElement | null;
    direction?:string
  }
H
hdx 已提交
72 73 74 75 76
  export default {
    data() {
      return {
        scrollTop: 0,
        oldScrollTop: 0,
77 78 79
        scrollLeft:120,
        showScrollbar: true,
        // 自动化测试
80 81 82 83 84
        isScrollTest:'',
        isScrolltolowerTest:'',
        isScrolltoupperTest:'',
        scrollDetailTest:null as UniScrollEventDetail|null,
        scrollEndDetailTest:null as UniScrollEventDetail|null,
H
hdx 已提交
85 86 87
      }
    },
    methods: {
Anne_LXM's avatar
Anne_LXM 已提交
88
      upper: function (e : UniScrollToUpperEvent) {
89
        console.log('滚动到顶部/左边',e)
90 91 92 93 94 95
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          direction:e.detail.direction,
        } as ScrollEventTest,'scrolltoupper')
H
hdx 已提交
96
      },
Anne_LXM's avatar
Anne_LXM 已提交
97
      lower: function (e : UniScrollToLowerEvent) {
98
        console.log('滚动到底部/右边',e)
99 100 101 102 103 104
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          direction:e.detail.direction,
        } as ScrollEventTest,'scrolltolower')
H
hdx 已提交
105
      },
Anne_LXM's avatar
Anne_LXM 已提交
106
      scroll: function (e : UniScrollEvent) {
107 108 109 110 111 112
        this.scrollDetailTest = e.detail
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget
        } as ScrollEventTest,'scroll')
H
hdx 已提交
113 114
        this.oldScrollTop = e.detail.scrollTop
      },
Anne_LXM's avatar
Anne_LXM 已提交
115 116
      end: function (e : UniScrollEvent){
        console.log('滚动结束时触发',e)
117 118 119 120 121 122
        this.scrollEndDetailTest = e.detail
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget
        } as ScrollEventTest,'scrollend')
Anne_LXM's avatar
Anne_LXM 已提交
123
      },
H
hdx 已提交
124 125 126 127 128 129 130 131 132 133
      goTop: function () {
        // 解决view层不同步的问题
        this.scrollTop = this.oldScrollTop
        this.$nextTick(function () {
          this.scrollTop = 0
        })
        uni.showToast({
          icon: 'none',
          title: '纵向滚动 scrollTop 值已被修改为 0',
        })
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      },
      // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
      checkEventTest(e:ScrollEventTest, eventName:String){
        const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement;
        const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
        switch (eventName){
          case 'scroll':
            this.isScrollTest = result
            break;
          case 'scrolltolower':
            this.isScrolltolowerTest =  result + `-${e.direction}`
            break;
          case 'scrolltoupper':
            this.isScrolltoupperTest =  result + `-${e.direction}`
            break;
          default:
            break;
        }
      },
H
hdx 已提交
153 154
    },
  }
shutao-dc's avatar
shutao-dc 已提交
155 156 157
</script>

<style>
H
hdx 已提交
158 159 160
  .scroll-Y {
    height: 150px;
  }
shutao-dc's avatar
shutao-dc 已提交
161

H
hdx 已提交
162 163 164 165
  .scroll-view_H {
    width: 100%;
    flex-direction: row;
  }
shutao-dc's avatar
shutao-dc 已提交
166

H
hdx 已提交
167 168 169 170 171
  .scroll-view-item {
    height: 150px;
    justify-content: center;
    align-items: center;
  }
shutao-dc's avatar
shutao-dc 已提交
172

H
hdx 已提交
173 174 175 176 177 178
  .scroll-view-item_H {
    width: 100%;
    height: 150px;
    justify-content: center;
    align-items: center;
  }
shutao-dc's avatar
shutao-dc 已提交
179

H
hdx 已提交
180 181 182 183
  .text {
    font-size: 18px;
    color: #ffffff;
  }
shutao-dc's avatar
shutao-dc 已提交
184

H
hdx 已提交
185 186 187
  .button {
    margin-top: 15px;
  }
shutao-dc's avatar
shutao-dc 已提交
188
</style>