轮播图+视频
代码
<el-carousel
id="carouselFit"
ref="slideCarousel"
:interval="6000"
:autoplay="carouselAutoplay"
:height="`${carouselHeight}px`"
@change="carouselChange"
>
<template v-if="videoIntroduce.length > 0">
<el-carousel-item
v-for="(item, index) in videoIntroduce"
:key="index"
style="text-align: center"
>
<video
ref="carouselVideo"
:src="item.link"
width="100%"
height="100%"
controls
@play="videoPlay"
@pause="videoPause"
@playing="videoPlay"
/>
</el-carousel-item>
</template>
<el-carousel-item v-for="(item, index) in photoIntroduce" :key="index">
<img :src="item.link" class="image" >
</el-carousel-item>
</el-carousel>
carouselChange() {
if (this.$refs.carouselVideo && this.$refs.carouselVideo.pause) {
this.$refs.carouselVideo.pause()
}
},
videoPlay(e) {
this.carouselAutoplay = false
},
videoPause(e) {
this.carouselAutoplay = true
}
移动端添加左右滑动
添加监听
mounted(){
//调用方法
this.slideBanner()
},
methods:{
// 滑动切换
slideBanner() {
//选中的轮播图
var box = document.querySelector('.el-carousel__container');
var startPoint = 0;
var stopPoint = 0;
//重置坐标
var resetPoint = function () {
startPoint = 0;
stopPoint = 0;
}
//手指按下
box.addEventListener("touchstart", function (e) {
//手指点击位置的X坐标
startPoint = e.changedTouches[0].pageX;
});
//手指滑动
box.addEventListener("touchmove", function (e) {
//手指滑动后终点位置X的坐标
stopPoint = e.changedTouches[0].pageX;
});
//当手指抬起的时候,判断图片滚动离左右的距离
let that = this
box.addEventListener("touchend", function (e) {
console.log("1:" + startPoint);
console.log("2:" + stopPoint);
if (stopPoint == 0 || startPoint - stopPoint == 0) {
resetPoint();
return;
}
if (startPoint - stopPoint > 0) {
resetPoint();
that.$refs.slideCarousel.next();
return;
}
if (startPoint - stopPoint < 0) {
resetPoint();
that.$refs.slideCarousel.prev();
return;
}
});
},
}
使用touchstart和touchmove方法
<el-carousel ref="nop" arrow="never" height="180px" :autoplay="type">
<div @touchstart="start($event)" @touchmove="move($event)">
<el-carousel-item class="el_carousel__item_style" v-for="item in list">
<el-image class="el_carousel__item_image" :src="item.value"></el-image>
</el-carousel-item>
</div>
</el-carousel>
start(e) {
this.startX = e.touches[0].clientX;
this.startY = e.touches[0].clientY;
},
move(e) {
this.moveX = e.touches[0].clientX;
this.moveY = e.touches[0].clientY;
var nowtime = new Date().getTime();
if (this.startTime == undefined || nowtime > this.startTime) {
if (this.startX - this.moveX <= 0) { // 右滑触发
this.prev();
return false;
} else {
this.next();
return false;
}
}
},
prev() {
this.$refs.nop.prev();
this.startTime = new Date().getTime() + 500;
},
next() {
this.$refs.nop.next();
this.startTime = new Date().getTime() + 500;
},
使用ElementUi中Carousel走马灯实现H5轮播图,可手动左右滑动图片 [https://blog.51cto.com/u_15064630/4383920] @touchstart="start($event)" @touchmove="move($event)"
