最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3實(shí)現(xiàn)tabs導(dǎo)航欄點(diǎn)擊每個(gè)導(dǎo)航項(xiàng)有下劃線動(dòng)畫(huà)效果

 更新時(shí)間:2024年07月19日 10:31:23   作者:我愛(ài)畫(huà)頁(yè)面  
這篇文章主要介紹了vue3實(shí)現(xiàn)tabs導(dǎo)航欄點(diǎn)擊每個(gè)導(dǎo)航項(xiàng)有下劃線動(dòng)畫(huà)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

vue3實(shí)現(xiàn)tabs導(dǎo)航欄,點(diǎn)擊每個(gè)導(dǎo)航項(xiàng)有下劃線動(dòng)畫(huà)效果

<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 = () => {
  // 獲取標(biāo)題的寬度,減去下劃線的寬度除以2,可以使下劃線在標(biāo)題下居中顯示
  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 實(shí)現(xiàn)類似于錨點(diǎn)滑動(dòng) 或者點(diǎn)擊不同的tab時(shí)添加下劃線并滑動(dòng)到對(duì)應(yīng)的盒子,手動(dòng)滑動(dòng)下劃線也隨位置變動(dòng)

1、這個(gè)方法有個(gè)不好的地方就是要自己算每個(gè)盒子要滑動(dòng)的高度,下面是結(jié)構(gòu)

<ul>
          <li class="liTem" :class="{ acver: currentTab === 'tab1' }" @click="scrollToTab('tab1')">首頁(yè)</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab2' }"  @click="scrollToTab('tab2')">服務(wù)內(nèi)容</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab3' }" @click="scrollToTab('tab3')">業(yè)務(wù)案例</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab4' }" @click="scrollToTab('tab4')">關(guān)于我們</li>
          <li class="liTem" :class="{ acver: currentTab === 'tab5' }" @click="scrollToTab('tab5')">聯(lián)系方式</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、簡(jiǎn)單寫(xiě)點(diǎn)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: {
        // 每個(gè)tab對(duì)應(yīng)的滾動(dòng)位置
        tab1: 0,
        tab2: 670,
        tab3: 1700,
        // tab4: 4360,
        tab4: 3760,
        tab5: 5015,
      },

4、這里寫(xiě)點(diǎn)擊事件

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
        ) {
          // 假設(shè)50是閾值
          this.currentTab = tab;
          break;
        }
      }
    },
}

到此這篇關(guān)于vue3實(shí)現(xiàn)tabs導(dǎo)航欄點(diǎn)擊每個(gè)導(dǎo)航項(xiàng)有下劃線動(dòng)畫(huà)效果的文章就介紹到這了,更多相關(guān)vue3 tabs導(dǎo)航欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3+antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法

    Vue3+antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法

    這篇文章主要為大家詳細(xì)介紹了基于Vue3和antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的小伙伴可以了解下
    2024-01-01
  • 前端實(shí)現(xiàn)Vue組件頁(yè)面跳轉(zhuǎn)的多種方式小結(jié)

    前端實(shí)現(xiàn)Vue組件頁(yè)面跳轉(zhuǎn)的多種方式小結(jié)

    這篇文章主要為大家詳細(xì)介紹了前端實(shí)現(xiàn)Vue組件頁(yè)面跳轉(zhuǎn)的多種方式,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,有需要的小伙伴可以了解下
    2024-02-02
  • 一文帶你搞懂Vue3如何使用訊飛大模型

    一文帶你搞懂Vue3如何使用訊飛大模型

    這篇文章主要為大家詳細(xì)介紹了Vue3使用訊飛大模型的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • Vue.js函數(shù)式組件的全面了解

    Vue.js函數(shù)式組件的全面了解

    函數(shù)式組件就是函數(shù)是組件,組件是函數(shù),它的特征是沒(méi)有內(nèi)部狀態(tài)、沒(méi)有生命周期鉤子函數(shù)、沒(méi)有this(不需要實(shí)例化的組件),這篇文章主要給大家介紹了關(guān)于Vue.js函數(shù)式組件的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 淺談vue的第一個(gè)commit分析

    淺談vue的第一個(gè)commit分析

    這篇文章主要介紹了淺談vue的第一個(gè)commit分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • vue+canvas實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)從上到下刷新瀑布圖效果(類似QT的)

    vue+canvas實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)從上到下刷新瀑布圖效果(類似QT的)

    這篇文章主要介紹了vue+canvas實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)從上到下刷新瀑布圖效果(類似QT的),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法

    基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法

    這篇文章主要給大家介紹了基于Ant-design-vue的Modal彈窗封裝命令式與Hooks用法,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以借鑒閱讀
    2023-06-06
  • Vue中$on和$emit的實(shí)現(xiàn)原理分析

    Vue中$on和$emit的實(shí)現(xiàn)原理分析

    這篇文章主要介紹了Vue中$on和$emit的實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解vue-router導(dǎo)航守衛(wèi)

    詳解vue-router導(dǎo)航守衛(wèi)

    今天小編就為大家分享一篇關(guān)于詳解vue-router導(dǎo)航守衛(wèi),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • uni-app?開(kāi)發(fā)微信小程序定位功能

    uni-app?開(kāi)發(fā)微信小程序定位功能

    這篇文章主要介紹了uni-app?開(kāi)發(fā)微信小程序定位,通過(guò)使用onLocationChange方法持續(xù)監(jiān)聽(tīng)地址,根據(jù)定位精度字段判斷是否使用此次定位的經(jīng)緯度,需要的朋友可以參考下
    2022-04-04

最新評(píng)論

美姑县| 新蔡县| 抚宁县| 高台县| 长顺县| 京山县| 纳雍县| 泾川县| 河间市| 旅游| 唐海县| 翁源县| 平塘县| 昭通市| 彰化市| 连南| 兴义市| 若羌县| 稻城县| 松滋市| 壶关县| 江孜县| 盐亭县| 山东省| 定远县| 闵行区| 金阳县| 肃宁县| 扶风县| 淄博市| 德格县| 吴桥县| 金溪县| 宁强县| 浑源县| 沁阳市| 永善县| 太保市| 涿鹿县| 广饶县| 山东省|