VUE篇之,实现锚点定位,滚动与导航联动

实现效果:点击侧边栏,右侧内容能够自动滑动到显示区域

滚动显示区域,侧边栏能够跟随高亮

代码如下:

html 复制代码
<template>
  <div class="root">
    <div class="slidebar">
      <ul class="sidebar-anchor-list">
        <li
          class="sidebar-anchor-item"
          :class="{ active: item.anchor === anchor }"
          v-for="item in anchorList"
          @click="anchorPosition(item.anchor)"
        >
          <a class="sidebar-anchor-item-link">
            <span class="sidebar-anchor-item-text">{{ item.text }}</span>
          </a>
        </li>
      </ul>
    </div>
    <div class="main" id="main">
      <div class="page-container" v-for="item in anchorList" :key="item.anchor" :id="item.anchor.replace(/#/g, '')">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>
<script>
import _ from 'lodash';
export default {
  data() {
    return {
      anchorList: [
        {
          anchor: '#labs',
          text: '实验室',
        },
        {
          anchor: '#courses',
          text: '学好课',
        },
        {
          anchor: '#project',
          text: '好作品',
        },
        {
          anchor: '#codingnews',
          text: '资讯',
        },
        {
          anchor: '#cocase',
          text: '校园',
        },
        {
          anchor: '#cooperator',
          text: '合作',
        },
      ],
      anchor: '#labs',
      offsetTopList: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.setOffsetTopList();
      document.getElementById('main').addEventListener('scroll', _.debounce(this.scrollHandle.bind(this), 20));
    });
  },
  methods: {
    anchorPosition(val) {
      this.anchor = val;
      this.scrollTo(val);
    },
    //获取每个模块距离顶部的距离
    setOffsetTopList() {
      const mainEl = document.querySelector('#main');

      this.offsetTopList = this.anchorList.map((item, index) => {
        const element = document.querySelector(item.anchor);
        return {
          offsetTop: index === 0 ? mainEl.offsetTop : element.offsetTop,
          anchor: item.anchor,
        };
      });
      console.log(this.offsetTopList);
    },
    scrollHandle({ target }) {
      const curScrollTop = target.scrollTop;
      const len = this.offsetTopList.length;
      for (let i = len - 1; i >= 0; i--) {
        const curReference = this.offsetTopList[i].offsetTop; // 当前参考值
        if (curScrollTop >= curReference - 10) {
          this.anchor = this.offsetTopList[i].anchor;
          break;
        }
      }
    },
    scrollTo(anchor) {
      const element = document.querySelector(anchor);
      const parent = document.querySelector('#main');
      parent.scrollTo({
        top: element.offsetTop,
        behavior: 'smooth',
      });
    },
  },
};
</script>
<style lang="scss" scoped>
* {
  list-style: none;
  padding: 0;
  margin: 0;
}
.root {
  width: 100%;
  height: 100%;
  display: flex;
}
.slidebar {
  width: 200px;
  height: fit-content;
  background-color: #f0f0f0;
  margin-top: 30px;
  .sidebar-anchor-item {
    height: 68px;
    border-bottom: 1px solid #eff3f8;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 12px;
    transition: all 200ms;
    .sidebar-anchor-item-link {
      display: block;
      width: 100%;
      color: rgba(74, 89, 111, 0.6);
      .sidebaricon {
        display: block;
        margin: 0 auto 6px;
        font-size: 18px;
      }
      .sidebar-anchor-item-text {
        display: block;
      }
    }

    &:hover {
      .sidebar-anchor-item-link {
        color: #009cff;
      }
    }

    &.active {
      .sidebar-anchor-item-link {
        color: #009cff;
      }
      &:hover {
        .sidebar-anchor-item-link {
          color: #009cff;
        }
      }
    }
  }
}
.main {
  width: 100%;
  height: 100%;
  overflow: auto;
  .page-container {
    width: 100%;
    height: 500px;
    border-bottom: 1px solid pink;
  }
}
</style>
相关推荐
phltxy4 分钟前
从零入门JavaScript:基础语法全解析
开发语言·javascript
烛阴5 分钟前
从“无”到“有”:手动实现一个 3D 渲染循环全过程
前端·webgl·three.js
BD_Marathon17 分钟前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot
Kagol17 分钟前
JavaScript 中的 sort 排序问题
前端·javascript
eason_fan44 分钟前
Service Worker 缓存请求:前端性能优化的进阶利器
前端·性能优化
光影少年1 小时前
rn如何和原生进行通信,是单线程还是多线程,通信方式都有哪些
前端·react native·react.js·taro
好大哥呀1 小时前
Java Web的学习路径
java·前端·学习
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue动物园管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
HashTang1 小时前
【AI 编程实战】第 7 篇:登录流程设计 - 多场景、多步骤的优雅实现
前端·uni-app·ai编程
北辰alk1 小时前
深入理解Vue数据流:单向与双向的哲学博弈
vue.js