推广小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.2 KiB

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