s-image-cube.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!-- 装修图文组件:广告魔方 -->
  2. <template>
  3. <view class="ss-cube-wrap" :style="[parseAdWrap]">
  4. <view v-for="(item, index) in data.list" :key="index">
  5. <view
  6. class="cube-img-wrap"
  7. :style="[parseImgStyle(item), { margin: data.space + 'px' }]"
  8. @tap="sheep.$router.go(item.url)"
  9. >
  10. <image class="cube-img" :src="sheep.$url.cdn(item.imgUrl)" mode="aspectFill"></image>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. /**
  17. /**
  18. * 广告魔方
  19. *
  20. * @property {Array<Object>} list - 魔方列表
  21. * @property {Object} styles - 组件样式
  22. * @property {String} background - 组件背景色
  23. * @property {Number} topSpace - 组件顶部间距
  24. * @property {Number} bottomSpace - 组件底部间距
  25. * @property {Number} leftSpace - 容器左间距
  26. * @property {Number} rightSpace - 容器右间距
  27. * @property {Number} imgSpace - 图片间距
  28. * @property {Number} imgTopRadius - 图片上圆角
  29. * @property {Number} imgBottomRadius - 图片下圆角
  30. *
  31. */
  32. import { computed, inject, unref } from 'vue';
  33. import sheep from '@/sheep';
  34. // 参数
  35. const props = defineProps({
  36. data: {
  37. type: Object,
  38. default() {},
  39. },
  40. styles: {
  41. type: Object,
  42. default() {},
  43. },
  44. });
  45. // 单元格大小
  46. const windowWidth = sheep.$platform.device.windowWidth;
  47. const cell = computed(() => {
  48. return (
  49. (windowWidth -
  50. ((props.styles.marginLeft || 0) + (props.styles.marginRight || 0) + (props.styles.padding || 0) * 2)) /
  51. 4
  52. );
  53. });
  54. //包裹容器高度
  55. const parseAdWrap = computed(() => {
  56. let heightArr = props.data.list.reduce(
  57. (prev, cur) => (prev.includes(cur.height + cur.top) ? prev : [...prev, cur.height + cur.top]),
  58. [],
  59. );
  60. let heightMax = Math.max(...heightArr);
  61. return {
  62. height: heightMax * cell.value + 'px',
  63. width:
  64. windowWidth -
  65. (props.data?.style?.marginLeft +
  66. props.data?.style?.marginRight +
  67. props.styles.padding * 2) *
  68. 2 +
  69. 'px',
  70. };
  71. });
  72. // 解析图片大小位置
  73. const parseImgStyle = (item) => {
  74. let obj = {
  75. width: item.width * cell.value - props.data.space + 'px',
  76. height: item.height * cell.value - props.data.space + 'px',
  77. left: item.left * cell.value + 'px',
  78. top: item.top * cell.value + 'px',
  79. 'border-top-left-radius': props.data.borderRadiusTop + 'px',
  80. 'border-top-right-radius': props.data.borderRadiusTop + 'px',
  81. 'border-bottom-left-radius': props.data.borderRadiusBottom + 'px',
  82. 'border-bottom-right-radius': props.data.borderRadiusBottom + 'px',
  83. };
  84. return obj;
  85. };
  86. </script>
  87. <style lang="scss" scoped>
  88. .ss-cube-wrap {
  89. position: relative;
  90. z-index: 2;
  91. width: 750rpx;
  92. }
  93. .cube-img-wrap {
  94. position: absolute;
  95. z-index: 3;
  96. overflow: hidden;
  97. }
  98. .cube-img {
  99. width: 100%;
  100. height: 100%;
  101. }
  102. </style>