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>

二、样式效果

相关推荐
脾气有点小暴15 小时前
UniApp实现刷新当前页面
开发语言·前端·javascript·vue.js·uni-app
YaeZed15 小时前
Vue3-全局组件 && 递归组件
前端·vue.js
一只Viki15 小时前
给 CS2 Major 竞猜做了个在线抄作业网站
前端
八点15 小时前
Electron 应用中 Sharp 模块跨架构兼容性问题解决方案
前端
黑臂麒麟15 小时前
DevUI modal 弹窗表单联动实战:表格编辑功能完整实现
前端·javascript·ui·angular.js
国服第二切图仔15 小时前
DevUI Design中后台产品的开源前端解决方案之DataTable 表格组件核心解析
前端
懒人村杂货铺15 小时前
FastAPI + 前端(Vue/React)Docker 部署全流程
前端·vue.js·fastapi
7***374515 小时前
前端技术的下一站:从“页面开发”走向“体验工程”
前端
哆啦A梦158815 小时前
商城后台管理系统 01,商品管理-搜索
前端·javascript·vue.js
苏小瀚15 小时前
[JavaEE] Spring Web MVC入门
前端·java-ee·mvc