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>

二、样式效果

相关推荐
祈澈菇凉20 分钟前
如何结合使用thread-loader和cache-loader以获得最佳效果?
前端
垣宇23 分钟前
Vite 和 Webpack 的区别和选择
前端·webpack·node.js
java1234_小锋27 分钟前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
化作繁星30 分钟前
如何在 React 中测试高阶组件?
前端·javascript·react.js
Au_ust37 分钟前
千峰React:函数组件使用(2)
前端·javascript·react.js
爱吃南瓜的北瓜1 小时前
npm install 卡在“sill idealTree buildDeps“
前端·npm·node.js
TTc_1 小时前
记录首次安装远古时代所需的运行环境成功npm install --save-dev node-sass
前端·npm·sass
翻滚吧键盘1 小时前
npm使用了代理,但是代理软件已经关闭导致创建失败
前端·npm·node.js
烂蜻蜓1 小时前
Uniapp 设计思路全分享
前端·css·vue.js·uni-app·html
GAMESLI-GIS1 小时前
【WebGL】fbo双pass案例
前端·javascript·webgl