index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!-- html -->
  2. <template>
  3. <view class="container">
  4. <view class="left">
  5. <image v-for="(item,index) in leftList" :key="index" @tap="previewImage(item.id)" :src="item.url" alt=""
  6. mode="widthFix"></image>
  7. </view>
  8. <view class="right">
  9. <image v-for="(item,index) in rightList" :key="index" @tap="previewImage(item.id)" :src="item.url" alt=""
  10. mode="widthFix"></image>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. components: {},
  17. data: function() {
  18. return {
  19. setMealList: [],
  20. // 初始化左右盒子
  21. leftList: [],
  22. rightList: [],
  23. // 初始化左右盒子高度
  24. leftH: 0,
  25. rightH: 0,
  26. baseImagePath:this.baseImagePath
  27. }
  28. },
  29. onLoad(options) {
  30. let data = options.photo.split(',')
  31. data.forEach(item=>{
  32. let img = {
  33. url : this.baseImagePath+item
  34. }
  35. this.setMealList.push(img)
  36. })
  37. console.log(data);
  38. this.doList()
  39. },
  40. methods: {
  41. async doList() {
  42. const that = this
  43. for (let index in this.setMealList) {
  44. // 获取图片宽高
  45. await this.getImageInfo(index).then((res) => {
  46. // 计算图片渲染高度
  47. let showH = (50 * res.height) / res.width
  48. // 判断左右盒子高度
  49. if (that.leftH <= that.rightH) {
  50. that.leftList.push(this.setMealList[index])
  51. that.leftH += showH
  52. } else {
  53. that.rightList.push(this.setMealList[index])
  54. that.rightH += showH
  55. }
  56. })
  57. }
  58. },
  59. async getImageInfo(index) {
  60. return new Promise((resolve, reject) => {
  61. uni.getImageInfo({
  62. src: this.setMealList[index].url,
  63. success: (image) => {
  64. resolve(image)
  65. },
  66. })
  67. })
  68. },
  69. previewImage(index) {
  70. //uniapp预览轮播图
  71. uni.previewImage({
  72. current: index, //预览图片的下标
  73. urls: this.setMealList.map(item => item.url) //预览图片的地址,必须要数组形式,如果不是数组形式就转换成数组形式就可以
  74. })
  75. }
  76. }
  77. }
  78. </script>
  79. <style>
  80. page{
  81. background-color: #F5F5F5;
  82. }
  83. .container {
  84. padding: 0 30rpx;
  85. font-size: 14rpx;
  86. line-height: 24rpx;
  87. padding-top: 21rpx;
  88. }
  89. .right,
  90. .left {
  91. display: inline-block;
  92. width: 49%;
  93. vertical-align: top;
  94. border-radius: 10rpx;
  95. }
  96. .left {
  97. margin-right: 2%;
  98. }
  99. .left image,
  100. .right image {
  101. border-radius: 10rpx;
  102. width: 100%;
  103. margin-bottom: 10rpx;
  104. }
  105. </style>