jinlin
2024-01-15 1a7af6fff5185bb257c16b0445140c93263a3331
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
<template>
  <el-dialog
    :visible.sync="isShowPreview"
    append-to-body
    class="gallery-dialog"
    style="text-align: center"
  >
    <div slot="title">{{sourceTitle || title}}</div>
    <!-- 视频 -->
    <template v-if="type === 'video'">
      <video
        :src="sources[initialIndex].src"
        autoplay="autoplay"
        class="ele-gallery-video"
        controls="controls"
        v-if="isShowPreview"
        width="100%"
      ></video>
    </template>
    <!-- iframe -->
    <template v-else-if="type === 'iframe'">
      <div
        class="ele-gallery-iframe embed-responsive embed-responsive-16by9"
        v-if="isShowPreview"
      >
        <iframe
          :src="sources[initialIndex].src"
          allowfullscreen="true"
          border="0"
          frameborder="no"
          framespacing="0"
          scrolling="no"
        ></iframe>
      </div>
    </template>
 
    <!-- 图片 -->
    <template v-else-if="type === 'image'">
      <!-- 单张图片 -->
      <img
        :src="sources[initialIndex].src"
        class="ele-gallery-image"
        v-if="(sliceSingle && sources[initialIndex]) || sources.length === 1"
      >
 
      <!-- 多张图片 -->
      <el-carousel
        :initial-index="initialIndex"
        @change="handleCarouselChange"
        indicator-position="outside"
        v-bind="carouselAttrs"
        v-else
      >
        <el-carousel-item
          :key="index"
          v-for="(image, index) in sources"
        >
          <img
            :src="image.src"
            class="ele-gallery-image"
          >
        </el-carousel-item>
      </el-carousel>
    </template>
  </el-dialog>
</template>
 
<script>
  export default {
    name: 'ele-gallery-dialog',
    props: {
      type: String,
      title: String,
      sources: Array,
      sliceSingle: {
        type: Boolean,
        default: false
      },
      carouselAttrs: Object
    },
    data() {
      return {
        sourceTitle: '',
        initialIndex: 0,
        isShowPreview: false
      }
    },
    methods: {
      startPreview(index) {
        this.isShowPreview = true
        this.initialIndex = index
        this.sourceTitle = this.sources[index].title
      },
      handleCarouselChange(index) {
        if (this.sources[index] && this.sources[index].title) {
          this.sourceTitle = this.sources[index].title
        } else {
          this.sourceTitle = ''
        }
      }
    }
  }
</script>
<style>
  .gallery-dialog .el-dialog__body {
    padding: 0px;
  }
</style>