Vue3网站锚点定位

网站上实现锚点定位的方法有很多,今天介绍vue3实现的定位效果的方式。通过scrollIntoViewh函数的行为,滑动至指定的容器。

一、样式布局

javascript 复制代码
<template>
<div  style="width: 100%; display: flex;flex-direction: column;">
    <div style="display: flex;flex-direction: row; height: 48px; width: 100%;background-color: cadetblue;">
      <div @click="handleAnchor('JinYong')"  style="height: 40px;padding: 10px;">金庸</div>
      <div @click="handleAnchor('GuLong')" style="height: 40px;padding: 10px;">古龙</div>
      <div @click="handleAnchor('LiangYuSheng')" style="height: 40px;padding: 10px;">梁羽生</div>
      <div @click="handleAnchor('WenRuiAn')" style="height: 40px;padding: 10px;">温瑞安</div>
      <div @click="handleAnchor('HuangYi')" style="height: 40px;padding: 10px;">黄易</div>
    </div>
    <div ref="JinYong" style="height: 600px;width: 100%; background-color: blueviolet;">
        <span style="display: block; margin: 10px;font-size: larger;">书剑恩仇录 雪山飞狐</span>
    </div>
    <div ref="GuLong" style="height: 600px;width: 100%; background-color:brown;">
        <span style="display: block; margin: 10px;font-size: larger;">边城浪子 风云第一刀</span>
    </div>
    <div  ref="LiangYuSheng" style="height: 600px;width: 100%; background-color:chocolate;">
        <span style="display: block; margin: 10px;font-size: larger;">白发魔女传  塞外传奇</span>
    </div>
    <div ref="WenRuiAn" style="height: 600px;width: 100%; background-color:darkgray;">
        <span style="display: block; margin: 10px;font-size: larger;">四大名捕 逆水寒</span>
    </div>
    <div ref="HuangYi" style="height: 600px;width: 100%; background-color:lightgreen;">
        <span style="display: block; margin: 10px;font-size: larger;">大唐双龙转 寻秦记</span>
    </div>
</div>
</template>

<script setup>
const { proxy } = getCurrentInstance()

function handleAnchor(herf){
    proxy.$refs[herf].scrollIntoView({ behavior: 'smooth' })
}
</script>

二、样式效果

相关推荐
IT_陈寒几秒前
Spring Boot 3.2震撼发布:5个必知的新特性让你开发效率提升50%
前端·人工智能·后端
San30.3 分钟前
JavaScript 深度解析:从 map 陷阱到字符串奥秘
开发语言·javascript·ecmascript
初遇你时动了情6 分钟前
前端使用TensorFlow.js reactjs调用本地模型 实现图像、文本、音频/声音、视频相关识别
前端·javascript·tensorflow
广州华水科技12 分钟前
单北斗GNSS变形监测系统安装与应用解析,提升位移监测精度
前端
J***Q29215 分钟前
前端微前端框架原理,qiankun源码分析
前端·前端框架
十一.36615 分钟前
66-69 原型对象,toString(),垃圾回收
开发语言·javascript·原型模式
菜鸟‍16 分钟前
【前端学习】React学习【万字总结】
前端·学习·react.js
百***844522 分钟前
Webpack、Vite区别知多少?
前端·webpack·node.js
Mintopia27 分钟前
零信任架构下的 WebAIGC 服务安全技术升级方向
前端·人工智能·trae
敏姐的后花园2 小时前
模考倒计时网页版
java·服务器·前端