swiper.uvue 11.0 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
<template>
H
hdx 已提交
2 3 4 5 6 7
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view>
      <page-head title="swiper,可滑动视图"></page-head>
      <view class="uni-margin-wrap">
8
        <swiper id="swiper-view" class="swiper" :vertical="verticalSelect" :indicator-dots="dotsSelect" :autoplay="autoplaySelect"
H
hdx 已提交
9 10 11
          :rebound="reboundSelect" :interval="intervalSelect" :circular="circularSelect"
          :indicator-color="indicatorColor" :indicator-active-color="indicatorColorActive"
          :disable-touch="disableTouchSelect" :current="currentVal" :current-item-id="currentItemIdVal"
12
          @change="swiperChange" @transition="swiperTransition" @animationfinish="swiperAnimationfinish" @touchstart="swipertouchStart">
H
hdx 已提交
13
          <swiper-item item-id="A">
14
            <view class="swiper-item uni-bg-red"><text class="swiper-item-Text" @touchstart="viewtouchStart">A</text></view>
H
hdx 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
          </swiper-item>
          <swiper-item item-id="B">
            <view class="swiper-item uni-bg-green"><text class="swiper-item-Text">B</text></view>
          </swiper-item>
          <swiper-item item-id="C">
            <view class="swiper-item uni-bg-blue"><text class="swiper-item-Text">C</text></view>
          </swiper-item>
        </swiper>
      </view>
      <view class="uni-list">
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">是否显示面板指示点</view>
          <switch :checked="dotsSelect" @change="dotsChange" />
杜庆泉's avatar
杜庆泉 已提交
28
        </view>
H
hdx 已提交
29 30 31 32 33 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
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">是否自动切换</view>
          <switch :checked="autoplaySelect" @change="autoplayChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">是否循环</view>
          <switch :checked="circularSelect" @change="circularChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">是否显示rebound效果</view>
          <switch :checked="reboundSelect" @change="reboundSelectChange" />
        </view>
        <view class="uni-title uni-list-cell-padding">间隔时间(毫秒)</view>
        <view class="uni-list-cell-padding">
          <slider @change="sliderChange" :value="1000" :min="500" :max="5000" :show-value="true" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">定制指示器颜色</view>
          <switch :checked="indicatorColorSelect" @change="indicatorColorChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">纵向</view>
          <switch :checked="verticalSelect" @change="verticalChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">指定current为最后一个元素</view>
          <switch :checked="currentSelect" @change="currentChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">指定current-item-id为最后一个元素</view>
          <switch :checked="currentItemIdSelect" @change="currentItemIdChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">禁止用户 touch 操作</view>
          <switch :checked="disableTouchSelect" @change="disableTouchChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">swiperTransition 是否打印</view>
          <switch :checked="swiperTransitionSelect" @change="swiperTransitionChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">swiperAnimationfinish 是否打印</view>
          <switch :checked="swiperAnimationfinishSelect" @change="swiperAnimationfinishChange" />
        </view>
        <view class="uni-list-cell uni-list-cell-padding">
          <view class="uni-list-cell-db">swiperChange 是否打印</view>
          <switch :checked="swiperChangeSelect" @change="swiperChangeChange" />
DCloud-WZF's avatar
DCloud-WZF 已提交
76 77 78 79 80 81 82
        </view>

        <navigator url="/pages/component/swiper/swiper-list-view">
          <button type="primary">
            swiper 嵌套 list-view 测试
          </button>
        </navigator>
shutao-dc's avatar
shutao-dc 已提交
83

H
hdx 已提交
84 85 86 87 88
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
89
</template>
H
hdx 已提交
90

DCloud-WZF's avatar
DCloud-WZF 已提交
91 92 93 94 95
<script>
  type SwiperEventTest = {
    type : string;
    target : UniElement | null;
    currentTarget : UniElement | null;
96
  }
