s-image-cube.vue 2.9 KB

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