¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view |
| | | class="u-subsection" |
| | | ref="u-subsection" |
| | | :class="[`u-subsection--${mode}`]" |
| | | :style="[$u.addStyle(customStyle), wrapperStyle]" |
| | | > |
| | | <view |
| | | class="u-subsection__bar" |
| | | ref="u-subsection__bar" |
| | | :style="[barStyle]" |
| | | :class="[ |
| | | mode === 'button' && 'u-subsection--button__bar', |
| | | current === 0 && |
| | | mode === 'subsection' && |
| | | 'u-subsection__bar--first', |
| | | current > 0 && |
| | | current < list.length - 1 && |
| | | mode === 'subsection' && |
| | | 'u-subsection__bar--center', |
| | | current === list.length - 1 && |
| | | mode === 'subsection' && |
| | | 'u-subsection__bar--last', |
| | | ]" |
| | | ></view> |
| | | <view |
| | | class="u-subsection__item" |
| | | :class="[ |
| | | `u-subsection__item--${index}`, |
| | | index < list.length - 1 && |
| | | 'u-subsection__item--no-border-right', |
| | | index === 0 && 'u-subsection__item--first', |
| | | index === list.length - 1 && 'u-subsection__item--last', |
| | | ]" |
| | | :ref="`u-subsection__item--${index}`" |
| | | :style="[itemStyle(index)]" |
| | | @tap="clickHandler(index)" |
| | | v-for="(item, index) in list" |
| | | :key="index" |
| | | > |
| | | <text |
| | | class="u-subsection__item__text" |
| | | :style="[textStyle(index)]" |
| | | >{{ getText(item) }}</text |
| | | > |
| | | </view> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | // #ifdef APP-NVUE |
| | | const dom = uni.requireNativePlugin("dom"); |
| | | const animation = uni.requireNativePlugin("animation"); |
| | | // #endif |
| | | import props from "./props.js"; |
| | | /** |
| | | * Subsection åæ®µå¨ |
| | | * @description è¯¥åæ®µå¨ä¸è¬ç¨äºç¨æ·ä»å 个é项ä¸éæ©æä¸ä¸ªçåºæ¯ |
| | | * @tutorial https://www.uviewui.com/components/subsection.html |
| | | * @property {Array} list tabçæ°æ® |
| | | * @property {String ï½ Number} current å½åæ´»å¨çtabçindexï¼é»è®¤ 0 ï¼ |
| | | * @property {String} activeColor æ¿æ´»æ¶çé¢è²ï¼é»è®¤ '#3c9cff' ï¼ |
| | | * @property {String} inactiveColor æªæ¿æ´»æ¶çé¢è²ï¼é»è®¤ '#303133' ï¼ |
| | | * @property {String} mode 模å¼éæ©ï¼mode=button为æé®å½¢å¼ï¼mode=subsectionæ¶ä¸ºå段模å¼ï¼é»è®¤ 'button' ï¼ |
| | | * @property {String ï½ Number} fontSize åä½å¤§å°ï¼åä½pxï¼é»è®¤ 12 ï¼ |
| | | * @property {Boolean} bold æ¿æ´»é项çå使¯å¦å ç²ï¼é»è®¤ true ï¼ |
| | | * @property {String} bgColor ç»ä»¶èæ¯é¢è²ï¼mode为buttonæ¶ææï¼é»è®¤ '#eeeeef' ï¼ |
| | | * @property {Object} customStyle å®ä¹éè¦ç¨å°çå¤é¨æ ·å¼ |
| | | * @property {String} keyName ä»`list`å
ç´ å¯¹è±¡ä¸è¯»åçé®åï¼é»è®¤ 'name' ï¼ |
| | | * |
| | | * @event {Function} change åæ®µå¨é项åçæ¹åæ¶è§¦å åè° indexï¼é项çindexç´¢å¼å¼ï¼ä»0å¼å§ |
| | | * @example <u-subsection :list="list" :current="curNow" @change="sectionChange"></u-subsection> |
| | | */ |
| | | export default { |
| | | name: "u-subsection", |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
| | | data() { |
| | | return { |
| | | // ç»ä»¶å°ºå¯¸ |
| | | itemRect: { |
| | | width: 0, |
| | | height: 0, |
| | | }, |
| | | }; |
| | | }, |
| | | watch: { |
| | | list(newValue, oldValue) { |
| | | this.init(); |
| | | }, |
| | | current: { |
| | | immediate: true, |
| | | handler(n) { |
| | | // #ifdef APP-NVUE |
| | | // å¨å®ånvueä¸ï¼å¦æéè¿translateXè¿è¡ä½ç§»ï¼å°æåä¸ä¸ªæ¶ï¼ä¼å¯¼è´å³ä¾§æ æ³ç»å¶åè§ |
| | | // æ
ç¨animation模åè¿è¡ä½ç§» |
| | | const ref = this.$refs?.["u-subsection__bar"]?.ref; |
| | | // ä¸åå¨refçæ¶å(çè§£ä¸ºç¬¬ä¸æ¬¡åå§åæ¶ï¼éè¦æ¸²ædomï¼è¿è¡ä¸å®å»¶æ¶åè·åref)ï¼è¿éç100msæ¯ç»è¿æµè¯å¾åºçç»æ(æäºå®åéè¦å»¶æ¶ä¹
ä¸ç¹)ï¼å¿éæä¿®æ¹ |
| | | uni.$u.sleep(ref ? 0 : 100).then(() => { |
| | | animation.transition(this.$refs["u-subsection__bar"].ref, { |
| | | styles: { |
| | | transform: `translateX(${ |
| | | n * this.itemRect.width |
| | | }px)`, |
| | | transformOrigin: "center center", |
| | | }, |
| | | duration: 300, |
| | | }); |
| | | }); |
| | | // #endif |
| | | }, |
| | | }, |
| | | }, |
| | | computed: { |
| | | wrapperStyle() { |
| | | const style = {}; |
| | | // buttonæ¨¡å¼æ¶ï¼è®¾ç½®èæ¯è² |
| | | if (this.mode === "button") { |
| | | style.backgroundColor = this.bgColor; |
| | | } |
| | | return style; |
| | | }, |
| | | // æ»åçæ ·å¼ |
| | | barStyle() { |
| | | const style = {}; |
| | | style.width = `${this.itemRect.width}px`; |
| | | style.height = `${this.itemRect.height}px`; |
| | | // éè¿translateXç§»å¨æ»åï¼å
¶ç§»å¨çè·ç¦»ä¸ºç´¢å¼*itemç宽度 |
| | | // #ifndef APP-NVUE |
| | | style.transform = `translateX(${ |
| | | this.current * this.itemRect.width |
| | | }px)`; |
| | | // #endif |
| | | if (this.mode === "subsection") { |
| | | // å¨subsection模å¼ä¸ï¼éè¦å¨æè®¾ç½®æ»åçåè§ï¼å ä¸ºç§»å¨æ»å使ç¨çæ¯translateXï¼æ æ³éè¿ç¶å
ç´ è®¾ç½®overflow: hiddenéèæ»åçç´è§ |
| | | style.backgroundColor = this.activeColor; |
| | | } |
| | | return style; |
| | | }, |
| | | // åæ®µå¨itemçæ ·å¼ |
| | | itemStyle(index) { |
| | | return (index) => { |
| | | const style = {}; |
| | | if (this.mode === "subsection") { |
| | | // 设置borderçæ ·å¼ |
| | | style.borderColor = this.activeColor; |
| | | style.borderWidth = "1px"; |
| | | style.borderStyle = "solid"; |
| | | } |
| | | return style; |
| | | }; |
| | | }, |
| | | // åæ®µå¨æåé¢è² |
| | | textStyle(index) { |
| | | return (index) => { |
| | | const style = {}; |
| | | style.fontWeight = |
| | | this.bold && this.current === index ? "bold" : "normal"; |
| | | style.fontSize = uni.$u.addUnit(this.fontSize); |
| | | // subsection模å¼ä¸ï¼æ¿æ´»æ¶é»è®¤ä¸ºç½è²çæå |
| | | if (this.mode === "subsection") { |
| | | style.color = |
| | | this.current === index ? "#fff" : this.inactiveColor; |
| | | } else { |
| | | // button模å¼ä¸ï¼æ¿æ´»æ¶æåé¢è²é»è®¤ä¸ºactiveColor |
| | | style.color = |
| | | this.current === index |
| | | ? this.activeColor |
| | | : this.inactiveColor; |
| | | } |
| | | return style; |
| | | }; |
| | | }, |
| | | }, |
| | | mounted() { |
| | | this.init(); |
| | | }, |
| | | methods: { |
| | | init() { |
| | | uni.$u.sleep().then(() => this.getRect()); |
| | | }, |
| | | // 夿å±ç¤ºææ¬ |
| | | getText(item) { |
| | | return typeof item === 'object' ? item[this.keyName] : item |
| | | }, |
| | | // è·åç»ä»¶ç尺寸 |
| | | getRect() { |
| | | // #ifndef APP-NVUE |
| | | this.$uGetRect(".u-subsection__item--0").then((size) => { |
| | | this.itemRect = size; |
| | | }); |
| | | // #endif |
| | | |
| | | // #ifdef APP-NVUE |
| | | const ref = this.$refs["u-subsection__item--0"][0]; |
| | | ref && |
| | | dom.getComponentRect(ref, (res) => { |
| | | this.itemRect = res.size; |
| | | }); |
| | | // #endif |
| | | }, |
| | | clickHandler(index) { |
| | | this.$emit("change", index); |
| | | }, |
| | | }, |
| | | }; |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | .u-subsection { |
| | | @include flex; |
| | | position: relative; |
| | | overflow: hidden; |
| | | /* #ifndef APP-NVUE */ |
| | | width: 100%; |
| | | box-sizing: border-box; |
| | | /* #endif */ |
| | | |
| | | &--button { |
| | | height: 32px; |
| | | background-color: rgb(238, 238, 239); |
| | | padding: 3px; |
| | | border-radius: 3px; |
| | | align-items: stretch; |
| | | |
| | | &__bar { |
| | | background-color: #ffffff; |
| | | border-radius: 3px !important; |
| | | } |
| | | } |
| | | |
| | | &--subsection { |
| | | height: 30px; |
| | | } |
| | | |
| | | &__bar { |
| | | position: absolute; |
| | | /* #ifndef APP-NVUE */ |
| | | transition-property: transform, color; |
| | | transition-duration: 0.3s; |
| | | transition-timing-function: ease-in-out; |
| | | /* #endif */ |
| | | |
| | | &--first { |
| | | border-top-left-radius: 3px; |
| | | border-bottom-left-radius: 3px; |
| | | border-top-right-radius: 0px; |
| | | border-bottom-right-radius: 0px; |
| | | } |
| | | |
| | | &--center { |
| | | border-top-left-radius: 0px; |
| | | border-bottom-left-radius: 0px; |
| | | border-top-right-radius: 0px; |
| | | border-bottom-right-radius: 0px; |
| | | } |
| | | |
| | | &--last { |
| | | border-top-left-radius: 0px; |
| | | border-bottom-left-radius: 0px; |
| | | border-top-right-radius: 3px; |
| | | border-bottom-right-radius: 3px; |
| | | } |
| | | } |
| | | |
| | | &__item { |
| | | @include flex; |
| | | flex: 1; |
| | | justify-content: center; |
| | | align-items: center; |
| | | // vueç¯å¢ä¸ï¼éè¦è®¾ç½®ç¸å¯¹å®ä½ï¼å 为æ»å为ç»å¯¹å®ä½ï¼iteméè¦å¨æ»åçä¸é¢ |
| | | position: relative; |
| | | |
| | | &--no-border-right { |
| | | border-right-width: 0 !important; |
| | | } |
| | | |
| | | &--first { |
| | | border-top-left-radius: 3px; |
| | | border-bottom-left-radius: 3px; |
| | | } |
| | | |
| | | &--last { |
| | | border-top-right-radius: 3px; |
| | | border-bottom-right-radius: 3px; |
| | | } |
| | | |
| | | &__text { |
| | | font-size: 12px; |
| | | line-height: 12px; |
| | | @include flex; |
| | | align-items: center; |
| | | transition-property: color; |
| | | transition-duration: 0.3s; |
| | | } |
| | | } |
| | | } |
| | | </style> |