H
hdx 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  export default {
    data() {
      return {
        background: ['color1', 'color2', 'color3'],
        dotsSelect: false,
        reboundSelect: false,
        autoplaySelect: false,
        circularSelect: false,
        indicatorColorSelect: false,
        verticalSelect: false,
        currentSelect: false,
        currentItemIdSelect: false,
        intervalSelect: 2000,
        indicatorColor: "",
        indicatorColorActive: "",
        currentVal: 0,
        currentItemIdVal: "",
        disableTouchSelect: false,
        swiperTransitionSelect: false,
        swiperAnimationfinishSelect: false,
        swiperChangeSelect: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
118 119 120 121 122 123 124
        currentValChange: 0,
        // 自动化测试
        changeDetailTest: null as UniSwiperChangeEventDetail | null,
        transitionDetailTest: null as UniSwiperTransitionEventDetail | null,
        animationfinishDetailTest: null as UniSwiperAnimationFinishEventDetail | null,
        isChangeTest: '',
        isTransitionTest: '',
125 126
        isAnimationfinishTest: '',
        swipeX: 0,
127
        swipeY: 0
H
hdx 已提交
128
      }
129 130 131 132 133
    },
    onReady() {
      // #ifndef MP
      // 获取模拟滑动手势的起始点
      let ele = uni.getElementById("swiper-view")
shutao-dc's avatar
shutao-dc 已提交
134 135 136 137
      let eleRect = ele?.getBoundingClientRect()
      if(eleRect != null) {
        this.swipeX = eleRect.width - 10
        this.swipeY += eleRect.y + uni.getSystemInfoSync().safeArea.top + 44 + 35
138 139
      }
      // #endif
DCloud-WZF's avatar
DCloud-WZF 已提交
140
    },
141 142 143 144 145 146
    methods: {
      swipertouchStart(e: UniTouchEvent) {
        console.log("swiper touchstart")
      },
      viewtouchStart(e: UniTouchEvent) {
        console.log("view touchstart:")
147
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
148 149 150 151 152 153 154
      swiperChange: function (e : UniSwiperChangeEvent) {
        this.changeDetailTest = e.detail
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget
        } as SwiperEventTest, 'change')
H
hdx 已提交
155 156 157 158 159 160 161
        this.currentValChange = e.detail.current
        console.log(this.currentValChange)
        if (this.swiperChangeSelect) {
          console.log("swiperChange")
          console.log(e)
        }
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
162 163 164 165 166 167 168
      swiperTransition: function (e : UniSwiperTransitionEvent) {
        this.transitionDetailTest = e.detail
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget
        } as SwiperEventTest, 'transition')
H
hdx 已提交
169 170 171 172 173
        if (this.swiperTransitionSelect) {
          console.log("swiperTransition")
          console.log(e)
        }
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
174 175 176 177 178 179 180
      swiperAnimationfinish: function (e : UniSwiperAnimationFinishEvent) {
        this.animationfinishDetailTest = e.detail
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget
        } as SwiperEventTest, 'animationfinish')
H
hdx 已提交
181 182 183 184 185
        if (this.swiperAnimationfinishSelect) {
          console.log("swiperAnimationfinish")
          console.log(e)
        }
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
186
      // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
187
      checkEventTest(e : SwiperEventTest, eventName : String) {
188
        // #ifndef MP
189 190
        const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement;
        // #endif
191
        // #ifdef MP
192
        const isPass = true;
193
        // #endif
DCloud-WZF's avatar
DCloud-WZF 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
        const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
        switch (eventName) {
          case 'change':
            this.isChangeTest = result
            break;
          case 'transition':
            this.isTransitionTest = result
            break;
          case 'animationfinish':
            this.isAnimationfinishTest = result
            break;
          default:
            break;
        }
208
      },
H
hdx 已提交
209
      dotsChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
210 211
        this.dotsSelect = e.detail.value
      },
H
hdx 已提交
212
      swiperTransitionChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
213 214
        this.swiperTransitionSelect = e.detail.value
      },
H
hdx 已提交
215
      swiperChangeChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
216 217
        this.swiperChangeSelect = e.detail.value
      },
H
hdx 已提交
218
      swiperAnimationfinishChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
219 220
        this.swiperAnimationfinishSelect = e.detail.value
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
221
      autoplayChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
222 223
        this.autoplaySelect = e.detail.value
      },
H
hdx 已提交
224
      verticalChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
225 226
        this.verticalSelect = e.detail.value
      },
H
hdx 已提交
227
      disableTouchChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
228 229
        this.disableTouchSelect = e.detail.value
      },
H
hdx 已提交
230
      currentItemIdChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
231 232 233 234 235 236 237
        this.currentItemIdSelect = e.detail.value
        if (this.currentItemIdSelect) {
          this.currentItemIdVal = 'C'
        } else {
          this.currentItemIdVal = 'A'
        }
      },
H
hdx 已提交
238
      currentChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
239 240 241 242 243 244
        this.currentSelect = e.detail.value
        if (this.currentSelect) {
          this.currentVal = 2
        } else {
          this.currentVal = 0
        }
DCloud-WZF's avatar
DCloud-WZF 已提交
245

杜庆泉's avatar
杜庆泉 已提交
246
      },
H
hdx 已提交
247
      circularChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
248 249 250
        this.circularSelect = e.detail.value
        console.log(this.circularSelect)
      },
H
hdx 已提交
251
      reboundSelectChange: function (e : UniSwitchChangeEvent) {
H
hdx 已提交
252 253 254
        this.reboundSelect = e.detail.value
        console.log(this.reboundSelect)
      },
H
hdx 已提交
255
      sliderChange(e : UniSliderChangeEvent) {
H
hdx 已提交
256 257
        this.intervalSelect = e.detail.value
      },
H
hdx 已提交
258
      indicatorColorChange(e : UniSwitchChangeEvent) {
H
hdx 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271
        this.indicatorColorSelect = e.detail.value
        if (this.indicatorColorSelect) {
          // 选择了定制指示器颜色
          this.indicatorColor = "#ff00ff"
          this.indicatorColorActive = "#0000ff"
        } else {
          // 没有选择颜色
          this.indicatorColor = ""
          this.indicatorColorActive = ""
        }
      }
    }
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
272 273
</script>

H
hdx 已提交
274
<style>
H
hdx 已提交
275 276 277
  .swiper {
    height: 150px;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
278

H
hdx 已提交
279 280 281 282
  .swiper-item {
    width: 100%;
    height: 150px;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
283

H
hdx 已提交
284 285 286 287 288
  .swiper-item-Text {
    width: 100%;
    text-align: center;
    line-height: 150px;
  }
289
</style>