vue+vant 的h5自定义tabbar栏,下滑隐藏tabbar

4 篇文章 0 订阅

首先在src目录下新建components文件夹,在components文件夹下建立自己的组件文件,在tabbar里建index.vue文件

tabbar,index.vue的代码如下

<template>
  <div class="tabBar" v-if="show">
    <van-tabbar
      v-model="currentIndex"
      active-color="#1989fa"
      :border="false"
      @change="isActives"
    >
      <van-tabbar-item
        v-for="item in tabBarList"
        :to="item.path"
        :key="item.id"
      >
        <span>{{ item.tabBarTxt }}</span>
        <img
          slot="icon"
          slot-scope="props"
          :src="props.active ? item.active : item.inactive"
          :class="item.tabBarTxt ? 'default_img' : 'tabBar_img'"
        />
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      active: 0,
      currentIndex: this.isActive, //currentIndex接收父组件传来的activeIndex值,,v-model绑定的必须有值,否则会报错
      tabBarList: [
        {
          active: require("@/assets/icon/h-active.png"),//选中的图片
          inactive: require("@/assets/icon/h.png"),//未选中的图片
          tabBarTxt: "首页",
          path: "/Home"
        },
        {
          active: require("@/assets/icon/n-active.png"),
          inactive: require("@/assets/icon/n.png"),
          tabBarTxt: "发现",
          path: "/Park"
        },
        {
          active: require("@/assets/icon/m-acitve.png"),
          inactive: require("@/assets/icon/m.png"),
          tabBarTxt: "我的",
          path: "/Mine"
        }
      ],
      url: ""
    };
  },
  // 接受父组件的值
  props: {
    show: Boolean,
    isActive: Number
  },
  watch: {},
  created() {},
  methods: {
    /**
     * 向父组件值通知
     */
    isActives(value) {
      this.$emit("isChecked", value);
    }
  }
};
</script>
<style lang="scss">
.van-tabbar {
  height: 12%;

  .van-tabbar-item {
    .van-tabbar-item__icon {
      img {
        height: 34px !important;
      }
    }
  }
}
</style>

然后在App.vue中引入组件并监听组件实现下滑隐藏tabbar

App.vue代码如下

<template>
  <div id="app" style="min-height:100vh;width:100%;height:100%">
    <!-- 缓存Park页面 -->
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
    <tabBar
      :show="isShow"
      :isActive="active"
      @isChecked="isActives"
      v-if="tabBarShow"
    ></tabBar>
  </div>
</template>
<script>

import tabBar from "@/components/tabBar/index.vue";
export default {
  data() {
    return {
      // 监听tabbar
      headerShow: "",
      isShow: "", //隐藏tabbar
      title: "",
      code: "",
      tabBarShow: "",
      active: 0
    };
  },
  components: { tabBar },
  watch: {
    // 使用vue的路由钩子监听页面header以及tabBar的显示与隐藏
    $route(to, from) {
      //判断tabbar是否显示
      if (to.name == "Home" || to.name == "Park" || to.name == "Mine") {
        this.isShow = true;
        this.tabBarShow = true;
        if (to.name == "Home") {
          this.active = 0;
        } else if (to.name == "Park") {
          this.active = 1;
        } else if (to.name == "Mine") {
          this.active = 2;
        }
      } else {
        this.isShow = false;
        this.tabBarShow = false;
      }
    }
  },
  created() {},
  mounted() {
    window.addEventListener("scroll", this.scrollHandle); //绑定页面滚动事件
  },
  methods: {
    // 监听屏幕高度
    scrollHandle(e) {
      let top = e.srcElement.scrollingElement.scrollTop; // 获取页面滚动高度
      //隐藏tabbar
      if (top > 0) this.isShow = false;
      else {
        this.isShow = true;
      }
    },
    isActives(type) {
      this.active = type;
    }
  }
};
</script>

<style lang="scss">
@import "@/style/mixin.scss";
html,
body,
#app {
  height: 100%;
}
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于Vue 3和Vant移动端TabBar的使用,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Vue 3和Vant。你可以使用以下命令来安装它们: ```bash npm install vue@next vant ``` 2. 在你的Vue项目中,创建一个新的组件文件,例如`TabBar.vue`。 3. 在`TabBar.vue`文件中,引入VueVant的相关组件和样式: ```javascript <template> <div> <van-tabbar v-model="active"> <van-tabbar-item icon="home-o" to="/"> 首页 </van-tabbar-item> <van-tabbar-item icon="search" to="/search"> 搜索 </van-tabbar-item> <van-tabbar-item icon="star-o" to="/favorites"> 收藏 </van-tabbar-item> <van-tabbar-item icon="contact" to="/profile"> 个人中心 </van-tabbar-item> </van-tabbar> </div> </template> <script> import { Tabbar, TabbarItem } from 'vant'; export default { components: { [Tabbar.name]: Tabbar, [TabbarItem.name]: TabbarItem, }, data() { return { active: '/', }; }, }; </script> <style> /* 这里可以添加自定义样式 */ </style> ``` 4. 在你的主应用组件中,例如`App.vue`,使用`TabBar`组件: ```html <template> <div id="app"> <!-- 其他内容 --> <TabBar /> </div> </template> <script> import TabBar from './components/TabBar.vue'; export default { components: { TabBar, }, }; </script> <style> /* 这里可以添加全局样式 */ </style> ``` 5. 最后,你可以根据自己的需要在`TabBar.vue`中设置每个Tab项的图标、文字和链接。你还可以通过修改`active`的值来控制当前选中的Tab。 这样,你就可以在Vue 3项目中使用Vant移动端的TabBar了。记得根据自己的需求进行样式和功能的调整。希望对你有帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博客园实习巡查员(工号95270)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值