¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <!-- #ifndef APP-NVUE --> |
| | | <view :class="['uni-col', sizeClass, pointClassList]" :style="{ |
| | | paddingLeft:`${Number(gutter)}rpx`, |
| | | paddingRight:`${Number(gutter)}rpx`, |
| | | }"> |
| | | <slot></slot> |
| | | </view> |
| | | <!-- #endif --> |
| | | <!-- #ifdef APP-NVUE --> |
| | | <!-- å¨nvueä¸ï¼ç±»åæ ·å¼ä¸çæï¼æ¢ä¸ºstyle --> |
| | | <!-- 设置rightæ£å¼å¤±æï¼è®¾ç½® left è´å¼ --> |
| | | <view :class="['uni-col']" :style="{ |
| | | paddingLeft:`${Number(gutter)}rpx`, |
| | | paddingRight:`${Number(gutter)}rpx`, |
| | | width:`${nvueWidth}rpx`, |
| | | position:'relative', |
| | | marginLeft:`${marginLeft}rpx`, |
| | | left:`${right === 0 ? left : -right}rpx` |
| | | }"> |
| | | <slot></slot> |
| | | </view> |
| | | <!-- #endif --> |
| | | </template> |
| | | |
| | | <script> |
| | | /** |
| | | * Col å¸å±-å |
| | | * @description æé
uni-row使ç¨ï¼æå»ºå¸å±ã |
| | | * @tutorial https://ext.dcloud.net.cn/plugin?id=3958 |
| | | * |
| | | * @property {span} type = Number æ
æ ¼å æ®çåæ° |
| | | * é»è®¤ 24 |
| | | * @property {offset} type = Number æ
æ ¼å·¦ä¾§çé´éæ ¼æ° |
| | | * @property {push} type = Number æ
æ ¼åå³ç§»å¨æ ¼æ° |
| | | * @property {pull} type = Number æ
æ ¼åå·¦ç§»å¨æ ¼æ° |
| | | * @property {xs} type = [Number, Object] <768px ååºå¼æ
æ ¼æ°æè
æ
æ ¼å±æ§å¯¹è±¡ |
| | | * @description Numberæ¶è¡¨ç¤ºå¨æ¤å±å¹å®½åº¦ä¸ï¼æ
æ ¼å æ®çåæ°ãObjectæ¶å¯é
ç½®å¤ä¸ªæè¿°{span: 4, offset: 4} |
| | | * @property {sm} type = [Number, Object] â¥768px ååºå¼æ
æ ¼æ°æè
æ
æ ¼å±æ§å¯¹è±¡ |
| | | * @description Numberæ¶è¡¨ç¤ºå¨æ¤å±å¹å®½åº¦ä¸ï¼æ
æ ¼å æ®çåæ°ãObjectæ¶å¯é
ç½®å¤ä¸ªæè¿°{span: 4, offset: 4} |
| | | * @property {md} type = [Number, Object] â¥992px ååºå¼æ
æ ¼æ°æè
æ
æ ¼å±æ§å¯¹è±¡ |
| | | * @description Numberæ¶è¡¨ç¤ºå¨æ¤å±å¹å®½åº¦ä¸ï¼æ
æ ¼å æ®çåæ°ãObjectæ¶å¯é
ç½®å¤ä¸ªæè¿°{span: 4, offset: 4} |
| | | * @property {lg} type = [Number, Object] â¥1200px ååºå¼æ
æ ¼æ°æè
æ
æ ¼å±æ§å¯¹è±¡ |
| | | * @description Numberæ¶è¡¨ç¤ºå¨æ¤å±å¹å®½åº¦ä¸ï¼æ
æ ¼å æ®çåæ°ãObjectæ¶å¯é
ç½®å¤ä¸ªæè¿°{span: 4, offset: 4} |
| | | * @property {xl} type = [Number, Object] â¥1920px ååºå¼æ
æ ¼æ°æè
æ
æ ¼å±æ§å¯¹è±¡ |
| | | * @description Numberæ¶è¡¨ç¤ºå¨æ¤å±å¹å®½åº¦ä¸ï¼æ
æ ¼å æ®çåæ°ãObjectæ¶å¯é
ç½®å¤ä¸ªæè¿°{span: 4, offset: 4} |
| | | */ |
| | | const ComponentClass = 'uni-col'; |
| | | |
| | | // -1 é»è®¤å¼ï¼å 为å¨å¾®ä¿¡å°ç¨åºç«¯åªç»Number伿é»è®¤å¼0 |
| | | export default { |
| | | name: 'uniCol', |
| | | // #ifdef MP-WEIXIN |
| | | options: { |
| | | virtualHost: true // å¨å¾®ä¿¡å°ç¨åºä¸å°ç»ä»¶èç¹æ¸²æä¸ºèæèç¹ï¼æ´å æ¥è¿Vueç»ä»¶çè¡¨ç° |
| | | }, |
| | | // #endif |
| | | props: { |
| | | span: { |
| | | type: Number, |
| | | default: 24 |
| | | }, |
| | | offset: { |
| | | type: Number, |
| | | default: -1 |
| | | }, |
| | | pull: { |
| | | type: Number, |
| | | default: -1 |
| | | }, |
| | | push: { |
| | | type: Number, |
| | | default: -1 |
| | | }, |
| | | xs: [Number, Object], |
| | | sm: [Number, Object], |
| | | md: [Number, Object], |
| | | lg: [Number, Object], |
| | | xl: [Number, Object] |
| | | }, |
| | | data() { |
| | | return { |
| | | gutter: 0, |
| | | sizeClass: '', |
| | | parentWidth: 0, |
| | | nvueWidth: 0, |
| | | marginLeft: 0, |
| | | right: 0, |
| | | left: 0 |
| | | } |
| | | }, |
| | | created() { |
| | | // åèå°ç¨åºä¸ï¼å¨computedä¸è¯»å$parent为undefined |
| | | let parent = this.$parent; |
| | | |
| | | while (parent && parent.$options.componentName !== 'uniRow') { |
| | | parent = parent.$parent; |
| | | } |
| | | |
| | | this.updateGutter(parent.gutter) |
| | | parent.$watch('gutter', (gutter) => { |
| | | this.updateGutter(gutter) |
| | | }) |
| | | |
| | | // #ifdef APP-NVUE |
| | | this.updateNvueWidth(parent.width) |
| | | parent.$watch('width', (width) => { |
| | | this.updateNvueWidth(width) |
| | | }) |
| | | // #endif |
| | | }, |
| | | computed: { |
| | | sizeList() { |
| | | let { |
| | | span, |
| | | offset, |
| | | pull, |
| | | push |
| | | } = this; |
| | | |
| | | return { |
| | | span, |
| | | offset, |
| | | pull, |
| | | push |
| | | } |
| | | }, |
| | | // #ifndef APP-NVUE |
| | | pointClassList() { |
| | | let classList = []; |
| | | |
| | | ['xs', 'sm', 'md', 'lg', 'xl'].forEach(point => { |
| | | const props = this[point]; |
| | | if (typeof props === 'number') { |
| | | classList.push(`${ComponentClass}-${point}-${props}`) |
| | | } else if (typeof props === 'object' && props) { |
| | | Object.keys(props).forEach(pointProp => { |
| | | classList.push( |
| | | pointProp === 'span' ? |
| | | `${ComponentClass}-${point}-${props[pointProp]}` : |
| | | `${ComponentClass}-${point}-${pointProp}-${props[pointProp]}` |
| | | ) |
| | | }) |
| | | } |
| | | }); |
| | | |
| | | // æ¯ä»å®å°ç¨åºä½¿ç¨ :class=[ ['a','b'] ]ï¼æ¸²æé误 |
| | | return classList.join(' '); |
| | | } |
| | | // #endif |
| | | }, |
| | | methods: { |
| | | updateGutter(parentGutter) { |
| | | parentGutter = Number(parentGutter); |
| | | if (!isNaN(parentGutter)) { |
| | | this.gutter = parentGutter / 2 |
| | | } |
| | | }, |
| | | // #ifdef APP-NVUE |
| | | updateNvueWidth(width) { |
| | | // ç¨äºå¨nvue端ï¼spanï¼offsetï¼pullï¼pushçè®¡ç® |
| | | this.parentWidth = width; |
| | | ['span', 'offset', 'pull', 'push'].forEach(size => { |
| | | const curSize = this[size]; |
| | | if ((curSize || curSize === 0) && curSize !== -1) { |
| | | let RPX = 1 / 24 * curSize * width |
| | | RPX = Number(RPX); |
| | | switch (size) { |
| | | case 'span': |
| | | this.nvueWidth = RPX |
| | | break; |
| | | case 'offset': |
| | | this.marginLeft = RPX |
| | | break; |
| | | case 'pull': |
| | | this.right = RPX |
| | | break; |
| | | case 'push': |
| | | this.left = RPX |
| | | break; |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | // #endif |
| | | }, |
| | | watch: { |
| | | sizeList: { |
| | | immediate: true, |
| | | handler(newVal) { |
| | | // #ifndef APP-NVUE |
| | | let classList = []; |
| | | for (let size in newVal) { |
| | | const curSize = newVal[size]; |
| | | if ((curSize || curSize === 0) && curSize !== -1) { |
| | | classList.push( |
| | | size === 'span' ? |
| | | `${ComponentClass}-${curSize}` : |
| | | `${ComponentClass}-${size}-${curSize}` |
| | | ) |
| | | } |
| | | } |
| | | // æ¯ä»å®å°ç¨åºä½¿ç¨ :class=[ ['a','b'] ]ï¼æ¸²æé误 |
| | | this.sizeClass = classList.join(' '); |
| | | // #endif |
| | | // #ifdef APP-NVUE |
| | | this.updateNvueWidth(this.parentWidth); |
| | | // #endif |
| | | } |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang='scss' > |
| | | /* breakpoints */ |
| | | $--sm: 768px !default; |
| | | $--md: 992px !default; |
| | | $--lg: 1200px !default; |
| | | $--xl: 1920px !default; |
| | | |
| | | $breakpoints: ('xs' : (max-width: $--sm - 1), |
| | | 'sm' : (min-width: $--sm), |
| | | 'md' : (min-width: $--md), |
| | | 'lg' : (min-width: $--lg), |
| | | 'xl' : (min-width: $--xl)); |
| | | |
| | | $layout-namespace: ".uni-"; |
| | | $col: $layout-namespace+"col"; |
| | | |
| | | @function getSize($size) { |
| | | /* TODO 1/24 * $size * 100 * 1%; 使ç¨è®¡ç®åçå¼ï¼ä¸ºäºè§£å³ vue3 æ§å¶å°æ¥é */ |
| | | @return 0.04166666666 * $size * 100 * 1%; |
| | | } |
| | | |
| | | @mixin res($key, $map:$breakpoints) { |
| | | @if map-has-key($map, $key) { |
| | | @media screen and #{inspect(map-get($map,$key))} { |
| | | @content; |
| | | } |
| | | } |
| | | |
| | | @else { |
| | | @warn "Undeinfed point: `#{$key}`"; |
| | | } |
| | | } |
| | | |
| | | /* #ifndef APP-NVUE */ |
| | | #{$col} { |
| | | float: left; |
| | | box-sizing: border-box; |
| | | } |
| | | |
| | | #{$col}-0 { |
| | | /* #ifdef APP-NVUE */ |
| | | width: 0; |
| | | height: 0; |
| | | margin-top: 0; |
| | | margin-right: 0; |
| | | margin-bottom: 0; |
| | | margin-left: 0; |
| | | /* #endif */ |
| | | /* #ifndef APP-NVUE */ |
| | | display: none; |
| | | /* #endif */ |
| | | } |
| | | |
| | | @for $i from 0 through 24 { |
| | | #{$col}-#{$i} { |
| | | width: getSize($i); |
| | | } |
| | | |
| | | #{$col}-offset-#{$i} { |
| | | margin-left: getSize($i); |
| | | } |
| | | |
| | | #{$col}-pull-#{$i} { |
| | | position: relative; |
| | | right: getSize($i); |
| | | } |
| | | |
| | | #{$col}-push-#{$i} { |
| | | position: relative; |
| | | left: getSize($i); |
| | | } |
| | | } |
| | | |
| | | @each $point in map-keys($breakpoints) { |
| | | @include res($point) { |
| | | #{$col}-#{$point}-0 { |
| | | display: none; |
| | | } |
| | | |
| | | @for $i from 0 through 24 { |
| | | #{$col}-#{$point}-#{$i} { |
| | | width: getSize($i); |
| | | } |
| | | |
| | | #{$col}-#{$point}-offset-#{$i} { |
| | | margin-left: getSize($i); |
| | | } |
| | | |
| | | #{$col}-#{$point}-pull-#{$i} { |
| | | position: relative; |
| | | right: getSize($i); |
| | | } |
| | | |
| | | #{$col}-#{$point}-push-#{$i} { |
| | | position: relative; |
| | | left: getSize($i); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /* #endif */ |
| | | </style> |