手写tab选中

html 复制代码
<div class="appBox">
   <div class="navBox">
     <p class="navItemBox" v-for="(item) in navList" :key="item.id" :class="activeNav == item.id ? 'activeText':''"
       @click="scrollToTarget(item.id)">
       <span>{{item.title}}</span>
     </p>
   </div>

   <div class="navContentBox">
     <div id="allApp" class="content-section">
       全部
     </div>
     <div id="app1" class="content-section">
       应用1
     </div>
     <div id="app2" class="content-section">
       应用2
     </div>
     <div id="app3" class="content-section">
       应用3
     </div>
     <div id="app4" class="content-section">
       应用4
     </div>
   </div>
</div>


<script setup>
import { ref, onMounted, reactive, } from 'vue'
// import { queryJgAPI, } from '@/api/instruct'
import { ElMessage, ElMessageBox } from 'element-plus';

// 记录当前激活的导航项(可选,用于高亮)
const activeNav = ref('allApp');

const navList = ref([
  { id:'allApp', title: '全部', },
  { id:'app1', title: '应用1', },
  { id:'app2', title: '应用2', },
  { id:'app3', title: '应用3', },
  { id:'app4', title: '应用4', },
])

const state = reactive({})

// 核心滚动方法
const scrollToTarget = (key) => {
  // 1. 更新激活状态(高亮当前项)
  activeNav.value = key;
  // 2. 找到右侧目标元素
  const target = document.getElementById(key);
  if (target) {
    // 3. 平滑滚动到目标元素
    target.scrollIntoView({
      behavior: 'smooth', // 平滑滚动(去掉则立即跳转)
      block: 'start' // 对齐到元素顶部(可选:center/end)
    });
  }
};

onMounted(()=>{})
</script>
<style>
.appBox{
   width: 100%;
   height: 100%;
   background-color: #fff;

   .navBox{
     display: flex;
     gap: 40px;
     align-items: center;
     padding: 8px 30%;
     font-weight: 600;

     .navItemBox{
       position: relative;
       padding: 10px;
     }
   }
   .activeText{
     &:after{
       content: '';
       display: block;
       position: absolute;
       bottom: 0;
       left: 50%;// 从中间开始
       transform: translateX(-50%);// 向左偏移自身宽度的一半,实现居中
       width: 40px; // 默认宽度为 0
       height: 3px;
       border-radius: 2px;
       transition: width 0.3s ease;  
       background-color: #426deb;
     }
   }

   .navContentBox{
     height: 98%;
     overflow: auto;
     
     .content-section{
       height: 40%;
       // background-color: pink;
     }
   }
 }
</style>
相关推荐
Z兽兽4 小时前
行内块元素在垂直方向上对齐问题
css·html·css3
后台模板学习4 小时前
从0到1用Vite构建电商订单系统前端性能优化方案
前端
Wang's Blog4 小时前
Vibe Coding一人即团队系列58: HTML演示文稿自动生成
前端·人工智能·html
FoldWinCard5 小时前
基于k8s高可用web集群
前端·容器·kubernetes
神探小白牙5 小时前
海康视频在vue2.0中的使用
前端·音视频
Tanjia_kiki5 小时前
谷歌浏览器中F12编辑并重发请求
开发语言·前端·javascript
honkun65 小时前
vue 表格组件 vxe-table 实现拖拽列字段自动生成透视表汇总
前端·javascript·vue.js
新中地GIS开发老师5 小时前
WebGIS开发入门 | 从 Web 开发到地图开发,差别在哪儿?
前端·vue·webgis
IMPYLH5 小时前
HTML 的 <p> 元素
前端·javascript·html
leoZ2315 小时前
第 4 篇:布局骨架——页面壳、栅格、卡片
前端·javascript·vue.js·opencv·计算机视觉·数据挖掘·语音识别