1.效果
2.代码展示
<template>
<view>
<!--轮播图-->
<swiper :circular='true' @change="videoplayObj.pause()" indicator-dots="true" indicator-color="#FCF3F3" indicator-active-color="#92C300" :autoplay="true" interval='5000' class='swiper-view'>
<swiper-item class="swiper-img" v-for="(item,index) in sliderImageArr" :key="index" >
<image mode='aspectFill' v-if="testingUrlIsImgOrVideo(item)=='image'" :src="item" @click='lookImg(item)'></image>
<video style="width: 100% !important;height: 530rpx;" v-if="testingUrlIsImgOrVideo(item.url)=='video'" id="myVideo" ref="myVideoRef"
:src="item"
:show-center-play-btn="true" :controls="true" object-fit="cover" >
</video>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
components: {
},
data() {
return {
sliderImageArr:['2022-08-26/202208261008569281562985416978497537.mp4','2.png'],//图片/视频路径我随便乱写的
videoplayObj: {
}, //video对象
}
},
onLoad(option) {
this.videoplayObj = wx.createVideoContext('myVideo')
},
methods:{
//图片预览
lookImg(e){
let array = [];
array.push(e);
uni.previewImage({
urls: array,
current: array
});
},
//检验路径类型()图片/视频
testingUrlIsImgOrVideo(url){
console.log('拿到路径',url);
let array = [];
// // 匹配 视频
const imgList = [
'png', 'jpg', 'jpeg', 'bmp', 'gif', 'PNG','JPG','JPEG','BMP','GIF','webp','WEBP','psd','PSD','svg','SVG','tiff','TIFF'
]
// 后缀获取
let suffix = ''
// 获取类型结果
let result = ''
try {
const flieArr = url.split('.') //根据.分割数组
suffix = flieArr[flieArr.length - 1] //取最后一个
console.log('取末尾', suffix)
} catch (err) {
suffix = ''
}
// 进行图片匹配
result = imgList.some(item => {
return item === suffix
})
if (result) {
result = 'image'
}else{
result = 'video'
}
console.log('检测',result);
return result
}
}
}
</script>
<style>
// 自定义轮播加视频
.swiper-view {
width: 100%;
height: 530rpx;
.swiper-img {
image {
width: 100%;
height: 530rpx;
}
}
}
// 指示点元素激活(当前选中)状态样式 */
::v-deep wx-swiper .wx-swiper-dot-active{
width: 30rpx;
height: 14rpx;
border-radius: 6rpx;
background-color: #92C300;
}
</style>