uni-grid-item.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view
  3. :style="'width:' + width + ';' + (square ? 'height:' + width : '')"
  4. class="uni-grid-item grid-item"
  5. >
  6. <view
  7. :class="{
  8. 'uni-grid-item--border': showBorder,
  9. 'uni-grid-item--border-top': showBorder && index < column,
  10. 'uni-highlight': highlight
  11. }"
  12. :style="{
  13. 'border-right-color': borderColor,
  14. 'border-bottom-color': borderColor,
  15. 'border-top-color': borderColor
  16. }"
  17. class="uni-grid-item__box"
  18. @click="_onClick"
  19. >
  20. <slot />
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. /**
  26. * GridItem 宫格
  27. * @description 宫格组件
  28. * @tutorial https://ext.dcloud.net.cn/plugin?id=27
  29. * @property {Number} index 子组件的唯一标识 ,点击gird会返回当前的标识
  30. */
  31. export default {
  32. name: 'UniGridItem',
  33. inject: ['grid'],
  34. props: {
  35. index: {
  36. type: Number,
  37. default: 0
  38. }
  39. },
  40. data() {
  41. return {
  42. column: 0,
  43. showBorder: true,
  44. square: true,
  45. highlight: true,
  46. left: 0,
  47. top: 0,
  48. openNum: 2,
  49. width: 0,
  50. borderColor: '#e5e5e5'
  51. };
  52. },
  53. created() {
  54. this.column = this.grid.column;
  55. this.showBorder = this.grid.showBorder;
  56. this.square = this.grid.square;
  57. this.highlight = this.grid.highlight;
  58. this.top = this.hor === 0 ? this.grid.hor : this.hor;
  59. this.left = this.ver === 0 ? this.grid.ver : this.ver;
  60. this.borderColor = this.grid.borderColor;
  61. this.grid.children.push(this);
  62. // this.grid.init()
  63. this.width = this.grid.width;
  64. },
  65. beforeDestroy() {
  66. this.grid.children.forEach((item, index) => {
  67. if (item === this) {
  68. this.grid.children.splice(index, 1);
  69. }
  70. });
  71. },
  72. methods: {
  73. _onClick() {
  74. this.grid.change({
  75. detail: {
  76. index: this.index
  77. }
  78. });
  79. }
  80. }
  81. };
  82. </script>
  83. <style lang="scss" scoped>
  84. .uni-grid-item {
  85. /* #ifndef APP-NVUE */
  86. height: 100%;
  87. display: flex;
  88. /* #endif */
  89. /* #ifdef H5 */
  90. cursor: pointer;
  91. /* #endif */
  92. }
  93. .uni-grid-item__box {
  94. /* #ifndef APP-NVUE */
  95. display: flex;
  96. width: 100%;
  97. /* #endif */
  98. position: relative;
  99. flex: 1;
  100. flex-direction: column;
  101. // justify-content: center;
  102. // align-items: center;
  103. }
  104. .uni-grid-item--border {
  105. position: relative;
  106. /* #ifdef APP-NVUE */
  107. border-bottom-color: #d2d2d2;
  108. border-bottom-style: solid;
  109. border-bottom-width: 0.5px;
  110. border-right-color: #d2d2d2;
  111. border-right-style: solid;
  112. border-right-width: 0.5px;
  113. /* #endif */
  114. /* #ifndef APP-NVUE */
  115. z-index: 0;
  116. border-bottom: 1px #d2d2d2 solid;
  117. border-right: 1px #d2d2d2 solid;
  118. /* #endif */
  119. }
  120. .uni-grid-item--border-top {
  121. position: relative;
  122. /* #ifdef APP-NVUE */
  123. border-top-color: #d2d2d2;
  124. border-top-style: solid;
  125. border-top-width: 0.5px;
  126. /* #endif */
  127. /* #ifndef APP-NVUE */
  128. border-top: 1px #d2d2d2 solid;
  129. z-index: 0;
  130. /* #endif */
  131. }
  132. .uni-highlight:active {
  133. background-color: #f1f1f1;
  134. }
  135. </style>