ImageGrid.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="image-grid-container">
  3. <view class="image-grid" :class="`count-${Math.min(images.length, 9)}`">
  4. <view
  5. v-for="(image, index) in showImages"
  6. :key="index"
  7. class="image-item"
  8. :style="getItemStyle(index)"
  9. >
  10. <image
  11. mode="heightFix"
  12. :src="image"
  13. class="image-content"
  14. lazy-load
  15. @click.stop="previewImage(index)"
  16. />
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script setup>
  22. import { defineProps, computed } from "vue";
  23. const props = defineProps({
  24. images: {
  25. type: Array,
  26. required: true,
  27. default: () => [],
  28. },
  29. spacing: {
  30. type: Number,
  31. default: 8,
  32. },
  33. maxShow: {
  34. type: Number,
  35. default: 9, // 最多显示几张
  36. },
  37. isSeeLarge: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. });
  42. const showImages = computed(() => {
  43. return props.images.slice(0, props.maxShow);
  44. });
  45. const previewImage = (index) => {
  46. if (props.isSeeLarge) {
  47. uni.previewImage({
  48. current: index,
  49. urls: props.images,
  50. indicator: "number",
  51. loop: true,
  52. });
  53. }
  54. };
  55. const getItemStyle = (index) => {
  56. const count = Math.min(props.images.length, 3);
  57. let style = {};
  58. if (count === 1) {
  59. style.width = "100%";
  60. style.paddingTop = "56.25%"; // 16:9比例
  61. } else {
  62. style.width = `calc(${100 / count}% - ${
  63. (props.spacing * (count - 1)) / count
  64. }px)`;
  65. style.paddingTop = style.width;
  66. }
  67. if (index % count !== count - 1) {
  68. style.marginRight = `${props.spacing}px`;
  69. }
  70. return style;
  71. };
  72. </script>
  73. <style lang="less" scoped>
  74. .image-grid-container {
  75. width: 100%;
  76. margin-bottom: 12px;
  77. .image-grid {
  78. display: flex;
  79. flex-wrap: wrap;
  80. width: 100%;
  81. .image-item {
  82. position: relative;
  83. overflow: hidden;
  84. border-radius: 16rpx;
  85. margin-bottom: v-bind('props.spacing + "px"');
  86. .image-content {
  87. position: absolute;
  88. top: 0;
  89. left: 0;
  90. width: 100%;
  91. height: 100%;
  92. object-fit: cover;
  93. transition: transform 0.2s ease;
  94. &:active {
  95. transform: scale(0.98);
  96. }
  97. }
  98. .image-more {
  99. position: absolute;
  100. top: 0;
  101. left: 0;
  102. width: 100%;
  103. height: 100%;
  104. display: flex;
  105. align-items: center;
  106. justify-content: center;
  107. background-color: rgba(0, 0, 0, 0.5);
  108. color: white;
  109. font-size: 24px;
  110. font-weight: bold;
  111. }
  112. }
  113. // 特殊处理只有一张图片的情况
  114. &.count-1 .image-item {
  115. border-radius: 12px;
  116. }
  117. }
  118. }
  119. ::v-deep .uni-preview-image {
  120. z-index: 999999 !important;
  121. }
  122. </style>