u-col.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="u-col" :class="[
  3. 'u-col-' + span
  4. ]" :style="{
  5. padding: `0 ${Number(gutter)/2 + 'rpx'}`,
  6. marginLeft: 100 / 12 * offset + '%',
  7. flex: `0 0 ${100 / 12 * span}%`,
  8. alignItems: uAlignItem,
  9. justifyContent: uJustify,
  10. textAlign: textAlign
  11. }" @tap.stop.prevent="click">
  12. <slot></slot>
  13. </view>
  14. </template>
  15. <script>
  16. /**
  17. * col 布局单元格
  18. * @description 通过基础的 12 分栏,迅速简便地创建布局(搭配<u-row>使用)
  19. * @tutorial https://www.uviewui.com/components/layout.html
  20. * @property {String Number} span 栅格占据的列数,总12等分(默认0)
  21. * @property {String} text-align 文字水平对齐方式(默认left)
  22. * @property {String Number} offset 分栏左边偏移,计算方式与span相同(默认0)
  23. * @example <u-col span="3"><view class="demo-layout bg-purple"></view></u-col>
  24. */
  25. export default {
  26. name: "u-col",
  27. props: {
  28. // 占父容器宽度的多少等分,总分为12份
  29. span: {
  30. type: [Number, String],
  31. default: 12
  32. },
  33. // 指定栅格左侧的间隔数(总12栏)
  34. offset: {
  35. type: [Number, String],
  36. default: 0
  37. },
  38. // 水平排列方式,可选值为`start`(或`flex-start`)、`end`(或`flex-end`)、`center`、`around`(或`space-around`)、`between`(或`space-between`)
  39. justify: {
  40. type: String,
  41. default: 'start'
  42. },
  43. // 垂直对齐方式,可选值为top、center、bottom
  44. align: {
  45. type: String,
  46. default: 'center'
  47. },
  48. // 文字对齐方式
  49. textAlign: {
  50. type: String,
  51. default: 'left'
  52. }
  53. },
  54. inject: ['gutter'],
  55. computed: {
  56. uJustify() {
  57. if (this.justify == 'end' || this.justify == 'start') return 'flex-' + this.justify;
  58. else if (this.justify == 'around' || this.justify == 'between') return 'space-' + this.justify;
  59. else return this.justify;
  60. },
  61. uAlignItem() {
  62. if (this.align == 'top') return 'flex-start';
  63. if (this.align == 'bottom') return 'flex-end';
  64. else return this.align;
  65. }
  66. },
  67. methods: {
  68. click() {
  69. this.$emit('click');
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss">
  75. @import "../../libs/css/style.components.scss";
  76. .u-col {
  77. /* #ifdef MP-WEIXIN || MP-QQ */
  78. float: left;
  79. /* #endif */
  80. }
  81. .u-col-0 {
  82. width: 0;
  83. }
  84. .u-col-1 {
  85. width: calc(100%/12);
  86. }
  87. .u-col-2 {
  88. width: calc(100%/12 * 2);
  89. }
  90. .u-col-3 {
  91. width: calc(100%/12 * 3);
  92. }
  93. .u-col-4 {
  94. width: calc(100%/12 * 4);
  95. }
  96. .u-col-5 {
  97. width: calc(100%/12 * 5);
  98. }
  99. .u-col-6 {
  100. width: calc(100%/12 * 6);
  101. }
  102. .u-col-7 {
  103. width: calc(100%/12 * 7);
  104. }
  105. .u-col-8 {
  106. width: calc(100%/12 * 8);
  107. }
  108. .u-col-9 {
  109. width: calc(100%/12 * 9);
  110. }
  111. .u-col-10 {
  112. width: calc(100%/12 * 10);
  113. }
  114. .u-col-11 {
  115. width: calc(100%/12 * 11);
  116. }
  117. .u-col-12 {
  118. width: calc(100%/12 * 12);
  119. }
  120. </style>