手写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>
相关推荐
子兮曰4 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰4 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万4 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝4 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋4 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁4 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄4 天前
JavaScript 隐式全局变量解析
javascript
汉堡大王95274 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大4 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师4 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端