123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <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
- enable-flex
- class="u-tabs__wrapper__scroll-view white-space"
- :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)"
- :ref="`u-tabs__wrapper__nav__item-${index}`"
- :style="[addStyle(itemStyle), { flex: scrollable ? '' : 1 }]"
- :class="[
- `u-tabs__wrapper__nav__item-${index}`,
- item.disabled && 'u-tabs__wrapper__nav__item--disabled',
- ]"
- >
- <uni-badge
- class="uni-badge-left-margin"
- :text="item.num"
- absolute="rightTop"
- size="small"
- v-if="badge && item.isShow"
- >
- <text
- :class="[item.disabled && 'u-tabs__wrapper__nav__item__text--disabled']"
- class="u-tabs__wrapper__nav__item__text"
- :style="[textStyle(index)]"
- >{{ item[keyName] }}</text
- >
- </uni-badge>
- <text
- :class="[item.disabled && 'u-tabs__wrapper__nav__item__text--disabled']"
- class="u-tabs__wrapper__nav__item__text"
- :style="[textStyle(index)]"
- v-else
- >{{ item[keyName] }}</text
-
- >
- </view>
-
- <view
- class="u-tabs__wrapper__nav__line"
- ref="u-tabs__wrapper__nav__line"
- :style="[
- {
- width: addUnit(lineWidth),
- height: addUnit(lineHeight),
- background: lineColor ? lineColor : 'var(--ui-BG-Main)',
- backgroundSize: lineBgSize,
- },
- ]"
- ></view>
-
-
- <view
- class="u-tabs__wrapper__nav__line"
- ref="u-tabs__wrapper__nav__line"
- :style="[
- {
- width: addUnit(lineWidth),
- transform: `translate(${lineOffsetLeft}px)`,
- transitionDuration: `${firstTime ? 0 : duration}ms`,
- height: addUnit(lineHeight),
- background: lineColor ? lineColor : 'var(--ui-BG-Main)',
- backgroundSize: lineBgSize,
- },
- ]"
- >
- </view>
-
- </view>
- </scroll-view>
- </view>
- <slot name="right" />
- </view>
- </view>
- </template>
- <script>
- import { deepMerge, addStyle, addUnit, sleep, getPx, sys } from '@/sheep/helper';
-
- const animation = uni.requireNativePlugin('animation');
- const dom = uni.requireNativePlugin('dom');
-
-
- export default {
- name: 'su-tabs',
- data() {
- return {
- addStyle,
- addUnit,
- firstTime: true,
- scrollLeft: 0,
- scrollViewWidth: 0,
- lineOffsetLeft: 0,
- tabsRect: {
- left: 0,
- },
- innerCurrent: 0,
- moving: false,
- };
- },
- props: {
-
- duration: {
- type: Number,
- default: 300,
- },
-
- list: {
- type: Array,
- default: [],
- },
- badge:{
- type:Boolean,
- default:false
- },
-
- lineColor: {
- type: String,
- default: '',
- },
-
- activeStyle: {
- type: [String, Object],
- default() {
- return {
- color: '#303133',
- };
- },
- },
-
- inactiveStyle: {
- type: [String, Object],
- default() {
- return {
- color: '#606266',
- };
- },
- },
-
- lineWidth: {
- type: [String, Number],
- default: 20,
- },
-
- lineHeight: {
- type: [String, Number],
- default: 3,
- },
-
- lineBgSize: {
- type: String,
- default: 'cover',
- },
-
- itemStyle: {
- type: [String, Object],
- default() {
- return {
- height: '44px',
- };
- },
- },
-
- scrollable: {
- type: Boolean,
- default: true,
- },
-
- current: {
- type: [Number, String],
- default: 0,
- },
-
- keyName: {
- type: String,
- default: 'name',
- },
- },
- 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 ? addStyle(this.activeStyle) : addStyle(this.inactiveStyle);
-
- if (this.list[index].disabled) {
- style.color = '#c8c9cc';
- }
- return deepMerge(customeStyle, style);
- };
- },
- },
- async mounted() {
- this.init();
- },
- methods: {
- $uGetRect(selector, all) {
- return new Promise((resolve) => {
- uni.createSelectorQuery()
- .in(this)
- [all ? 'selectAll' : 'select'](selector)
- .boundingClientRect((rect) => {
- if (all && Array.isArray(rect) && rect.length) {
- resolve(rect);
- }
- if (!all && rect) {
- resolve(rect);
- }
- })
- .exec();
- });
- },
- 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 = 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,
- });
- },
- init() {
- 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 = 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>
- .u-tabs {
- background: #fff;
- border-bottom: 2rpx solid #eee;
- &__wrapper {
- @include flex;
- align-items: center;
- &__scroll-view-wrapper {
- flex: 1;
- /* #ifndef APP-NVUE */
- overflow: auto hidden;
- /* #endif */
- }
- &__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: 14px;
- color: #606266;
- white-space: nowrap !important;
- &--disabled {
- color: #c8c9cc !important;
- }
- }
- }
- &__line {
- height: 3px;
- background: #3c9cff;
- width: 30px;
- position: absolute;
- bottom: 2px;
- border-radius: 100px;
- transition-property: transform;
- transition-duration: 300ms;
- }
- }
- }
- }
- </style>
|