uni-image-preview.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div class="uni-image-preview-mask" :class="{ 'uni-image-preview-show': show }">
  3. <div class="uni-image-preview-content">
  4. <div class="uni-image-preview-header">
  5. <span>{{ current + 1 }}/{{ urls.length }}</span>
  6. <i class="uni-icon-close" @click="close"></i>
  7. </div>
  8. <swiper class="uni-image-preview-swiper" :current="current" @change="onChange">
  9. <swiper-item v-for="(item, index) in urls" :key="index" class="uni-image-preview-swiper-item">
  10. <img :src="item" mode="aspectFit" class="uni-image-preview-img" @longpress="$emit('longpress')" @click="$emit('clickImage', urls[current])"/>
  11. </swiper-item>
  12. </swiper>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'uniImagePreview',
  19. props: {
  20. urls: {
  21. type: Array,
  22. default() {
  23. return [];
  24. },
  25. },
  26. current: {
  27. type: Number,
  28. default: 0,
  29. },
  30. show: {
  31. type: Boolean,
  32. default: false,
  33. },
  34. },
  35. methods: {
  36. close() {
  37. this.$emit('update:show', false);
  38. this.$emit('close');
  39. },
  40. onChange(event) {
  41. this.$emit('update:current', event.detail.current);
  42. },
  43. },
  44. };
  45. </script>
  46. <style>
  47. .uni-image-preview-mask {
  48. position: fixed;
  49. top: 0;
  50. left: 0;
  51. right: 0;
  52. bottom: 0;
  53. background-color: rgba(0, 0, 0, 0.7);
  54. z-index: 9999;
  55. opacity: 0;
  56. visibility: hidden;
  57. transition: all 0.3s ease-in-out;
  58. }
  59. .uni-image-preview-show {
  60. opacity: 1;
  61. visibility: visible;
  62. }
  63. .uni-image-preview-content {
  64. position: absolute;
  65. top: 50%;
  66. left: 50%;
  67. transform: translate(-50%, -50%);
  68. background-color: #fff;
  69. border-radius: 2px;
  70. overflow: hidden;
  71. box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
  72. }
  73. .uni-image-preview-header {
  74. display: flex;
  75. align-items: center;
  76. justify-content: space-between;
  77. padding: 10px;
  78. color: #fff;
  79. background-color: rgba(0, 0, 0, 0.7);
  80. font-size: 14px;
  81. }
  82. .uni-icon-close {
  83. font-size: 20px;
  84. }
  85. .uni-image-preview-swiper {
  86. width: 100vw;
  87. height: 100vh;
  88. }
  89. .uni-image-preview-img {
  90. max-width: 100%;
  91. max-height: 100%;
  92. }
  93. </style>