elementui-plus实现锚点功能

效果:点击导航栏菜单,定位到指定的地方

适用于单界面操作

javascript 复制代码
<template>  
  <el-menu>  
    <el-menu-item index="1" @click="scrollToAnchor('section1')">Section 1</el-menu-item>  
    <el-menu-item index="2" @click="scrollToAnchor('section2')">Section 2</el-menu-item>  
  </el-menu>  
  
  <div style="height: 1000px;"> <!-- 假设页面有足够的高度以展示滚动效果 -->  
    <h2 id="section1">Section 1</h2>  
    <!-- 更多内容 -->  
    <h2 id="section2">Section 2</h2>  
    <!-- 更多内容 -->  
  </div>  
</template>  
  
<script>  
export default {  
  methods: {  
    scrollToAnchor(anchorId) {  
      const element = document.getElementById(anchorId);  
      if (element) {  
        element.scrollIntoView({ behavior: 'smooth' });  
      }  
    }  
  }  
}  
</script>
相关推荐
刀法如飞31 分钟前
Claude Code Skills 推荐:2026年最值得安装的10个AI技能
前端·后端·ai编程
阿豪只会阿巴1 小时前
【没事学点啥】TurboBlog轻量级个人博客项目——项目介绍
javascript·python·django·html
Lee川1 小时前
面试手写 KeepAlive:React 组件缓存的实现原理
前端·react.js·面试
墨染天姬1 小时前
【AI】cursor提示词小技巧
前端·数据库·人工智能
烛阴1 小时前
TEngine 入门系列(一):TEngine 是什么 & 为什么选它
前端·unity3d
转转技术团队2 小时前
WebNN:让 AI 推理在浏览器中“零距离”运行
前端
刀法如飞2 小时前
TypeScript 数组去重的 20 种实现方式,哪一种你还不知道?
前端·javascript·算法
IT_陈寒2 小时前
Vite热更新失效?你可能漏了这个小细节
前端·人工智能·后端
海石2 小时前
面试官:说一下你现在使用的 AI IDE,什么,JoyCode 是什么?
前端·ai编程
彩票管理中心秘书长2 小时前
一次搞懂:在Vue里用Showdown渲染Markdown+KaTeX数学公式
前端