uni-grid.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view>
  3. <view :id="elId" :class="{ border: showBorder }" :style="{ 'border-left': showBorder ? '1px ' + borderColor + ' solid' : 'none' }" class="uni-grid"><slot /></view>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'UniGrid',
  9. props: {
  10. // 每列显示个数
  11. column: {
  12. type: Number,
  13. default: 3
  14. },
  15. // 是否显示边框
  16. showBorder: {
  17. type: Boolean,
  18. default: true
  19. },
  20. // 是否显示边框
  21. borderColor: {
  22. type: String,
  23. default: '#e5e5e5'
  24. },
  25. // 全局标记水平方向移动距离 ,起点为中心,负数为左移动,正数为右移动
  26. hor: {
  27. type: Number,
  28. default: 0
  29. },
  30. // 全局标记垂直方向移动距离 ,起点为中心,负数为上移动,正数为下移动
  31. ver: {
  32. type: Number,
  33. default: 0
  34. },
  35. // 是否正方形显示,默认为 true
  36. square: {
  37. type: Boolean,
  38. default: true
  39. },
  40. highlight: {
  41. type: Boolean,
  42. default: true
  43. }
  44. },
  45. provide() {
  46. return {
  47. grid: this
  48. };
  49. },
  50. data() {
  51. const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`;
  52. return {
  53. index: 0,
  54. elId
  55. };
  56. },
  57. created() {
  58. this.index = 0;
  59. this.childrens = [];
  60. this.pIndex = this.pIndex ? this.pIndex++ : 0;
  61. },
  62. methods: {
  63. change(e) {
  64. this.$emit('change', e);
  65. },
  66. _getSize(fn) {
  67. uni.createSelectorQuery()
  68. .in(this)
  69. .select(`#${this.elId}`)
  70. .boundingClientRect()
  71. .exec(ret => {
  72. if (!ret[0]) {
  73. setTimeout(this._getSize(fn));
  74. return;
  75. }
  76. let width = parseInt(ret[0].width / this.column) - 1 + 'px';
  77. typeof fn === 'function' && fn(width);
  78. });
  79. }
  80. }
  81. };
  82. </script>
  83. <style>
  84. @charset "UTF-8";
  85. .uni-grid {
  86. display: flex;
  87. flex-wrap: wrap;
  88. box-sizing: border-box;
  89. border-left: 1px #e5e5e5 solid;
  90. }
  91. </style>