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

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

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

代码实现:

<!--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>
//关键方法
//跳转到指点的位置
 const goToDom = (refName) => {
        proxy.$refs[refName].scrollIntoView({block: 'start', behavior: 'smooth'});
  }
    .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;
                }
            }
        }
    }

相关推荐

  1. vuehash模式实现定位滑动

    2024-06-13 06:34:03       26 阅读
  2. Vue实现hash模式 和 history模式原理

    2024-06-13 06:34:03       55 阅读
  3. vue3 hash和history模式配置

    2024-06-13 06:34:03       37 阅读
  4. Vue Router hash和history模式

    2024-06-13 06:34:03       34 阅读
  5. Vue2面试题:说一下模式hash和history的区别?

    2024-06-13 06:34:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-13 06:34:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-13 06:34:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-13 06:34:03       87 阅读
  4. Python语言-面向对象

    2024-06-13 06:34:03       96 阅读

热门阅读

  1. 学习AI 机器学习,深度学习需要用到的python库

    2024-06-13 06:34:03       32 阅读
  2. 深度学习每周学习总结N1(one-hot 编码案例)

    2024-06-13 06:34:03       22 阅读
  3. 人脑神经元与AI神经网络的奥秘

    2024-06-13 06:34:03       31 阅读
  4. Android 10.0 framework层禁止扫描5g wifi功能实现

    2024-06-13 06:34:03       27 阅读
  5. 【无标题】

    2024-06-13 06:34:03       28 阅读
  6. k8s-CCE创建工作负载变量引用

    2024-06-13 06:34:03       29 阅读
  7. vscode react我常用的开发插件汇总

    2024-06-13 06:34:03       37 阅读
  8. 创建项目模板:vite+vue2+eslint

    2024-06-13 06:34:03       30 阅读