<template>
<section class="demo-container">
<h1>Carousel - 走马灯</h1>
<el-carousel v-model="value" ref="carouse" :autoplay="autoplay" class="carousel-wrap">
<el-carousel-item>
<div class="demo-carousel">1</div>
</el-carousel-item>
<el-carousel-item>
<div class="demo-carousel">2</div>
</el-carousel-item>
<el-carousel-item>
<div class="demo-carousel">3</div>
</el-carousel-item>
<el-carousel-item>
<div class="demo-carousel">4</div>
</el-carousel-item>
</el-carousel>
</section>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'HomeIndex',
data() {
return {
value: 0,
autoplay: true,
}
},
mounted() {
this.slideBanner()
},
methods: {
slideBanner() {
const box = document.querySelector('.carousel-wrap') as Element
const that: any = this
let startPoint = 0
let stopPoint = 0
const resetPoint = () => {
startPoint = 0
stopPoint = 0
}
box.addEventListener('touchstart', (e: any) => {
that.autoplay = false
startPoint = e.changedTouches[0].pageX
})
box.addEventListener('touchmove', (e: any) => {
stopPoint = e.changedTouches[0].pageX
})
box.addEventListener('touchend', () => {
that.autoplay = true
if (stopPoint === 0 || startPoint - stopPoint === 0) {
resetPoint()
return
}
if (startPoint - stopPoint > 0) {
resetPoint()
that.$refs.carouse.next()
return
}
if (startPoint - stopPoint < 0) {
resetPoint()
that.$refs.carouse.prev()
return
}
})
},
},
})
</script>
<style lang="scss">
.demo-container {
h1 {
text-align: center;
height: 100px;
line-height: 80px;
}
.carousel-wrap {
height: 400px;
}
.demo-carousel {
height: 400px;
line-height: 400px;
width: 100%;
text-align: center;
font-size: 30px;
color: #fff;
}
.el-carousel__item {
&:nth-of-type(1) .demo-carousel {
background: #79e4f8;
}
&:nth-of-type(2) .demo-carousel {
background: #a14ae6;
}
&:nth-of-type(3) .demo-carousel {
background: #ff8c11;
}
&:nth-of-type(4) .demo-carousel {
background: #fe0e11;
}
}
}
</style>