u-col.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view
  3. class="u-col"
  4. ref="u-col"
  5. :class="[
  6. 'u-col-' + span
  7. ]"
  8. :style="[colStyle]"
  9. @tap="clickHandler"
  10. >
  11. <slot></slot>
  12. </view>
  13. </template>
  14. <script>
  15. import props from './props.js';
  16. import mpMixin from '../../libs/mixin/mpMixin.js';
  17. import mixin from '../../libs/mixin/mixin.js';
  18. /**
  19. * CodeInput 栅格系统的列
  20. * @description 该组件一般用于Layout 布局 通过基础的 12 分栏,迅速简便地创建布局
  21. * @tutorial https://ijry.github.io/uview-plus/components/Layout.html
  22. * @property {String | Number} span 栅格占据的列数,总12等份 (默认 12 )
  23. * @property {String | Number} offset 分栏左边偏移,计算方式与span相同 (默认 0 )
  24. * @property {String} justify 水平排列方式,可选值为`start`(或`flex-start`)、`end`(或`flex-end`)、`center`、`around`(或`space-around`)、`between`(或`space-between`) (默认 'start' )
  25. * @property {String} align 垂直对齐方式,可选值为top、center、bottom、stretch (默认 'stretch' )
  26. * @property {String} textAlign 文字水平对齐方式 (默认 'left' )
  27. * @property {Object} customStyle 定义需要用到的外部样式
  28. * @event {Function} click col被点击,会阻止事件冒泡到row
  29. * @example <u-col span="3" offset="3" > <view class="demo-layout bg-purple"></view> </u-col>
  30. */
  31. export default {
  32. name: 'u-col',
  33. mixins: [mpMixin, mixin, props],
  34. data() {
  35. return {
  36. width: 0,
  37. parentData: {
  38. gutter: 0
  39. },
  40. gridNum: 12
  41. }
  42. },
  43. // 微信小程序中 options 选项
  44. options: {
  45. virtualHost: true // 将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  46. },
  47. computed: {
  48. uJustify() {
  49. if (this.justify == 'end' || this.justify == 'start') return 'flex-' + this.justify
  50. else if (this.justify == 'around' || this.justify == 'between') return 'space-' + this.justify
  51. else return this.justify
  52. },
  53. uAlignItem() {
  54. if (this.align == 'top') return 'flex-start'
  55. if (this.align == 'bottom') return 'flex-end'
  56. else return this.align
  57. },
  58. colStyle() {
  59. const style = {
  60. // 这里写成"padding: 0 10px"的形式是因为nvue的需要
  61. paddingLeft: uni.$u.addUnit(uni.$u.getPx(this.parentData.gutter)/2),
  62. paddingRight: uni.$u.addUnit(uni.$u.getPx(this.parentData.gutter)/2),
  63. alignItems: this.uAlignItem,
  64. justifyContent: this.uJustify,
  65. textAlign: this.textAlign,
  66. // #ifndef APP-NVUE
  67. // 在非nvue上,使用百分比形式
  68. flex: `0 0 ${100 / this.gridNum * this.span}%`,
  69. marginLeft: 100 / 12 * this.offset + '%',
  70. // #endif
  71. // #ifdef APP-NVUE
  72. // 在nvue上,由于无法使用百分比单位,这里需要获取父组件的宽度,再计算得出该有对应的百分比尺寸
  73. width: uni.$u.addUnit(Math.floor(this.width / this.gridNum * Number(this.span))),
  74. marginLeft: uni.$u.addUnit(Math.floor(this.width / this.gridNum * Number(this.offset))),
  75. // #endif
  76. }
  77. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  78. }
  79. },
  80. mounted() {
  81. this.init()
  82. },
  83. emits: ["click"],
  84. methods: {
  85. async init() {
  86. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  87. this.updateParentData()
  88. this.width = await this.parent.getComponentWidth()
  89. },
  90. updateParentData() {
  91. this.getParentData('u-row')
  92. },
  93. clickHandler(e) {
  94. this.$emit('click');
  95. }
  96. },
  97. }
  98. </script>
  99. <style lang="scss" scoped>
  100. @import "../../libs/css/components.scss";
  101. .u-col {
  102. padding: 0;
  103. /* #ifndef APP-NVUE */
  104. box-sizing:border-box;
  105. /* #endif */
  106. /* #ifdef MP */
  107. display: block;
  108. /* #endif */
  109. }
  110. // nvue下百分比无效
  111. /* #ifndef APP-NVUE */
  112. .u-col-0 {
  113. width: 0;
  114. }
  115. .u-col-1 {
  116. width: calc(100%/12);
  117. }
  118. .u-col-2 {
  119. width: calc(100%/12 * 2);
  120. }
  121. .u-col-3 {
  122. width: calc(100%/12 * 3);
  123. }
  124. .u-col-4 {
  125. width: calc(100%/12 * 4);
  126. }
  127. .u-col-5 {
  128. width: calc(100%/12 * 5);
  129. }
  130. .u-col-6 {
  131. width: calc(100%/12 * 6);
  132. }
  133. .u-col-7 {
  134. width: calc(100%/12 * 7);
  135. }
  136. .u-col-8 {
  137. width: calc(100%/12 * 8);
  138. }
  139. .u-col-9 {
  140. width: calc(100%/12 * 9);
  141. }
  142. .u-col-10 {
  143. width: calc(100%/12 * 10);
  144. }
  145. .u-col-11 {
  146. width: calc(100%/12 * 11);
  147. }
  148. .u-col-12 {
  149. width: calc(100%/12 * 12);
  150. }
  151. /* #endif */
  152. </style>