tn-grid-item.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view
  3. class="tn-grid-item-class tn-grid-item"
  4. :class="[
  5. backgroundColorClass
  6. ]"
  7. :hover-class="hoverClass"
  8. :hover-stay-time="150"
  9. :style="{
  10. backgroundColor: backgroundColorStyle,
  11. width: gridWidth
  12. }"
  13. @tap="click"
  14. >
  15. <view
  16. class="tn-grid-item__box"
  17. >
  18. <slot></slot>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import componentsColorMixin from '../../libs/mixin/components_color.js'
  24. export default {
  25. mixins: [ componentsColorMixin ],
  26. name: 'tn-grid-item',
  27. props: {
  28. // 序号
  29. index: {
  30. type: [Number, String],
  31. default: ''
  32. }
  33. },
  34. data() {
  35. return {
  36. // 父组件数据
  37. parentData: {
  38. // 按下去的样式
  39. hoverClass: '',
  40. col: 3
  41. }
  42. }
  43. },
  44. created() {
  45. // 父组件实例
  46. this.updateParentData()
  47. this.parent.children.push(this)
  48. },
  49. computed: {
  50. // 计算每个宫格的宽度
  51. gridWidth() {
  52. // #ifdef MP-WEIXIN
  53. return '100%'
  54. // #endif
  55. // #ifndef MP-WEIXIN
  56. return 100 / Number(this.parentData.col) + '%'
  57. // #endif
  58. },
  59. // 点击效果
  60. hoverClass() {
  61. return this.parentData.hoverClass !== 'none'
  62. ? this.parentData.hoverClass + ' tn-grid-item--hover'
  63. : this.parentData.hoverClass
  64. }
  65. },
  66. methods: {
  67. // 获取父组件参数
  68. updateParentData() {
  69. this.getParentData('tn-grid')
  70. },
  71. click() {
  72. this.$emit('click', this.index)
  73. this.parent && this.parent.click(this.index)
  74. }
  75. }
  76. }
  77. </script>
  78. <style lang="scss" scoped>
  79. .tn-grid-item {
  80. box-sizing: border-box;
  81. background-color: #FFFFFF;
  82. /* #ifndef APP-NVUE */
  83. display: flex;
  84. flex-direction: row;
  85. /* #endif */
  86. align-items: center;
  87. justify-content: center;
  88. position: relative;
  89. flex-direction: column;
  90. /* #ifdef MP */
  91. // float: left;
  92. /* #endif */
  93. &__box {
  94. /* #ifndef APP-NVUE */
  95. display: flex;
  96. flex-direction: row;
  97. /* #endif */
  98. align-items: center;
  99. justify-content: center;
  100. flex-direction: column;
  101. flex: 1;
  102. width: 100%;
  103. height: 100%;
  104. }
  105. &--hover {
  106. background: $tn-space-color !important;
  107. }
  108. }
  109. </style>