vue3实现tabs导航栏点击每个导航项有下划线动画效果

 更新时间:2024年07月19日 10:31:23   作者:我爱画页面  
这篇文章主要介绍了vue3实现tabs导航栏点击每个导航项有下划线动画效果,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

vue3实现tabs导航栏,点击每个导航项有下划线动画效果

<template>
  <div class="navigation">
    <div
      v-for="(item, index) in items"
      :key="index"
      class="navigation-item"
      :class="{ active: index === activeIndex }"
      @click="activeIndex = index"
      ref="itemRefs"
    >
      {{ item }}
    </div>
    <div
      class="navigation-underline"
      :class="{ active: activeIndex !== -1 }"
      :style="underlineStyle"
      ref="underlineRef"
    ></div>
  </div>
</template>
<script setup>
import { ref, watch, onMounted } from "vue";
const activeIndex = ref(0);
const items = ref(["Home", "About", "Services", "Services"]);
const underlineStyle = ref({
  transform: "translateX(0)",
});
const itemRefs = ref([]);
const underlineRef = ref(null);
const updateUnderline = () => {
  // 获取标题的宽度,减去下划线的宽度除以2,可以使下划线在标题下居中显示
  const item = itemRefs.value[activeIndex.value];
  const underline = underlineRef.value;
  let newLeft = Number((item.offsetWidth - underline.offsetWidth)) / 2;
  if (item && underline) {
    const { offsetLeft } = item;
    console.log(offsetLeft);
    underlineStyle.value = {
      transform: `translateX(${offsetLeft + newLeft}px)`,
    };
  }
};
watch(activeIndex, () => {
  updateUnderline();
});
onMounted(() => {
  updateUnderline();
});
</script>
<style>
.navigation {
  width: 400px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  position: relative;
  padding: 0 20px;
  height: 50px;
  font-size: 16px;
  font-weight: bold;
}
.navigation-item {
  cursor: pointer;
}
.navigation-item.active {
  color: #f00;
}
.navigation-underline {
  /* 下划线的宽度 */
  width: 50px;
  height: 3px;
  border-radius: 2px;
  position: absolute;
  bottom: 0;
  left: 0;
  background-color: #f00;
  transition: all 0.3s ease-in-out;
}
.navigation-underline.active {
  transform: translateY(-3px);
}
</style>

效果图:

vue 实现类似于锚点滑动 或者点击不同的tab时添加下划线并滑动到对应的盒子,手动滑动下划线也随位置变动

1、这个方法有个不好的地方就是要自己算每个盒子要滑动的高度,下面是结构

<ul>
          <li class="liTem" :class="{ acver: currentTab === 'tab1' }" @click="scrollToTab('tab1')">首页</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab2' }"  @click="scrollToTab('tab2')">服务内容</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab3' }" @click="scrollToTab('tab3')">业务案例</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab4' }" @click="scrollToTab('tab4')">关于我们</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab5' }" @click="scrollToTab('tab5')">联系方式</li>
        </ul>
<div class="section1 " id="section1">Section 1</div>
<div class="section2 " id="section2">Section 2</div>
<div class="section3 " id="section3">Section 3</div>

2、简单写点css

.tabsul {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #f0f0f0;
    display: flex;
    justify-content: center;
    z-index: 1000;
  }
  .liTem {
    padding: 0 0px 5px 0px;
    box-sizing: border-box;
    height: 35px;
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    margin: 0 20px;
    cursor: pointer;
  }
  .section {
    display: flex;
    justify-content: center;
    font-size: 24px;
    color: #fff;
    border: 1px solid #ccc;
  }
  .section1{
    height: 430px;
    background: #48a994;
  }
  .section2{
    height: 1200px;
    background: #49396f;
  }
  .section3{
    height: 600px;
    background: #349f64;
  }
  .section4{
    height: 1200px;
    background: #9c7d39;
  }
.acver {
          font-size: 16px;
          font-weight: 500;
          border-bottom: 3px #ff8000 solid;
        }

3、在data里声明这些变量

currentTab: "tab1",
      tabScrollPositions: {
        // 每个tab对应的滚动位置
        tab1: 0,
        tab2: 670,
        tab3: 1700,
        // tab4: 4360,
        tab4: 3760,
        tab5: 5015,
      },

4、这里写点击事件

mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
  },
 methods: {
     scrollToTab(tab) {
      this.currentTab = tab;
      const position = this.tabScrollPositions[tab];
      window.scrollTo({
        top: position,
        behavior: "smooth",
      });
    },
    handleScroll() {
      const scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      for (const tab in this.tabScrollPositions) {
        if (
          scrollTop >= this.tabScrollPositions[tab] - 50 &&
          scrollTop < this.tabScrollPositions[tab] + 50
        ) {
          // 假设50是阈值
          this.currentTab = tab;
          break;
        }
      }
    },
}

到此这篇关于vue3实现tabs导航栏点击每个导航项有下划线动画效果的文章就介绍到这了,更多相关vue3 tabs导航栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • Vue3+antDesignVue实现表单校验的方法

    Vue3+antDesignVue实现表单校验的方法

    这篇文章主要为大家详细介绍了基于Vue3和antDesignVue实现表单校验的方法,文中的示例代码讲解详细,具有一定的参考价值,需要的小伙伴可以了解下
    2024-01-01
  • 前端实现Vue组件页面跳转的多种方式小结

    前端实现Vue组件页面跳转的多种方式小结

    这篇文章主要为大家详细介绍了前端实现Vue组件页面跳转的多种方式,文中的示例代码讲解详细,具有一定的参考价值,有需要的小伙伴可以了解下
    2024-02-02
  • 一文带你搞懂Vue3如何使用讯飞大模型

    一文带你搞懂Vue3如何使用讯飞大模型

    这篇文章主要为大家详细介绍了Vue3使用讯飞大模型的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • Vue.js函数式组件的全面了解

    Vue.js函数式组件的全面了解

    函数式组件就是函数是组件,组件是函数,它的特征是没有内部状态、没有生命周期钩子函数、没有this(不需要实例化的组件),这篇文章主要给大家介绍了关于Vue.js函数式组件的相关资料,需要的朋友可以参考下
    2021-10-10
  • 浅谈vue的第一个commit分析

    浅谈vue的第一个commit分析

    这篇文章主要介绍了浅谈vue的第一个commit分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • vue+canvas实现数据实时从上到下刷新瀑布图效果(类似QT的)

    vue+canvas实现数据实时从上到下刷新瀑布图效果(类似QT的)

    这篇文章主要介绍了vue+canvas实现数据实时从上到下刷新瀑布图效果(类似QT的),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 基于Ant-design-vue的Modal弹窗 封装 命令式与Hooks用法

    基于Ant-design-vue的Modal弹窗 封装 命令式与Hooks用法

    这篇文章主要给大家介绍了基于Ant-design-vue的Modal弹窗封装命令式与Hooks用法,文中有详细的代码示例,具有一定的参考价值,感兴趣的同学可以借鉴阅读
    2023-06-06
  • Vue中$on和$emit的实现原理分析

    Vue中$on和$emit的实现原理分析

    这篇文章主要介绍了Vue中$on和$emit的实现原理分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • 详解vue-router导航守卫

    详解vue-router导航守卫

    今天小编就为大家分享一篇关于详解vue-router导航守卫,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • uni-app 开发微信小程序定位功能

    uni-app 开发微信小程序定位功能

    这篇文章主要介绍了uni-app 开发微信小程序定位,通过使用onLocationChange方法持续监听地址,根据定位精度字段判断是否使用此次定位的经纬度,需要的朋友可以参考下
    2022-04-04

最新评论