H5版基于uni-app实现自定义底部tabbar效果(支持底部图标选中时的渐变效果)

首先声明部分代码转载自 uniapp自定义tabbar导航解析

我自己又在基础上添加了一些功能,如实现tab的数字角标、tab页面跳转、tabbar选中状态的图标高亮渐变。

效果如下:
在这里插入图片描述

tabbar已经抽离成组件了。

tabbar组件代码

<template>
  <view class="tab-bar-container">
    <block v-for="(item, index) in tabList" :key="index">
      <view class="navigator" :class="[currentTabIndex === index ? 'on' : '']" @tap="switchTab(item, index)">
        <view class="icon">
          <text class="icon-style iconfont" :class="[currentTabIndex === index ? [item.iconActive, 'height-light'] : item.icon]" :style="[currentTabIndex === index ? '' : { color: color }]"></text>
        </view>
        <view class="text" :style="[currentTabIndex === index ? { color: tintColor } : { color: color }]">{{ item.text }}</view>
        <!-- 消息数字角标 -->
        <uni-badge class="num-badge" v-if="item.badgeNum" size="small" type="error" :text="item.badgeNum" @click="handleBadge(index)"></uni-badge>
      </view>
    </block>
  </view>
</template>

<script>
import uniBadge from "@/components/uni-badge/uni-badge.vue"
export default {
  components: {
    uniBadge
  },
  props: {
    current: { 
      type: [Number, String],
      default: 0
    },
    backgroundColor: {
      type: String,
      default: '#fbfbfb'
    },
    color: {
      type: String, 
      default: '#999'
    },
    tintColor: { 
      type: String, 
      default: '#3768FC'
    }
  },
  data() {
    return {
      // 数字角标
      /*
       * icon 默认图标
       * iconActive 选中图标
       * text 文字信息
       * path 页面路径
       */
      tabList: [
        {
          icon: 'icon-home',
          iconActive: 'icon-home-hl',
          text: '首页',
          path: '../index/index'
        },
        {
          icon: 'icon-tidings',
          iconActive: 'icon-tidings-hl',
          text: '消息',
          path: '../message/message',
          badgeNum: 2
        },
        {
          icon: 'icon-mailList',
          iconActive: 'icon-mailList-hl',
          text: '通讯录',
          path: '../contacts/contacts'
        },
        {
          icon: 'icon-workPlatform',
          iconActive: 'icon-workPlatform-hl',
          text: '工作台',
          path: '../workPlatform/workPlatform'
        },
        {
          icon: 'icon-user',
          iconActive: 'icon-user-hl',
          text: '我的',
          path: '../my/my'
        }
      ],
      currentTabIndex: this.current
    };
  },
  methods: {
    // 切换页面
    switchTab(tab, index) {
      this.currentTabIndex = index;
      
      if (this.currentTabIndex === index && tab.badgeNum) {
        this.$nextTick(function(){
          this.$set(this.tabList[index], 'badgeNum', 0)
        })
      }
      
      this.$emit('click', {
        path: tab.path,
        index
      });
    },
    // 点击数字角标
    handleBadge(index) {
      // if (this.currentTabIndex === index) {
      //   this.badgeNum = 0
      // }
    }
  }
};
</script>

<style scoped lang="scss">
.tab-bar-container {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 999;
  width: 100%;
  height: 50px;
  box-shadow: 0 0 6px 0 rgba(0,0,0,0.12);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 50rpx;
  box-sizing: border-box;
  background-color: #fff;
  
  
  .navigator {
    height: 100%;
    width: 80rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    
    .icon-style {
      font-size: 20px;
    }
    
    /* 选中图标的高亮样式 */
    .height-light {
      background: linear-gradient(137deg, #507EFF 26%, #2A56F7 71%, #193EEE 100%);
      -webkit-background-clip: text;
      color: transparent;
    }
    
    .text {
      font-size: 24rpx;
    }
    
    .num-badge {
      position: absolute;
      top: 0;
      right: -26rpx;
    }
  }
}
</style>

tabbar组件的使用

<template>
  <view class="container">
    <text class="title">{{ name }}</text>

    <!-- 自定义tabbar -->
    <tab-bar :current="currentTabIndex" backgroundColor="#FFFFFF" color="#999" tintColor="#3768FC" @click="tabClick"></tab-bar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      name: '消息',
      // 当前tab高亮索引
      currentTabIndex: 1
    };
  },
  methods: {
    // 进行tab页跳转
    tabClick(obj) {
      this.currentTabIndex = obj.index
      uni.redirectTo({
         url: obj.path
      })
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.title {
  font-size: 24rpx;
  color: #8f8f94;
}
</style>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app中,可以通过自定义底部tabbar组件来实现切换动画效果。首先,在tab-bar组件中,可以使用uni的tabbar组件来实现底部tab切换功能。在tab-bar组件中,可以通过设置backgroundColor属性,color属性和tintColor属性来自定义底部tabbar的背景颜色,字体颜色和选中状态的图标高亮颜色。接着,在methods中的tabClick方法中,可以使用uni的redirectTo方法来进行tab页的跳转,并将当前tab的高亮索引更新为点击的tab的索引。这样,当点击底部tab,页面会实现切换动画效果并跳转到对应的tab页。同,可以根据自己的需求添加一些额外的功能,如实现tab的数字角标和tab页面的跳转。例如,在基础上可以添加tab的数字角标和tab页面跳转功能。为了保证底部tabbar的高度一致,可以在全局的数据中设置一个tabbarHeight属性,并在tab-bar组件中引用该属性。这样,就可以实现定义底部tabbar切换动画效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [H5基于uni-app实现定义底部tabbar效果支持底部图标选中渐变效果)](https://blog.csdn.net/CherryLee_1210/article/details/106281571)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [uni-app定义底部导航栏的实现](https://blog.csdn.net/weixin_32786041/article/details/111911595)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值