Mr.Shi
2023-08-21 3d1a4b4bf3d37e29a3d3345e00f9f84c0d7959a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
    <div class="preview">
        <div class="preview_close" @click="closes">
            <van-icon name="cross" size="20" />
        </div>
        <div class="preview_content">
            <Swiper @slideChange="onSlideChange" :initialSlide="props.index">
                <SwiperSlide v-for="(item, index) in props.list" :key="index">
                    <div class="preview_content_box">
                        <img v-if="item.type === 0" :src="item.url" alt="" />
                        <video ref="video" v-else-if="item.type === 1" :src="item.url" controls="controls"></video>
                    </div>
                </SwiperSlide>
            </Swiper>
        </div>
    </div>
</template>
 
<script setup lang="ts">
    import { ref, defineProps, defineEmits } from 'vue'
    import { Swiper, SwiperSlide } from 'swiper/vue'
    import 'swiper/css'
    import 'swiper/css/effect-cube'
    import 'swiper/css/pagination'
 
    const props = defineProps({
        list: {
            type: Array,
            require: true
        },
        index: {
            type: Number,
            default: 0
        }
    })
 
    let video: any = ref(null)
 
    const emit = defineEmits([ 'close' ])
 
    // 关闭组件
    const closes = () => {
        emit('close')
    }
 
    // 切换暂停播放
    const onSlideChange = () => {
        if (!video.value) return
        video.value.forEach((item: any) => {
            item.pause()
        })
    }
</script>
 
<style lang="scss" scoped>
.preview {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 9999;
    .preview_close {
        position: fixed;
        right: 50px;
        top: 50px;
        width: 70px;
        height: 70px;
        border-radius: 50%;
        background: #B2B2B2;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 99999;
    }
    .preview_content {
        height: 100%;
        position: relative;
        top: 50%;
        left: 0;
        transform: translate(0, -50%);
        .swiper {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            .swiper-slide {
                width: 100%;
                height: 100%;
                position: relative;
                .preview_content_box {
                    width: 100%;
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    img {
                        width: 100%;
                    }
                    video {
                        width: 100%;
                    }
                }
            }
        }
    }
}
</style>