mes_mobile/components/customTabBar/index.vue

192 lines
4.1 KiB
Vue
Raw Normal View History

2024-01-16 16:57:49 +08:00
<template>
<view class="tabbar-container">
<block>
<!-- 针对中间的导航栏 通过true来判断控制类名和样式 -->
<view class="tabbar-item" v-for="(item, index) in tabbarList" :class="[item.centerItem ? 'center-item' : '']" @click="changeItem(item)" :key="index">
<view class="item-top">
2024-04-23 11:48:55 +08:00
<image class="img" :src="currentItem == item.id ? item.selectIcon:item.icon"></image>
2024-01-16 16:57:49 +08:00
</view>
<!-- 通过三元判断控制类名 达到控制选中和未选中的样式 -->
<view class="item-bottom" :class="[currentItem == item.id ? 'item-active' : '']">
<text>{{ item.text }}</text>
</view>
</view>
</block>
</view>
</template>
<script>
// 组件的书写符合easycom规范 无需引入直接使用
export default {
props: {
currentPage: {
type: Number,
default: 0
}
},
data () {
return {
currentItem: 0,
tabbarList: [
{
id: 0,
path: '/pages/index/index',
2024-03-13 16:07:11 +08:00
icon: '/static/indexA.png',
selectIcon: '/static/indexB.png',
2024-01-16 16:57:49 +08:00
text: '首页',
centerItem: false
},
{
id: 1,
2024-04-23 11:48:55 +08:00
path: '',
icon: '/static/sweep.png',
selectIcon: '',
2024-01-16 18:52:23 +08:00
text: '扫一扫',
2024-01-16 16:57:49 +08:00
// 通过类名class控制样式大小
centerItem: true
},
{
id: 2,
2024-01-16 18:52:23 +08:00
path: '/pages/person/person',
2024-03-13 16:07:11 +08:00
icon: '/static/personB.png',
selectIcon: '/static/personA.png',
2024-01-16 16:57:49 +08:00
text: '我的',
centerItem: false
}
]
}
},
mounted () {
this.currentItem = this.currentPage
},
methods: {
2024-04-23 11:48:55 +08:00
changeItem (item) {
if(item.centerItem){
this.scanQRCode();
return
}
uni.switchTab({
url: item.path
})
},
scanQRCode(){
uni.scanCode({
success: (res) => {
console.log('扫码结果:' + res.result);
uni.showModal({
title: '扫码结果',
content: res.result,
showCancel: false
});
},
fail: (err) => {
console.error('扫码失败:' + err);
uni.showToast({
title: '扫码失败',
icon: 'none'
});
}
});
}
2024-01-16 16:57:49 +08:00
}
};
</script>
2024-01-16 18:52:23 +08:00
<style lang="scss" scope>
2024-01-16 16:57:49 +08:00
view {
padding: 0;
margin: 0;
box-sizing: border-box;
}
2024-04-23 11:48:55 +08:00
.tabbar-container::before{
content: '';
position: absolute;
left: 10%;
top: 0;
z-index: 10;
height: 1px;
width: 80%;
background-image: linear-gradient(90deg, $uni-border-color-default, $uni-color-primary 50%, $uni-border-color-default);
background-size: 100% 1px;
background-repeat:no-repeat;
z-index: 1;
}
2024-01-16 16:57:49 +08:00
.tabbar-container {
position: fixed;
bottom: 0rpx;
left: 0rpx;
width: 100%;
height: 110rpx;
display: flex;
align-items: center;
padding: 5rpx 0;
2024-04-23 11:48:55 +08:00
color: $uni-text-color-grey;
background: $uni-border-color-default;
box-shadow: 0 2px 4px $uni-border-shadow-color-default;
2024-01-16 16:57:49 +08:00
/* 针对tabbar的统一处理 */
.tabbar-item {
width: 33.33%;
height: 100rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
.item-top {
width: 70rpx;
height: 70rpx;
padding: 10rpx;
image {
width: 100%;
height: 100%;
}
}
// 未被选中的导航栏字体
.item-bottom {
2024-01-16 18:52:23 +08:00
font-size: 12px;
2024-01-16 16:57:49 +08:00
width: 100%;
}
// 被选中的导航栏字体
.item-active {
2024-04-23 11:48:55 +08:00
color: $uni-text-color-inverse;
2024-01-16 16:57:49 +08:00
}
}
// 最中间的tabbar导航栏
.center-item {
display: block;
position: relative;
2024-04-23 11:48:55 +08:00
z-index: 20;
2024-01-16 16:57:49 +08:00
.item-top {
flex-shrink: 0;
width: 100rpx;
height: 100rpx;
position: absolute;
2024-04-23 11:48:55 +08:00
top: -60rpx;
2024-01-16 16:57:49 +08:00
left: calc(50% - 50rpx);
border-radius: 50%;
2024-04-23 11:48:55 +08:00
box-shadow: 0 2px 4px $uni-border-shadow-color-default;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid $uni-text-color-inverse;
.img{
width: 100%;
height: 100%;
border-radius: 50%;
}
2024-01-16 16:57:49 +08:00
}
.item-bottom {
position: absolute;
bottom: 5rpx;
}
.item-active {
position: absolute;
bottom: 5rpx;
2024-04-23 11:48:55 +08:00
color: $uni-text-color-inverse;
2024-01-16 16:57:49 +08:00
}
}
}
</style>