u-col.vue 3.1 KB

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