123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <template>
- <view class="tn-tabs-class tn-tabs" :class="[backgroundColorClass]" :style="{backgroundColor: backgroundColorStyle, marginTop: $t.string.getLengthUnitValue(top, 'px')}">
-
-
- <view :id="id">
- <scroll-view scroll-x class="tn-tabs__scroll-view" :scroll-left="scrollLeft" scroll-with-animation>
- <view class="tn-tabs__scroll-view__box" :class="{'tn-tabs__scroll-view--flex': !isScroll}">
-
- <view
- v-for="(item, index) in list"
- :key="index"
- :id="'tn-tabs__scroll-view__item-' + index"
- class="tn-tabs__scroll-view__item tn-text-ellipsis"
- :style="[tabItemStyle(index)]"
- @tap="clickTab(index)"
- >
- <tn-badge v-if="item[count] || item['count']" backgroundColor="tn-bg-red" fontColor="#FFFFFF" :absolute="true" :top="badgeOffset[0] || 0" :right="badgeOffset[1] || 0">{{ item[count] || item['count']}}</tn-badge>
- {{ item[name] || item['name'] }}
- </view>
-
-
- <view v-if="showBar" class="tn-tabs__bar" :style="[tabBarStyle]"></view>
- </view>
- </scroll-view>
- </view>
- </view>
- </template>
- <script>
- import componentsColor from '../../libs/mixin/components_color.js'
- export default {
- mixins: [componentsColor],
- name: 'tn-tabs',
- props: {
-
- list: {
- type: Array,
- default() {
- return []
- }
- },
-
- name: {
- type: String,
- default: 'name'
- },
-
- count: {
- type: String,
- default: 'count'
- },
-
- current: {
- type: Number,
- default: 0
- },
-
- isScroll: {
- type: Boolean,
- default: true
- },
-
- height: {
- type: Number,
- default: 80
- },
-
- top: {
- type: Number,
- default: 0
- },
-
- itemWidth: {
- type: [String, Number],
- default: 'auto'
- },
-
- duration: {
- type: Number,
- default: 0.3
- },
-
- activeColor: {
- type: String,
- default: '#01BEFF'
- },
-
- inactiveColor: {
- type: String,
- default: '#080808'
- },
-
- activeItemStyle: {
- type: Object,
- default() {
- return {}
- }
- },
-
- showBar: {
- type: Boolean,
- default: true
- },
-
- barWidth: {
- type: Number,
- default: 40
- },
-
- barHeight: {
- type: Number,
- default: 6
- },
-
- barStyle: {
- type: Object,
- default() {
- return {}
- }
- },
-
- gutter: {
- type: Number,
- default: 30
- },
-
- badgeOffset: {
- type: Array,
- default() {
- return [20, 22]
- }
- },
-
- bold: {
- type: Boolean,
- default: false
- }
- },
- computed: {
-
- tabBarStyle() {
- let style = {
- width: this.$t.string.getLengthUnitValue(this.barWidth),
- height: this.$t.string.getLengthUnitValue(this.barHeight),
- borderRadius: `${this.barHeight / 2}rpx`,
- backgroundColor: this.activeColor,
- opacity: this.barMoveFirst ? 0 : 1,
- transform: `translate(${this.scrollBarLeft}px, -100%)`,
- transitionDuration: this.barMoveFirst ? '0s' : `${this.duration}s`
- }
- Object.assign(style, this.barStyle)
- return style
- },
-
- tabItemStyle() {
- return index => {
- let style = {
- width: this.$t.string.getLengthUnitValue(this.itemWidth),
- height: this.$t.string.getLengthUnitValue(this.height),
- lineHeight: this.$t.string.getLengthUnitValue(this.height),
- fontSize: this.fontSizeStyle || '28rpx',
- padding: this.isScroll ? `0 ${this.gutter}rpx` : '',
- flex: this.isScroll ? 'auto' : '1',
- transitionDuration: `${this.duration}s`
- }
- if (index === this.currentIndex) {
- if (this.bold) {
- style.fontWeight = 'bold'
- }
- style.color = this.activeColor
- Object.assign(style, this.activeItemStyle)
- } else {
- style.color = this.inactiveColor
- }
- return style
- }
- }
- },
- data() {
- return {
-
- id: this.$t.uuid(),
-
- scrollLeft: 0,
-
- tabQueryInfo: [],
-
- componentWidth: 0,
-
- scrollBarLeft: 0,
-
- componentLeft: 0,
-
- currentIndex: this.current,
-
- barMoveFirst: true
- }
- },
- watch: {
-
- list(newValue, oldValue) {
-
- if (newValue.length !== oldValue.length) this.currentIndex = 0
- this.$nextTick(() => {
- this.init()
- })
- },
- current: {
- handler(val) {
- this.$nextTick(() => {
- this.currentIndex = val
- this.scrollByIndex()
- })
- },
- immediate: true
- }
- },
- mounted() {
- this.init()
- },
- methods: {
-
- async init() {
-
- let tabRect = await this._tGetRect('#' + this.id)
-
- this.componentLeft = tabRect.left
- this.componentWidth = tabRect.width
- this.getTabRect()
- },
-
- clickTab(index) {
- if (index === this.currentIndex) return
- this.$emit('change', index)
- },
-
- getTabRect() {
- let query = uni.createSelectorQuery().in(this)
-
- for (let i = 0; i < this.list.length; i++) {
- query.select(`#tn-tabs__scroll-view__item-${i}`).fields({
- size: true,
- rect: true
- })
- }
- query.exec((res) => {
- this.tabQueryInfo = res
-
- this.scrollByIndex()
- })
- },
-
- scrollByIndex() {
-
- let tabInfo = this.tabQueryInfo[this.currentIndex]
- if (!tabInfo) return
-
-
- let tabWidth = tabInfo.width
-
- let offsetLeft = tabInfo.left - this.componentLeft
-
- let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2
- this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft
-
-
- let left = tabInfo.left + tabInfo.width / 2 - this.componentLeft
-
-
- this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2
-
-
- if (this.barMoveFirst) {
- setTimeout(() => {
- this.barMoveFirst = false
- }, 100)
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- /* #ifndef APP-NVUE */
- ::-webkit-scrollbar {
- display: none;
- width: 0 !important;
- height: 0 !important;
- -webkit-appearance: none;
- background: transparent;
- }
- /* #endif */
-
- /* #ifdef H5 */
- // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
- scroll-view ::v-deep ::-webkit-scrollbar {
- display: none;
- width: 0 !important;
- height: 0 !important;
- -webkit-appearance: none;
- background: transparent;
- }
- /* #endif */
-
- .tn-tabs {
- &__scroll-view {
- position: relative;
- width: 100%;
- white-space: nowrap;
-
- &__box {
- position: relative;
- /* #ifdef MP-TOUTIAO */
- white-space: nowrap;
- /* #endif */
- }
-
- &__item {
- position: relative;
- /* #ifndef APP-NVUE */
- display: inline-block;
- /* #endif */
- text-align: center;
- transition-property: background-color, color;
- }
-
- &--flex {
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- }
- }
-
- &__bar {
- position: absolute;
- bottom: 0;
- }
- }
- </style>
|