guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 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 | |
| 17 | <script> |
| 18 | /** |
| 19 | * col 布局单元格 |
| 20 | * @description 通过基础的 12 分栏,迅速简便地创建布局(搭配<u-row>使用) |
| 21 | * @tutorial https://www.uviewui.com/components/layout.html |
| 22 | * @property {String Number} span 栅格占据的列数,总12等分(默认0) |
| 23 | * @property {String} text-align 文字水平对齐方式(默认left) |
| 24 | * @property {String Number} offset 分栏左边偏移,计算方式与span相同(默认0) |
| 25 | * @example <u-col span="3"><view class="demo-layout bg-purple"></view></u-col> |
| 26 | */ |
| 27 | export default { |
| 28 | name: "u-col", |
| 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'); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | </script> |
| 95 | |
| 96 | <style lang="scss"> |
| 97 | @import "../../libs/css/style.components.scss"; |
| 98 | |
| 99 | .u-col { |
| 100 | /* #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO */ |
| 101 | float: left; |
| 102 | /* #endif */ |
| 103 | } |
| 104 | |
| 105 | .u-col-0 { |
| 106 | width: 0; |
| 107 | } |
| 108 | |
| 109 | .u-col-1 { |
| 110 | width: calc(100%/12); |
| 111 | } |
| 112 | |
| 113 | .u-col-2 { |
| 114 | width: calc(100%/12 * 2); |
| 115 | } |
| 116 | |
| 117 | .u-col-3 { |
| 118 | width: calc(100%/12 * 3); |
| 119 | } |
| 120 | |
| 121 | .u-col-4 { |
| 122 | width: calc(100%/12 * 4); |
| 123 | } |
| 124 | |
| 125 | .u-col-5 { |
| 126 | width: calc(100%/12 * 5); |
| 127 | } |
| 128 | |
| 129 | .u-col-6 { |
| 130 | width: calc(100%/12 * 6); |
| 131 | } |
| 132 | |
| 133 | .u-col-7 { |
| 134 | width: calc(100%/12 * 7); |
| 135 | } |
| 136 | |
| 137 | .u-col-8 { |
| 138 | width: calc(100%/12 * 8); |
| 139 | } |
| 140 | |
| 141 | .u-col-9 { |
| 142 | width: calc(100%/12 * 9); |
| 143 | } |
| 144 | |
| 145 | .u-col-10 { |
| 146 | width: calc(100%/12 * 10); |
| 147 | } |
| 148 | |
| 149 | .u-col-11 { |
| 150 | width: calc(100%/12 * 11); |
| 151 | } |
| 152 | |
| 153 | .u-col-12 { |
| 154 | width: calc(100%/12 * 12); |
| 155 | } |
| 156 | </style> |