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导航栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • 详解如何使用vue实现可视化界面设计

    详解如何使用vue实现可视化界面设计

    Vue是一款流行的前端开发框架,它的响应式数据绑定和组件化特性使得它成为了可视化界面设计的一个理想选择,本文将介绍如何使用Vue实现可视化界面设计,并且演示一个基于Vue的可视化界面设计案例,需要的朋友可以参考下
    2023-12-12
  • Vue源码解析之Template转化为AST的实现方法

    Vue源码解析之Template转化为AST的实现方法

    这篇文章主要介绍了Vue源码解析之Template转化为AST的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • vue-pdf插件实现pdf文档预览方式(自动分页预览)

    vue-pdf插件实现pdf文档预览方式(自动分页预览)

    这篇文章主要介绍了vue-pdf插件实现pdf文档预览方式(自动分页预览),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • vue中modal传输数据并刷新部分页面数据方式

    vue中modal传输数据并刷新部分页面数据方式

    这篇文章主要介绍了vue中modal传输数据并刷新部分页面数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • vue2自定义组件通过rollup配置发布到npm的详细步骤

    vue2自定义组件通过rollup配置发布到npm的详细步骤

    这篇文章主要介绍了vue2自定义组件通过rollup配置发布到npm,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • vue 项目优雅的对url参数加密详解

    vue 项目优雅的对url参数加密详解

    这篇文章主要为大家介绍了vue 项目优雅的对url参数加密详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • vue.js父组件使用外部对象的方法示例

    vue.js父组件使用外部对象的方法示例

    在我们日常开发中经常因为思维定式,我们会犯一些奇怪的错误,有时候看似简单的问题却给出了复杂的解决方案。下面这篇文章主要介绍了vue.js父组件使用外部对象的方法,这个看似简单却绕了一些弯路的问题,有必要和大家分享下,需要的朋友可以参考学习,下面来看看吧。
    2017-04-04
  • vue3.0引入百度地图并标记点的实现代码

    vue3.0引入百度地图并标记点的实现代码

    这篇文章主要介绍了vue3.0引入百度地图并标记点,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • vue element-ui里的table中表头与表格出现错位的解决

    vue element-ui里的table中表头与表格出现错位的解决

    这篇文章主要介绍了vue element-ui里的table中表头与表格出现错位的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 详解vue3.x页面功能拆分方式

    详解vue3.x页面功能拆分方式

    本文主要介绍了vue3.x页面功能拆分方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05

最新评论