当前位置: 首页 > news >正文

锚点跳转跟踪#

 一、html

<div ref="computingref"><section id="section1"> </section><section id="section2"> </section><section id="section3"> </section>
</div><div class="nav-list" v-if="activeHash"><!-- <el-tabs:tab-position="'right'"v-model="activeHash"@tab-change="scrollTo"><el-tab-panev-for="(item, index) in anchorList":key="index":label="item.label" :name="item.value"></el-tab-pane></el-tabs> --><el-timeline style="width: 245px"><el-timeline-itemv-for="(activity, index) in anchorList":key="index":type="activeHash == activity.value?'primary':''":hollow="activeHash != activity.value"><spanclass="timelineclass":style="activeHash == activity.value?'color:#eb682c':''"@click="scrollTo(activity.value)">{{ activity.label }}</span></el-timeline-item></el-timeline></div>

二、js

// 常量定义
const computingref = ref();
const anchorList = ref([{label:'xxxxx',value:'#section1'},{label:'xxxxxxx',value:'#section2'},{label:'xxxxxxxxxxx',value:'#section3'}
])// 响应式数据
const activeHash = ref('');
const offsets = ref([]);// 计算锚点偏移量
const calculateOffsets = () => {offsets.value = anchorList.value.map(e=>{const element = document.querySelector(e.value)return element.offsetTop  - 300 // 上偏移多少})
};// 更新激活的 hash
const updateActiveHash = () => {const scrollTop = computingref.value?.scrollTop || 0const findtops = offsets.value.filter(offset => scrollTop > offset);activeHash.value = findtops.length > 0 ? `#section${findtops.length}` : '';
};// 滚动到指定锚点
const scrollTo = (hash) => {activeHash.value = hashconst target = document.querySelector(hash);if (target) {const offset = target.offsetTop// window.scrollTo(0, offset);if(computingref.value) computingref.value.scrollTop = offset}
};const handleContainerScroll = () => {updateActiveHash()
}// 生命周期钩子
onMounted(() => {nextTick(() => {calculateOffsets();});
});

相关文章:

  • NHANES指标推荐:MDS
  • No module named‘serial‘解决办法
  • 【超分辨率专题】一种考量视频编码比特率优化能力的超分辨率基准
  • 智能AI构建工地安全网:跌倒、抽搐、区域入侵多场景覆盖
  • TII-2024《AGP-Net: Adaptive Graph Prior Network for Image Denoising》
  • 解决CLion控制台不能及时显示输出的问题
  • 告别传统的防抖机制,提交按钮的新时代来临
  • math toolkit for real-time development读书笔记一三角函数快速计算(1)
  • 1Panel应用推荐:Beszel轻量级服务器监控平台
  • 火语言RPA--EmpireV7发布资讯
  • 实战解析MCP-使用本地的Qwen-2.5模型-AI协议的未来?
  • mysql的not exists走索引吗
  • 海盗王3.0的数据库3合1并库处理方案
  • 麒麟桌面系统文件保险箱快捷访问指南:让重要文件夹一键直达桌面!
  • 使用 gcloud CLI 自动化管理 Google Cloud 虚拟机
  • 机器学习入门之KNN算法和交叉验证与超参数搜索(三)
  • 【在aosp中,那些情况下可以拉起蓝牙服务进程】
  • 使用Frp搭建内网穿透,外网也可以访问本地电脑。
  • 第三十三节:特征检测与描述-Shi-Tomasi 角点检测
  • Linux》Ubuntu》安装Harbor 私有仓库
  • 上海交大曾小勤:科技传播不应停留于知识搬运,要做科学思维的播种机
  • 梅花奖在上海|湘剧《夫人如见》竞梅,长沙文旅来沪推广
  • 打击网络侵权盗版!四部门联合启动“剑网2025”专项行动
  • 长三角首次,在铁三赛事中感受竞技与生态的共鸣
  • 新闻1+1丨城市,如何对青年更友好?
  • 外交部:反对美方人士发表不负责任谬论