PreviewPhoto.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="container">
  3. <!-- 图片列表 -->
  4. <view class="image-list">
  5. <view class="image-item" :style="{height:height || '180rpx'}" v-for="(item, index) in imageList"
  6. :key="index">
  7. <text class="imagedelete" @click="$emit('imagedelete',index)" v-if="type != 'view'">
  8. <uni-icons type="closeempty" size="18" color="#ccc"></uni-icons>
  9. </text>
  10. <image :src="item" mode="aspectFill" lazy-load @click="previewImage(index)"></image>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. height: '180rpx'
  20. };
  21. },
  22. watch: {
  23. },
  24. props: {
  25. type: {
  26. type: String,
  27. default: 'edit'
  28. },
  29. imageList: {
  30. type: Array,
  31. default: () => []
  32. }
  33. },
  34. mounted() {
  35. this.getElementWidth();
  36. },
  37. methods: {
  38. // 预览图片
  39. previewImage(index) {
  40. uni.previewImage({
  41. current: index,
  42. urls: this.imageList.map(item => item)
  43. });
  44. },
  45. getElementWidth() {
  46. // 创建查询实例
  47. const query = uni.createSelectorQuery().in(this);
  48. // 选择要查询的元素
  49. query.select('.container')
  50. .boundingClientRect(data => {
  51. console.log(data.width,'data.width')
  52. let height = Math.floor(((data.width - 20) * 31) / 50);
  53. this.height = height > 160 ? height + 'rpx' : '180rpx'
  54. })
  55. .exec();
  56. }
  57. }
  58. };
  59. </script>
  60. <style lang="scss" scoped>
  61. .container {
  62. padding: 10px;
  63. }
  64. .imagedelete {
  65. position: absolute;
  66. z-index: 20;
  67. display: block;
  68. right: 4rpx;
  69. top: -2rpx;
  70. text-align: center;
  71. background: rgba(0, 0, 0, 0.5);
  72. color: #fff;
  73. border-radius: 50%;
  74. font-size: 30rpx;
  75. width: 44rpx;
  76. height: 44rpx;
  77. line-height: 27rpx;
  78. text-align: center;
  79. }
  80. .image-list {
  81. display: flex;
  82. flex-wrap: wrap;
  83. .image-item {
  84. position: relative;
  85. width: 31%;
  86. margin-bottom: 10px;
  87. margin-right: 3%;
  88. image {
  89. width: 100%;
  90. height: 100%;
  91. border-radius: 5px;
  92. }
  93. text {
  94. display: block;
  95. margin-top: 5px;
  96. font-size: 14px;
  97. text-align: center;
  98. }
  99. }
  100. .image-item:nth-child(3n) {
  101. margin-right: 0;
  102. }
  103. }
  104. </style>