u-col.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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="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. emits: ["click"],
  29. props: {
  30. // 占父容器宽度的多少等分,总分为12份
  31. span: {
  32. type: [Number, String],
  33. default: 12
  34. },
  35. // 指定栅格左侧的间隔数(总12栏)
  36. offset: {
  37. type: [Number, String],
  38. default: 0
  39. },
  40. // 水平排列方式,可选值为`start`(或`flex-start`)、`end`(或`flex-end`)、`center`、`around`(或`space-around`)、`between`(或`space-between`)
  41. justify: {
  42. type: String,
  43. default: 'start'
  44. },
  45. // 垂直对齐方式,可选值为top、center、bottom
  46. align: {
  47. type: String,
  48. default: 'center'
  49. },
  50. // 文字对齐方式
  51. textAlign: {
  52. type: String,
  53. default: 'left'
  54. },
  55. // 是否阻止事件传播
  56. stop: {
  57. type: Boolean,
  58. default: true
  59. }
  60. },
  61. data() {
  62. return {
  63. gutter: 20, // 给col添加间距,左右边距各占一半,从父组件u-row获取
  64. }
  65. },
  66. created() {
  67. this.parent = false;
  68. },
  69. mounted() {
  70. // 获取父组件实例,并赋值给对应的参数
  71. this.parent = this.$u.$parent.call(this, 'u-row');
  72. if (this.parent) {
  73. this.gutter = this.parent.gutter;
  74. }
  75. },
  76. computed: {
  77. uJustify() {
  78. if (this.justify == 'end' || this.justify == 'start') return 'flex-' + this.justify;
  79. else if (this.justify == 'around' || this.justify == 'between') return 'space-' + this.justify;
  80. else return this.justify;
  81. },
  82. uAlignItem() {
  83. if (this.align == 'top') return 'flex-start';
  84. if (this.align == 'bottom') return 'flex-end';
  85. else return this.align;
  86. }
  87. },
  88. methods: {
  89. click(e) {
  90. this.$emit('click',e);
  91. }
  92. }
  93. }
  94. </script>
  95. <style lang="scss">
  96. @import "../../libs/css/style.components.scss";
  97. .u-col {
  98. /* #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO */
  99. float: left;
  100. /* #endif */
  101. }
  102. .u-col-0 {
  103. width: 0;
  104. }
  105. .u-col-1 {
  106. width: calc(100%/12);
  107. }
  108. .u-col-2 {
  109. width: calc(100%/12 * 2);
  110. }
  111. .u-col-3 {
  112. width: calc(100%/12 * 3);
  113. }
  114. .u-col-4 {
  115. width: calc(100%/12 * 4);
  116. }
  117. .u-col-5 {
  118. width: calc(100%/12 * 5);
  119. }
  120. .u-col-6 {
  121. width: calc(100%/12 * 6);
  122. }
  123. .u-col-7 {
  124. width: calc(100%/12 * 7);
  125. }
  126. .u-col-8 {
  127. width: calc(100%/12 * 8);
  128. }
  129. .u-col-9 {
  130. width: calc(100%/12 * 9);
  131. }
  132. .u-col-10 {
  133. width: calc(100%/12 * 10);
  134. }
  135. .u-col-11 {
  136. width: calc(100%/12 * 11);
  137. }
  138. .u-col-12 {
  139. width: calc(100%/12 * 12);
  140. }
  141. </style>