vue在hash路由模式下实现点击定位滑动

背景:一般是使用锚点来实现dom的定位,但在hash模式下,这种方式不行,会刷新路由,没法实现dom定位的效果。

其实scrollIntoView就可以解决这个问题,scrollIntoView可以将调用它的元素滚动到浏览器窗口的可见区域。

代码实现:

css 复制代码
<!--html模块-->
<div class="bottom-wrapper">
        <div class="left">
            <div @click="goToDom(item.value)" v-for="(item,index) in menuList" :key="index" class="menu-item">
                <span>{{item.name}}</span>
            </div>
        </div>
        <div class="right">
            <div class="scroll-container">
                <div class="scroll-page" ref="modelA">
                    <div class="head">模块1</div>
                 </div>
                <div class="scroll-page" ref="modelB">
                    <div class="head">模块2</div>
                 </div>
                <div class="scroll-page" ref="modelC">
                    <div class="head">模块3</div>
                 </div>
            </div>
</div>
css 复制代码
//关键方法
//跳转到指点的位置
 const goToDom = (refName) => {
        proxy.$refs[refName].scrollIntoView({block: 'start', behavior: 'smooth'});
  }
css 复制代码
    .bottom-wrapper {
        display: flex;
        margin: 10px;
        scroll-behavior: smooth;
        height: calc(100% - 365px);

        .left {
            width: 150px;
            @include box-ht;
            display: flex;
            flex-direction: column;
            align-items: center;

            .menu-item {
                margin: 5px 0;
                border-bottom: 1px solid $theme-bg;
                padding-bottom: 5px;
                color: #000000;
                &:hover{
                    cursor: pointer;
                }
            }
        }

        .right {
            margin-left: 10px;
            width: calc(100% - 150px);
            height: 100%;

            .scroll-container {
                /* 核心代码:缓慢滚动过渡 */
                scroll-behavior: smooth;
                display: block;
                height: 100%;
                overflow-y: scroll;
            }

            .scroll-page {
                position: relative;
                @include box-ht;
                margin: 10px 0;

                .head {
                    font-weight: bold;
                    border-bottom: 1px solid $theme-bg;
                    padding-bottom: 5px;
                }
            }
        }
    }
相关推荐
数聚天成DeepSData13 分钟前
外贸海关进出口数据去哪免费下载?从统计到明细的查找指南
linux·服务器·开发语言·前端·网络·人工智能·自然语言处理
半夜里咳嗽的狼19 分钟前
组件进侧栏就变形?用 CSS Container Queries 把响应式边界收回组件
前端·css
xx240625 分钟前
前端性能优化笔记
前端·笔记·性能优化
带娃的IT创业者31 分钟前
单文件架构的极致美学:深入解析 Bento 的 HTML 幻灯片技术实现
前端·架构·html·web开发·bento·单文件架构
muddjsv34 分钟前
CSS 值的完整计算过程:指定值、计算值、使用值、实际值与动态依赖
前端·css
恋猫de小郭35 分钟前
KotlinLLM 开源 ,一个可以在运行时自己生成永久 Kotlin 代码的 Agent 库
android·前端·人工智能
名字还没想好☜37 分钟前
React createPortal 实战:模态框逃出 overflow:hidden、事件冒泡与焦点管理
前端·javascript·react.js·ecmascript·react·createportal
用户059540174461 小时前
Playwright测试AI记忆存储踩坑实录:这个时序问题让我排查了6小时
前端·css
IT_陈寒1 小时前
Redis踩了个大坑,原来DEL命令也会卡住整个实例
前端·人工智能·后端
一个水瓶座程序猿.1 小时前
基于Spring AI RAG 的AI知识库前端交互实现
前端·人工智能·spring