uni-grid-item.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view v-if="width" :style="{ width: width }" class="uni-grid-item">
  3. <view
  4. :class="{ border: showBorder, 'uni-grid-item__box-square': square, 'border-top': showBorder && index < column, 'uni-highlight': highlight }"
  5. :style="{ 'border-color': borderColor }"
  6. class="uni-grid-item__box"
  7. @click="_onClick"
  8. >
  9. <view v-if="marker === 'dot'" :style="{ left: top + 'px', top: left + 'px' }" class="uni-grid-item__box-dot" />
  10. <view v-if="marker === 'badge'" :style="{ left: top + 'px', top: left + 'px' }" class="uni-grid-item__box-badge">
  11. <uni-badge :text="text" :type="type" :size="size" :inverted="inverted" />
  12. </view>
  13. <view v-if="marker === 'image'" :style="{ left: top + 'px', top: left + 'px' }" class="uni-grid-item__box-image">
  14. <image :style="{ width: imgWidth + 'px' }" :src="src" class="box-image" mode="widthFix" />
  15. </view>
  16. <view class="uni-grid-item__box-item"><slot /></view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import uniBadge from '../uni-badge/uni-badge.vue';
  22. export default {
  23. name: 'UniGridItem',
  24. components: {
  25. uniBadge
  26. },
  27. props: {
  28. // 类型:可选值,dot:圆点;badge:角标;image:图片
  29. marker: {
  30. type: String,
  31. default: ''
  32. },
  33. // 水平方向
  34. hor: {
  35. type: Number,
  36. default: 0
  37. },
  38. // 垂直方向
  39. ver: {
  40. type: Number,
  41. default: 0
  42. },
  43. // badge 下颜色类型,可选值:default(灰色)、primary(蓝色)、success(绿色)、warning(黄色)、error(红色)
  44. type: {
  45. type: String,
  46. default: ''
  47. },
  48. // badge 下显示内容,汉字最多为1个
  49. text: {
  50. type: String,
  51. default: ''
  52. },
  53. // badge 下 Badge 大小
  54. size: {
  55. type: String,
  56. default: 'normal'
  57. },
  58. // badge 下 是否无需背景颜色
  59. inverted: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // image 下图片路径
  64. src: {
  65. type: String,
  66. default: ''
  67. },
  68. // image 下图片宽度 ,最大 为 100 。 默认为 30
  69. imgWidth: {
  70. type: Number,
  71. default: 30
  72. }
  73. },
  74. inject: ['grid'],
  75. data() {
  76. return {
  77. column: 0,
  78. showBorder: true,
  79. square: true,
  80. highlight: true,
  81. left: 0,
  82. top: 0,
  83. index: 0,
  84. openNum: 2,
  85. width: 0,
  86. borderColor: '#e5e5e5'
  87. };
  88. },
  89. created() {
  90. this.column = this.grid.column;
  91. this.showBorder = this.grid.showBorder;
  92. this.square = this.grid.square;
  93. this.highlight = this.grid.highlight;
  94. this.top = this.hor === 0 ? this.grid.hor : this.hor;
  95. this.left = this.ver === 0 ? this.grid.ver : this.ver;
  96. this.borderColor = this.grid.borderColor;
  97. this.index = this.grid.index++;
  98. },
  99. // #ifdef H5
  100. mounted() {
  101. this.grid._getSize(width => {
  102. this.width = width;
  103. });
  104. },
  105. // #endif
  106. // #ifndef H5
  107. onReady() {
  108. this.grid._getSize(width => {
  109. this.width = width;
  110. });
  111. },
  112. // #endif
  113. methods: {
  114. _onClick() {
  115. this.grid.change({
  116. detail: {
  117. index: this.index
  118. }
  119. });
  120. }
  121. }
  122. };
  123. </script>
  124. <style>
  125. @charset "UTF-8";
  126. .uni-grid-item {
  127. box-sizing: border-box;
  128. }
  129. .uni-grid-item__box {
  130. position: relative;
  131. width: 100%;
  132. }
  133. .uni-grid-item__box-item {
  134. display: flex;
  135. justify-content: center;
  136. flex-direction: column;
  137. align-items: center;
  138. width: 100%;
  139. height: 100%;
  140. font-size: 32rpx;
  141. color: #666;
  142. padding: 20rpx 0;
  143. box-sizing: border-box;
  144. }
  145. .uni-grid-item__box-item .image {
  146. width: 50rpx;
  147. height: 50rpx;
  148. }
  149. .uni-grid-item__box-item .text {
  150. font-size: 26rpx;
  151. margin-top: 10rpx;
  152. }
  153. .uni-grid-item__box.uni-grid-item__box-square {
  154. height: 0;
  155. padding-top: 100%;
  156. }
  157. .uni-grid-item__box.uni-grid-item__box-square .uni-grid-item__box-item {
  158. position: absolute;
  159. top: 0;
  160. }
  161. .uni-grid-item__box.border {
  162. position: relative;
  163. box-sizing: border-box;
  164. border-bottom: 1px #e5e5e5 solid;
  165. border-right: 1px #e5e5e5 solid;
  166. }
  167. .uni-grid-item__box.border-top {
  168. border-top: 1px #e5e5e5 solid;
  169. }
  170. .uni-grid-item__box.uni-highlight:active {
  171. background-color: #eee;
  172. }
  173. .uni-grid-item__box-badge,
  174. .uni-grid-item__box-dot,
  175. .uni-grid-item__box-image {
  176. position: absolute;
  177. top: 0;
  178. right: 0;
  179. left: 0;
  180. bottom: 0;
  181. margin: auto;
  182. z-index: 10;
  183. }
  184. .uni-grid-item__box-dot {
  185. width: 20rpx;
  186. height: 20rpx;
  187. background: #ff5a5f;
  188. border-radius: 50%;
  189. }
  190. .uni-grid-item__box-badge {
  191. display: flex;
  192. justify-content: center;
  193. align-items: center;
  194. width: 0;
  195. height: 0;
  196. }
  197. .uni-grid-item__box-image {
  198. display: flex;
  199. justify-content: center;
  200. align-items: center;
  201. width: 100rpx;
  202. height: 100rpx;
  203. overflow: hidden;
  204. }
  205. .uni-grid-item__box-image .box-image {
  206. width: 90rpx;
  207. }
  208. </style>