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>

二、样式效果

相关推荐
陈随易5 小时前
有生之年系列,Nodejs进程管理pm2 v7.0发布
前端·后端·程序员
冰暮流星6 小时前
javascript之事件代理/事件委托
前端
陈随易7 小时前
AI时代,你还在坚持手搓文章吗
前端·后端·程序员
里欧跑得慢9 小时前
17. Flutter Hero动画实现:让界面过渡更加优雅
前端·css·flutter·web
IT_陈寒9 小时前
Vue的这个响应式陷阱,我debug了一整天才爬出来
前端·人工智能·后端
cn_mengbei10 小时前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
kyriewen10 小时前
前端测试:别为了100%覆盖率而写测试,那是自欺欺人
前端·javascript·单元测试
去伪存真10 小时前
我自己写的第一个skills--project-core-standards
前端·agent
Data_Journal10 小时前
如何使用cURL更改User Agent
大数据·服务器·前端·javascript·数据库
掌心向暖RPA自动化10 小时前
如何获取网页某个元素在屏幕可见部分的中心坐标影刀RPA懒加载坐标定位技巧
java·javascript·自动化·rpa·影刀rpa