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

如何实现点击目录跳转到指定位置?【vue】

需求:实现目录点击跳转到指定位置,点击后直接定位到指定模块

效果:

实现方法:

(1)a标签跳转

     普通使用:

<!DOCTYPE html>
<html><head><title>a-Demo</title></head><style>/* bar超出屏幕,显示在屏幕最上方 */.bar {position:fixed;top:0;bottom:0}.bar a {padding: 20px;}div.content div {padding-top: 20px;height: 1000px;scroll-behavior: smooth;}/* 加上这一句,实现平滑滚动效果 */html, body {scroll-behavior:smooth;}</style><body><div class="bar"><a href="#div1">跳转1</a><a href="#div2">跳转2</a><a href="#div3">跳转3</a></div><div class="content"><div id="div1">这是div1</div><div id="div2">这是div2</div><div id="div3">这是div3</div></div></body>
</html>
<script></script>

(2)scrollIntoView跳转

语法:

element.scrollIntoView(); // 等同于element.scrollIntoView(true)
element.scrollIntoView(alignToTop); //布尔参数
element.scrollIntoView(scrollIntoViewOptions); //对象参数

 在vue3中应用(结合上面页面中的案例):

绑定a标签,href地址填入对应id名称,增加点击事件

 <div v-for="(item,index) in navList" :key="index"><a :href="'#' + index" class="mulu-item" :class="index==navIndex?'active':''"  @click.prevent="clickNav(index,item)" ><img :src="item.icon" alt="">{{item.title}}</a></div>
<div class="link" v-for="(content,index) in navList" :key="content.name"><h2 :id="index">{{content.title}}</h2>........</div>
const clickNav = (index, item) => {navIndex.value = indexconst element = document.getElementById(index);element.scrollIntoView({ behavior: 'smooth' });}

(3)v-scrollspy插件

在 Vue 中,你可以使用 v-scrollspy 指令来设置目录的锚点。首先,你需要安装 vue-scrollspy

import VueScrollspy from 'vue-scrollspy';

在vue组件中使用:

<div class="mulu-item" :class="index==navIndex?'active':''"  @click="clickNav(index,item)" ><img :src="item.icon" alt=""><a :href="'#' + index">{{item.title}}</a></div><div class="link" v-for="(content,index) in navList" :key="content.name"><h2 :id="index">{{content.title}}</h2>........</div>
import VueScrollspy from 'vue-scrollspy';methods: {clickNav(index, item) {this.navIndex = index;this.$scrollspy.scrollTo(index);}}

http://www.lryc.cn/news/489258.html

相关文章:

  • SQL 通配符
  • ubuntu显示管理器_显示导航栏
  • 黑芝麻嵌入式面试题及参考答案
  • 使用 PyTorch-BigGraph 构建和部署大规模图嵌入的完整教程
  • 系统性能优化方法论详解:从理解系统到验证迭代
  • 使用Tengine 对负载均衡进行状态检查(day028)
  • 网站推广实战案例:杭州翔胜科技有限公司如何为中小企业打开市场大门
  • 视频修复技术和实时在线处理
  • 文心一言 VS 讯飞星火 VS chatgpt (396)-- 算法导论25.2 1题
  • 如何使用本地大模型做数据分析
  • 【Nginx从入门到精通】04-安装部署-使用XShell给虚拟机配置静态ip
  • C# 面向对象的接口
  • 使用IDEA+Maven实现MapReduced的WordCount
  • go语言示例代码
  • 华为云容器监控平台
  • 阿里短信发送报错 InvalidTimeStamp.Expired
  • Ubuntu问题 -- 设置ubuntu的IP为静态IP (图形化界面设置) 小白友好
  • Sigrity SPEED2000 TDR TDT Simulation模式如何进行时域阻抗仿真分析操作指导-差分信号
  • Cesium 加载B3DM模型
  • 阿里巴巴官方「SpringCloudAlibaba全彩学习手册」限时开源!
  • Docker是一个容器化平台注意事项
  • Redis中的zset用法详解
  • 上位机编程命名规范
  • Python 操作mysql - 关系型数据库存储
  • React基础知识一
  • 游戏行业趋势:“AI、出海、IP”大热下,如何提升竞争力?
  • shell--第一次作业
  • Rust:原子操作 AtomicBool
  • 深入浅出学算法002-n个1
  • GPT1.0 和 GPT2.0 的联系与区别