¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <!-- #ifndef APP-NVUE --> |
| | | <view |
| | | class="u-grid-item" |
| | | hover-class="u-grid-item--hover-class" |
| | | :hover-stay-time="200" |
| | | @tap="clickHandler" |
| | | :class="classes" |
| | | :style="[itemStyle]" |
| | | > |
| | | <slot /> |
| | | </view> |
| | | <!-- #endif --> |
| | | <!-- #ifdef APP-NVUE --> |
| | | <view |
| | | class="u-grid-item" |
| | | :hover-stay-time="200" |
| | | @tap="clickHandler" |
| | | :class="classes" |
| | | :style="[itemStyle]" |
| | | > |
| | | <slot /> |
| | | </view> |
| | | <!-- #endif --> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * gridItem æç¤º |
| | | * @description å®«æ ¼ç»ä»¶ä¸è¬ç¨äºåæ¶å±ç¤ºå¤ä¸ªå类项ç®çåºæ¯ï¼å¯ä»¥ç»å®«æ ¼ç项ç®è®¾ç½®å¾½æ ç»ä»¶(badge)ï¼æè
徿 çï¼ä¹å¯ä»¥æ©å±ä¸ºå·¦å³æ»å¨çè½®æå½¢å¼ãæé
u-gridä½¿ç¨ |
| | | * @tutorial https://www.uviewui.com/components/grid.html |
| | | * @property {String | Number} name å®«æ ¼çname ( é»è®¤ null ) |
| | | * @property {String} bgColor å®«æ ¼çèæ¯é¢è² ï¼é»è®¤ 'transparent' ï¼ |
| | | * @property {Object} customStyle èªå®ä¹æ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @event {Function} click ç¹å»å®«æ ¼è§¦å |
| | | * @example <u-grid-item></u-grid-item> |
| | | */ |
| | | export default { |
| | | name: "u-grid-item", |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | data() { |
| | | return { |
| | | parentData: { |
| | | col: 3, // ç¶ç»ä»¶ååçå®«æ ¼æ° |
| | | border: true, // æ¯å¦æ¾ç¤ºè¾¹æ¡ï¼æ ¹æ®ç¶ç»ä»¶å³å® |
| | | }, |
| | | // #ifdef APP-NVUE |
| | | width: 0, // nvue䏿è¿ä¹è®¡ç®ï¼vue䏿¾å°computedä¸ï¼å¦åä¼å 为延æ¶é æéªç |
| | | // #endif |
| | | classes: [], // ç±»åéåï¼ç¨äºå¤ææ¯å¦æ¾ç¤ºå³è¾¹åä¸è¾¹æ¡ |
| | | }; |
| | | }, |
| | | mounted() { |
| | | this.init() |
| | | }, |
| | | computed: { |
| | | // #ifndef APP-NVUE |
| | | // vue䏿¾å°computedä¸ï¼å¦åä¼å 为延æ¶é æéªç |
| | | width() { |
| | | return 100 / Number(this.parentData.col) + '%' |
| | | }, |
| | | // #endif |
| | | itemStyle() { |
| | | const style = { |
| | | background: this.bgColor, |
| | | width: this.width |
| | | } |
| | | return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)) |
| | | } |
| | | }, |
| | | methods: { |
| | | init() { |
| | | // ç¨äºå¨ç¶ç»ä»¶u-gridçchildrenä¸è¢«æ·»å å
¥åç»ä»¶æ¶ï¼ |
| | | // éæ°è®¡ç®itemçè¾¹æ¡ |
| | | uni.$on('$uGridItem', () => { |
| | | this.gridItemClasses() |
| | | }) |
| | | // ç¶ç»ä»¶çå®ä¾ |
| | | this.updateParentData() |
| | | // #ifdef APP-NVUE |
| | | // è·åå
ç´ è¯¥æçé¿åº¦ï¼nvueä¸è¦å»¶æ¶æåç¡® |
| | | this.$nextTick(function(){ |
| | | this.getItemWidth() |
| | | }) |
| | | // #endif |
| | | // ååºäºä»¶ï¼éç¥ææçgrid-itemé½éæ°è®¡ç®èªå·±çè¾¹æ¡ |
| | | uni.$emit('$uGridItem') |
| | | this.gridItemClasses() |
| | | }, |
| | | // è·åç¶ç»ä»¶çåæ° |
| | | updateParentData() { |
| | | // æ¤æ¹æ³åå¨mixinä¸ |
| | | this.getParentData('u-grid'); |
| | | }, |
| | | clickHandler() { |
| | | let name = this.name |
| | | // å¦ææ²¡æè®¾ç½®name屿§ï¼åéç¶ç»ä»¶çchildrenæ°ç»ï¼å¤æå½åçå
ç´ æ¯å¦åæ¬å®ä¾thisç¸çï¼æ¾åºå½åç»ä»¶çç´¢å¼ |
| | | const children = this.parent?.children |
| | | if(children && this.name === null) { |
| | | name = children.findIndex(child => child === this) |
| | | } |
| | | // è°ç¨ç¶ç»ä»¶æ¹æ³ï¼ååºäºä»¶ |
| | | this.parent && this.parent.childClick(name) |
| | | this.$emit('click', name) |
| | | }, |
| | | async getItemWidth() { |
| | | // 妿æ¯nvueï¼ä¸è½ä½¿ç¨ç¾åæ¯ï¼åªè½ä½¿ç¨åºå®å®½åº¦ |
| | | let width = 0 |
| | | if(this.parent) { |
| | | // è·åç¶ç»ä»¶å®½åº¦åï¼é¤ä»¥æ
æ ¼æ°ï¼å¾åºæ¯ä¸ªitemç宽度 |
| | | const parentWidth = await this.getParentWidth() |
| | | width = parentWidth / Number(this.parentData.col) + 'px' |
| | | } |
| | | this.width = width |
| | | }, |
| | | // è·åç¶å
ç´ ç尺寸 |
| | | getParentWidth() { |
| | | // #ifdef APP-NVUE |
| | | // è¿åä¸ä¸ªpromiseï¼è®©è°ç¨è
å¯ä»¥ç¨await忥è·å |
| | | const dom = uni.requireNativePlugin('dom') |
| | | return new Promise(resolve => { |
| | | // è°ç¨ç¶ç»ä»¶çref |
| | | dom.getComponentRect(this.parent.$refs['u-grid'], res => { |
| | | resolve(res.size.width) |
| | | }) |
| | | }) |
| | | // #endif |
| | | }, |
| | | gridItemClasses() { |
| | | if(this.parentData.border) { |
| | | const classes = [] |
| | | this.parent.children.map((child, index) =>{ |
| | | if(this === child) { |
| | | const len = this.parent.children.length |
| | | // è´´è¿å³è¾¹å±å¹è¾¹æ²¿çchildï¼å¹¶ä¸æåä¸ä¸ªï¼æ¯å¦åªææ¨ªå2ä¸ªçæ¶åï¼ï¼æ éå³è¾¹æ¡ |
| | | if((index + 1) % this.parentData.col !== 0 && index + 1 !== len) { |
| | | classes.push('u-border-right') |
| | | } |
| | | // æ»çå®«æ ¼æ°é坹忰åä½çå¼ |
| | | // 妿åä½åï¼å¼ä¸º0ï¼åæå³çè¦å°æå䏿çå®«æ ¼ï¼é½ä¸éè¦ä¸è¾¹æ¡ |
| | | const lessNum = len % this.parentData.col === 0 ? this.parentData.col : len % this.parentData.col |
| | | // æä¸é¢ç䏿childï¼æ éä¸è¾¹æ¡ |
| | | if(index < len - lessNum) { |
| | | classes.push('u-border-bottom') |
| | | } |
| | | } |
| | | }) |
| | | // æ¯ä»å®ï¼å¤´æ¡å°ç¨åºæ æ³å¨æç»å®ä¸ä¸ªæ°ç»ç±»åï¼å¦åè§£æåºæ¥çç»æä¼å¸¦æ","ï¼è导è´å¤±æ |
| | | // #ifdef MP-ALIPAY || MP-TOUTIAO |
| | | classes = classes.join(' ') |
| | | // #endif |
| | | this.classes = classes |
| | | } |
| | | } |
| | | }, |
| | | beforeDestroy() { |
| | | // ç§»é¤äºä»¶çå¬ï¼éæ¾æ§è½ |
| | | uni.$off('$uGridItem') |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | $u-grid-item-hover-class-opcatiy:.5 !default; |
| | | $u-grid-item-margin-top:1rpx !default; |
| | | $u-grid-item-border-right-width:0.5px !default; |
| | | $u-grid-item-border-bottom-width:0.5px !default; |
| | | $u-grid-item-border-right-color:$u-border-color !default; |
| | | $u-grid-item-border-bottom-color:$u-border-color !default; |
| | | .u-grid-item { |
| | | align-items: center; |
| | | justify-content: center; |
| | | position: relative; |
| | | flex-direction: column; |
| | | /* #ifndef APP-NVUE */ |
| | | box-sizing: border-box; |
| | | display: flex; |
| | | /* #endif */ |
| | | |
| | | /* #ifdef MP */ |
| | | position: relative; |
| | | float: left; |
| | | /* #endif */ |
| | | |
| | | /* #ifdef MP-WEIXIN */ |
| | | margin-top:$u-grid-item-margin-top; |
| | | /* #endif */ |
| | | |
| | | &--hover-class { |
| | | opacity:$u-grid-item-hover-class-opcatiy; |
| | | } |
| | | } |
| | | |
| | | /* #ifdef APP-NVUE */ |
| | | // ç±äºnvue䏿¯æç»ä»¶å
å¼å
¥app.vueä¸åå¼å
¥çæ ·å¼ï¼æä»¥éè¦åå¨è¿é |
| | | .u-border-right { |
| | | border-right-width:$u-grid-item-border-right-width; |
| | | border-color: $u-grid-item-border-right-color; |
| | | } |
| | | |
| | | .u-border-bottom { |
| | | border-bottom-width:$u-grid-item-border-bottom-width; |
| | | border-color:$u-grid-item-border-bottom-color; |
| | | } |
| | | |
| | | /* #endif */ |
| | | </style> |