123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <template>
- <view class="u-tabs">
- <view class="u-tabs__wrapper">
- <slot name="left" />
- <view class="u-tabs__wrapper__scroll-view-wrapper">
- <scroll-view
- :scroll-x="scrollable"
- :scroll-left="scrollLeft"
- scroll-with-animation
- class="u-tabs__wrapper__scroll-view"
- :show-scrollbar="false"
- ref="u-tabs__wrapper__scroll-view"
- >
- <view
- class="u-tabs__wrapper__nav"
- ref="u-tabs__wrapper__nav"
- >
- <view
- class="u-tabs__wrapper__nav__item"
- v-for="(item, index) in list"
- :key="index"
- @tap="clickHandler(item, index)"
- @longpress="longPressHandler(item,index)"
- :ref="`u-tabs__wrapper__nav__item-${index}`"
- :style="[$u.addStyle(itemStyle), {flex: scrollable ? '' : 1}]"
- :class="[`u-tabs__wrapper__nav__item-${index}`, item.disabled && 'u-tabs__wrapper__nav__item--disabled']"
- >
- <text
- :class="[item.disabled && 'u-tabs__wrapper__nav__item__text--disabled']"
- class="u-tabs__wrapper__nav__item__text"
- :style="[textStyle(index)]"
- >{{ item[keyName] }}</text>
- <u-badge
- :show="!!(item.badge && (item.badge.show || item.badge.isDot || item.badge.value))"
- :isDot="item.badge && item.badge.isDot || propsBadge.isDot"
- :value="item.badge && item.badge.value || propsBadge.value"
- :max="item.badge && item.badge.max || propsBadge.max"
- :type="item.badge && item.badge.type || propsBadge.type"
- :showZero="item.badge && item.badge.showZero || propsBadge.showZero"
- :bgColor="item.badge && item.badge.bgColor || propsBadge.bgColor"
- :color="item.badge && item.badge.color || propsBadge.color"
- :shape="item.badge && item.badge.shape || propsBadge.shape"
- :numberType="item.badge && item.badge.numberType || propsBadge.numberType"
- :inverted="item.badge && item.badge.inverted || propsBadge.inverted"
- customStyle="margin-left: 4px;"
- ></u-badge>
- </view>
-
- <view
- class="u-tabs__wrapper__nav__line"
- ref="u-tabs__wrapper__nav__line"
- :style="[{
- width: $u.addUnit(lineWidth),
- height: $u.addUnit(lineHeight),
- background: lineColor,
- backgroundSize: lineBgSize,
- }]"
- >
-
-
- <view
- class="u-tabs__wrapper__nav__line"
- ref="u-tabs__wrapper__nav__line"
- :style="[{
- width: $u.addUnit(lineWidth),
- transform: `translate(${lineOffsetLeft}px)`,
- transitionDuration: `${firstTime ? 0 : duration}ms`,
- height: $u.addUnit(lineHeight),
- background: lineColor,
- backgroundSize: lineBgSize,
- }]"
- >
-
- </view>
- </view>
- </scroll-view>
- </view>
- <slot name="right" />
- </view>
- </view>
- </template>
- <script>
-
- const animation = uni.requireNativePlugin('animation')
- const dom = uni.requireNativePlugin('dom')
-
- import props from './props.js';
-
- export default {
- name: 'u-tabs',
- mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
- data() {
- return {
- firstTime: true,
- scrollLeft: 0,
- scrollViewWidth: 0,
- lineOffsetLeft: 0,
- tabsRect: {
- left: 0
- },
- innerCurrent: 0,
- moving: false,
- }
- },
- watch: {
- current: {
- immediate: true,
- handler (newValue, oldValue) {
-
- if (newValue !== this.innerCurrent) {
- this.innerCurrent = newValue
- this.$nextTick(() => {
- this.resize()
- })
- }
- }
- },
-
- list() {
- this.$nextTick(() => {
- this.resize()
- })
- }
- },
- computed: {
- textStyle() {
- return index => {
- const style = {}
-
- const customeStyle = index === this.innerCurrent ? uni.$u.addStyle(this.activeStyle) : uni.$u
- .addStyle(
- this.inactiveStyle)
-
- if (this.list[index].disabled) {
- style.color = '#c8c9cc'
- }
- return uni.$u.deepMerge(customeStyle, style)
- }
- },
- propsBadge() {
- return uni.$u.props.badge
- }
- },
- async mounted() {
- this.init()
- },
- methods: {
- setLineLeft() {
- const tabItem = this.list[this.innerCurrent];
- if (!tabItem) {
- return;
- }
-
- let lineOffsetLeft = this.list
- .slice(0, this.innerCurrent)
- .reduce((total, curr) => total + curr.rect.width, 0);
-
- const lineWidth = uni.$u.getPx(this.lineWidth);
- this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - lineWidth) / 2
-
-
- this.animation(this.lineOffsetLeft, this.firstTime ? 0 : parseInt(this.duration))
-
-
-
- if (this.firstTime) {
- setTimeout(() => {
- this.firstTime = false
- }, 10);
- }
- },
-
- animation(x, duration = 0) {
-
- const ref = this.$refs['u-tabs__wrapper__nav__line']
- animation.transition(ref, {
- styles: {
- transform: `translateX(${x}px)`
- },
- duration
- })
-
- },
-
- clickHandler(item, index) {
-
- this.$emit('click', {
- ...item,
- index
- })
-
- if (item.disabled) return
- this.innerCurrent = index
- this.resize()
- this.$emit('change', {
- ...item,
- index
- })
- },
-
- longPressHandler(item, index) {
- this.$emit('longPress', {
- ...item,
- index
- })
- },
- init() {
- uni.$u.sleep().then(() => {
- this.resize()
- })
- },
- setScrollLeft() {
-
- const tabRect = this.list[this.innerCurrent]
-
- const offsetLeft = this.list
- .slice(0, this.innerCurrent)
- .reduce((total, curr) => {
- return total + curr.rect.width
- }, 0)
-
- const windowWidth = uni.$u.sys().windowWidth
-
- let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect
- .right) / 2 + this.tabsRect.left / 2
-
- scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width)
- this.scrollLeft = Math.max(0, scrollLeft)
- },
-
- resize() {
-
- if(this.list.length === 0) {
- return
- }
- Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(([tabsRect, itemRect = []]) => {
- this.tabsRect = tabsRect
- this.scrollViewWidth = 0
- itemRect.map((item, index) => {
-
- this.scrollViewWidth += item.width
-
- this.list[index].rect = item
- })
-
- this.setLineLeft()
- this.setScrollLeft()
- })
- },
-
- getTabsRect() {
- return new Promise(resolve => {
- this.queryRect('u-tabs__wrapper__scroll-view').then(size => resolve(size))
- })
- },
-
- getAllItemRect() {
- return new Promise(resolve => {
- const promiseAllArr = this.list.map((item, index) => this.queryRect(
- `u-tabs__wrapper__nav__item-${index}`, true))
- Promise.all(promiseAllArr).then(sizes => resolve(sizes))
- })
- },
-
- queryRect(el, item) {
-
-
-
- return new Promise(resolve => {
- this.$uGetRect(`.${el}`).then(size => {
- resolve(size)
- })
- })
-
-
-
-
- return new Promise(resolve => {
- dom.getComponentRect(item ? this.$refs[el][0] : this.$refs[el], res => {
- resolve(res.size)
- })
- })
-
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-tabs {
- &__wrapper {
- @include flex;
- align-items: center;
- &__scroll-view-wrapper {
- flex: 1;
- /* #ifndef APP-NVUE */
- overflow: auto hidden;
- /* #endif */
- }
- &__scroll-view {
- @include flex;
- flex: 1;
- }
- &__nav {
- @include flex;
- position: relative;
- &__item {
- padding: 0 11px;
- @include flex;
- align-items: center;
- justify-content: center;
- &--disabled {
- /* #ifndef APP-NVUE */
- cursor: not-allowed;
- /* #endif */
- }
- &__text {
- font-size: 15px;
- color: $u-content-color;
- &--disabled {
- color: $u-disabled-color !important;
- }
- }
- }
- &__line {
- height: 3px;
- background: $u-primary;
- width: 30px;
- position: absolute;
- bottom: 2px;
- border-radius: 100px;
- transition-property: transform;
- transition-duration: 300ms;
- }
- }
- }
- }
- </style>
|