我使用以下组件或多或少地让它工作:
<!-- components/VSwipable.vue -->
<template>
<div ref="container" class="swiper-container">
<div class="swiper-wrapper">
<div v-if="swipeRight" class="swiper-slide">
<slot name="right-content"/>
</div>
<div class="swiper-slide">
<slot/>
</div>
<div v-if="swipeLeft" class="swiper-slide">
<slot name="left-content"/>
</div>
</div>
</div>
</template>
<script lang="ts">
import Swiper, { SelectableElement } from 'swiper';
import 'swiper/css/swiper.css';
import Vue from 'vue';
export default Vue.extend({
mounted() {
const swiper = new Swiper(this.$refs.container as SelectableElement, {
initialSlide: this.swipeRight ? 1 : 0,
resistanceRatio: 0,
speed: 150,
});
swiper.on('transitionEnd', () => {
// slide index swipe mode
// 0 1 R
// 0 1 2 R L
// 0 1 L
switch (true) {
case this.swipeRight && swiper.activeIndex === 0:
this.$emit('swipedRight');
break;
case this.swipeRight && swiper.activeIndex === 2:
this.$emit('swipedLeft');
break;
case !this.swipeRight && swiper.activeIndex === 1:
this.$emit('swipedLeft');
break;
default:
}
switch (swiper.activeIndex) {
case 0:
this.$emit('swipedRight');
break;
case 2:
this.$emit('swipedLeft');
break;
default:
}
});
},
props: {
swipeLeft: Boolean,
swipeRight: Boolean,
},
});
</script>
以及示例用法:
<template>
<v-container>
<v-list>
<template v-for="item in items">
<!-- swipe-left enablesleft swiping (you can also use swipe-right), it supports two events: swipedLeft and swipedRight -->
<v-swipable :key="item" swipe-left>
<!-- inject a red list item in the left slide -
you can also inject buttons and stuff,
use right-content for the right one -->
<v-list-item class="error" slot="left-content"/>
<v-list-item :value="item">
<v-list-item-content>
<v-list-item-title v-text="item"/>
<v-list-item-subtitle v-text="`Some details about ${item}`"/>
</v-list-item-content>
</v-list-item>
</v-swipable>
</template>
</v-list>
</v-container>
</template>
<script lang="ts">
import VSwipable from '@/components/VSwipable.vue';
export default Vue.extend({
data: () => ({
items: ['Item 1', 'Item 2', 'Item 3']
}),
components: {
VSwipable,
},
});
</script>