123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <template>
- <view v-if="show" class="u-tabbar" @touchmove.stop.prevent="() => {}">
- <view class="u-tabbar__content safe-area-inset-bottom" :style="{
- height: $u.addUnit(height),
- backgroundColor: bgColor,
- }" :class="{
- 'u-border-top': borderTop
- }">
- <view class="u-tabbar__content__item" v-for="(item, index) in list" :key="index" :class="{
- 'u-tabbar__content__circle': midButton &&item.midButton
- }" @tap.stop="clickHandler(index)" :style="{
- backgroundColor: bgColor
- }">
- <view :class="[
- midButton && item.midButton ? 'u-tabbar__content__circle__button' : 'u-tabbar__content__item__button'
- ]">
- <u-icon
- :size="midButton && item.midButton ? midButtonSize : iconSize"
- :name="elIconPath(index)"
- img-mode="scaleToFill"
- :color="elColor(index)"
- :custom-prefix="item.customIcon ? 'custom-icon' : 'uicon'"
- ></u-icon>
- <u-badge :count="item.count" :is-dot="item.isDot"
- v-if="item.count || item.isDot"
- :offset="[-2, getOffsetRight(item.count, item.isDot)]"
- ></u-badge>
- </view>
- <view class="u-tabbar__content__item__text" :style="{
- color: elColor(index)
- }">
- <text class="u-line-1">{{item.text}}</text>
- </view>
- </view>
- <view v-if="midButton" class="u-tabbar__content__circle__border" :class="{
- 'u-border': borderTop,
- }" :style="{
- backgroundColor: bgColor,
- left: midButtonLeft
- }">
- </view>
- </view>
-
- <view class="u-fixed-placeholder safe-area-inset-bottom" :style="{
- height: `calc(${$u.addUnit(height)} + ${midButton ? 48 : 0}rpx)`,
- }"></view>
- </view>
- </template>
- <script>
- export default {
- props: {
-
- show: {
- type: Boolean,
- default: true
- },
-
- value: {
- type: [String, Number],
- default: 0
- },
-
- bgColor: {
- type: String,
- default: '#ffffff'
- },
-
- height: {
- type: [String, Number],
- default: '50px'
- },
-
- iconSize: {
- type: [String, Number],
- default: 40
- },
-
- midButtonSize: {
- type: [String, Number],
- default: 90
- },
-
- activeColor: {
- type: String,
- default: '#303133'
- },
-
- inactiveColor: {
- type: String,
- default: '#606266'
- },
-
- midButton: {
- type: Boolean,
- default: false
- },
-
- list: {
- type: Array,
- default () {
- return []
- }
- },
-
- beforeSwitch: {
- type: Function,
- default: null
- },
-
- borderTop: {
- type: Boolean,
- default: true
- },
-
- hideTabBar: {
- type: Boolean,
- default: true
- },
- },
- data() {
- return {
-
- midButtonLeft: '50%',
- pageUrl: '',
- }
- },
- created() {
-
- if(this.hideTabBar) uni.hideTabBar();
-
- let pages = getCurrentPages();
-
- this.pageUrl = pages[pages.length - 1].route;
- },
- computed: {
- elIconPath() {
- return (index) => {
-
-
-
- let pagePath = this.list[index].pagePath;
-
-
- if(pagePath) {
- if(pagePath == this.pageUrl || pagePath == '/' + this.pageUrl) {
- return this.list[index].selectedIconPath;
- } else {
- return this.list[index].iconPath;
- }
- } else {
-
- return index == this.value ? this.list[index].selectedIconPath : this.list[index].iconPath
- }
- }
- },
- elColor() {
- return (index) => {
-
- let pagePath = this.list[index].pagePath;
- if(pagePath) {
- if(pagePath == this.pageUrl || pagePath == '/' + this.pageUrl) return this.activeColor;
- else return this.inactiveColor;
- } else {
- return index == this.value ? this.activeColor : this.inactiveColor;
- }
- }
- }
- },
- mounted() {
- this.midButton && this.getMidButtonLeft();
- },
- methods: {
- async clickHandler(index) {
- if(this.beforeSwitch && typeof(this.beforeSwitch) === 'function') {
-
-
-
- let beforeSwitch = this.beforeSwitch.bind(this.$u.$parent.call(this))(index);
-
- if (!!beforeSwitch && typeof beforeSwitch.then === 'function') {
- await beforeSwitch.then(res => {
-
- this.switchTab(index);
- }).catch(err => {
- })
- } else if(beforeSwitch === true) {
-
- this.switchTab(index);
- }
- } else {
- this.switchTab(index);
- }
- },
-
- switchTab(index) {
-
- this.$emit('change', index);
-
- if(this.list[index].pagePath) {
- uni.switchTab({
- url: this.list[index].pagePath
- })
- } else {
-
-
- this.$emit('input', index);
- }
- },
-
- getOffsetRight(count, isDot) {
-
- if(isDot) {
- return -20;
- } else if(count > 9) {
- return -40;
- } else {
- return -30;
- }
- },
-
- getMidButtonLeft() {
- let windowWidth = this.$u.sys().windowWidth;
-
- this.midButtonLeft = (windowWidth / 2) + 'px';
- }
- }
- }
- </script>
- <style scoped lang="scss">
- @import "../../libs/css/style.components.scss";
- .u-fixed-placeholder {
- /* #ifndef APP-NVUE */
- box-sizing: content-box;
- /* #endif */
- }
- .u-tabbar {
- &__content {
- @include vue-flex;
- align-items: center;
- position: relative;
- position: fixed;
- bottom: 0;
- left: 0;
- width: 100%;
- z-index: 998;
- /* #ifndef APP-NVUE */
- box-sizing: content-box;
- /* #endif */
- &__circle__border {
- border-radius: 100%;
- width: 110rpx;
- height: 110rpx;
- top: -48rpx;
- position: absolute;
- z-index: 4;
- background-color: #ffffff;
- // 由于安卓的无能,导致只有3个tabbar item时,此css计算方式有误差
- // 故使用js计算的形式来定位,此处不注释,是因为js计算有延后,避免出现位置闪动
- left: 50%;
- transform: translateX(-50%);
- &:after {
- border-radius: 100px;
- }
- }
- &__item {
- flex: 1;
- justify-content: center;
- height: 100%;
- padding: 12rpx 0;
- @include vue-flex;
- flex-direction: column;
- align-items: center;
- position: relative;
- &__button {
- position: absolute;
- top: 14rpx;
- left: 50%;
- transform: translateX(-50%);
- }
- &__text {
- color: $u-content-color;
- font-size: 26rpx;
- line-height: 28rpx;
- position: absolute;
- bottom: 14rpx;
- left: 50%;
- transform: translateX(-50%);
- width: 100%;
- text-align: center;
- }
- }
- &__circle {
- position: relative;
- @include vue-flex;
- flex-direction: column;
- justify-content: space-between;
- z-index: 10;
- /* #ifndef APP-NVUE */
- height: calc(100% - 1px);
- /* #endif */
- &__button {
- width: 90rpx;
- height: 90rpx;
- border-radius: 100%;
- @include vue-flex;
- justify-content: center;
- align-items: center;
- position: absolute;
- background-color: #ffffff;
- top: -40rpx;
- left: 50%;
- z-index: 6;
- transform: translateX(-50%);
- }
- }
- }
- }
- </style>
|