magicCube.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!-- 装修图文组件:广告魔方 -->
  2. <template>
  3. <div class="ss-cube-wrap" :style="parseAdWrap">
  4. <div v-for="(item, index) in data.list" :key="index">
  5. <div class="cube-img-wrap" :style="[parseImgStyle(item), { margin: data.space + 'px' }]"
  6. @click="navigateTo(item.url)">
  7. <image class="cube-img" :src="sheep.$url.cdn(item.imgUrl)" />
  8. </div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import sheep from '@/sheep';
  14. export default {
  15. props: {
  16. data: {
  17. type: Object,
  18. default: () => ({}),
  19. },
  20. styles: {
  21. type: Object,
  22. default: () => ({}),
  23. },
  24. },
  25. watch: {
  26. data(newVal) {
  27. if (newVal) {
  28. console.log(newVal, '更新后的data');
  29. this.updateComponent(newVal, this.styles);
  30. }
  31. },
  32. styles(newVal) {
  33. if (newVal) {
  34. console.log(newVal, '更新后的styles');
  35. this.updateComponent(this.data, newVal);
  36. }
  37. }
  38. },
  39. mounted() {
  40. this.updateComponent(this.data, this.styles);
  41. },
  42. methods: {
  43. updateComponent(data, styles) {
  44. console.log(data, styles, '88888888');
  45. // 在这里处理更新后的 data 和 styles
  46. },
  47. parseAdWrap() {
  48. // 更新 cell 的值
  49. this.cell = (this.windowWidth -
  50. ((this.styles.marginLeft || 0) + (this.styles.marginRight || 0) + (this.styles.padding || 0) * 2)
  51. ) /
  52. 4;
  53. let heightArr = this.data.list.reduce(
  54. (prev, cur) => (prev.includes(cur.height + cur.top) ? prev : [...prev, cur.height + cur.top]),
  55. []
  56. );
  57. let heightMax = Math.max(...heightArr);
  58. return {
  59. height: heightMax * this.cell + 'px',
  60. width: this.windowWidth -
  61. (this.data?.style?.marginLeft +
  62. this.data?.style?.marginRight +
  63. this.styles.padding * 2) *
  64. 2 +
  65. 'px',
  66. };
  67. },
  68. parseImgStyle(item) {
  69. return {
  70. width: item.width * this.cell - this.data.space + 'px',
  71. height: item.height * this.cell - this.data.space + 'px',
  72. left: item.left * this.cell + 'px',
  73. top: item.top * this.cell + 'px',
  74. 'border-top-left-radius': this.data.borderRadiusTop + 'px',
  75. 'border-top-right-radius': this.data.borderRadiusTop + 'px',
  76. 'border-bottom-left-radius': this.data.borderRadiusBottom + 'px',
  77. 'border-bottom-right-radius': this.data.borderRadiusBottom + 'px',
  78. };
  79. },
  80. navigateTo(url) {
  81. this.sheep.$router.go(url);
  82. },
  83. },
  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>