Vue2——监听页面滚动实现菜单和页面对应

简介: Vue2——监听页面滚动实现菜单和页面对应

前言

如题,监听页面的滚动并激活相应的菜单,一个老项目的维护,后面反正要全部重构,这里就先实现功能就好了;

内容

元素内容

主要是添加相应的id,生成目录后直接通过锚点来跳转

监听滚动

window.addEventListener('scroll', () => {
      const sections = document.getElementsByClassName('card-item')
      for (let i = 0; i < sections.length; i++) {
        const top = sections[i].getBoundingClientRect().top
        if (top < 100 && top > 0) this.activeIndex = i
        if (i === 4 && top < 0) this.activeIndex = 6
      }
    }, true)

DEMO示例

<template>
  <div>
    <el-row :gutter="24">
      <el-col
        :span="4"
        style="position: sticky;top: 10px;"
      >
        <el-card
          class="card-item-menu"
          shadow="hover"
        >
          <div class="menu">项目计划</div>
          <ul>
            <li
              v-for="(item, index) in menu"
              :key="item.href"
              class="sub-item"
            >
              <el-link
                :class="{active: index == activeIndex}"
                :href="item.href"
                :underline="false"
              >{{ item.title }}</el-link>
            </li>
          </ul>
        </el-card>
      </el-col>
      <el-col :span="20">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="80px"
        >
          <!-- 营销目标和场景 -->
          <el-card
            id="p1"
            class="card-item"
            shadow="hover"
            header="营销目标与场景"
          >
          </el-card>
          <!-- 投放内容与目标 -->
          <el-card
            id="p2"
            class="card-item"
            shadow="hover"
            header="投放内容与目标"
          >
          </el-card>
          <!-- 投放版位 -->
          <el-card
            id="p3"
            class="card-item"
            shadow="hover"
            header="投放版位"
          >
          </el-card>
          <!-- 投放定向 -->
          <AudienceTarget
            id="p4"
            :form-data="formData"
            @data="handleFormData"
          />
          <!-- 排期与预算 -->
          <el-card
            id="p5"
            class="card-item"
            shadow="hover"
            header="排期与预算"
          >
          </el-card>
          <!-- 搜索快投 -->
          <el-card
            id="p6"
            class="card-item"
            shadow="hover"
            header="搜索快投"
          >
          </el-card>
          <!-- 项目名称 -->
          <el-card
            id="p7"
            class="card-item"
            shadow="hover"
            header="项目名称"
          >
          </el-card>
          <el-form-item>
            <el-button
              type="primary"
              @click="onSubmit"
            >立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeIndex: 0,
      menu: [
        { href: '#p1', title: '营销目标与场景' },
        { href: '#p2', title: '投放内容与目标' },
        { href: '#p3', title: '投放版位' },
        { href: '#p4', title: '用户定向' },
        { href: '#p5', title: '排期与预算' },
        { href: '#p6', title: '搜索快投' },
        { href: '#p7', title: '项目名称' }
      ],
    }
  },
  mounted() {
    window.addEventListener('scroll', () => {
      const sections = document.getElementsByClassName('card-item')
      for (let i = 0; i < sections.length; i++) {
        const top = sections[i].getBoundingClientRect().top
        if (top < 100 && top > 0) this.activeIndex = i
        if (i === 4 && top < 0) this.activeIndex = 6
      }
    }, true)
  },
  methods: {
  }
}
</script>
<style scoped>
.card-item-menu {
    margin: 15px;
    height: 800px;
    cursor: pointer;
    .menu {
        color: #2a55e5;
        font-weight: 800;
        height: 22px;
        line-height: 22px;
    }
    .active {
          color: #2a55e5;
    }
    .sub-item {
        display: flex;
        min-height: 40px;
        padding: 8px 0px;
        color: #64666b;
        padding-left: 40px;
        padding-right: 9px;
        align-items: center;
        font-size: 14px;
        cursor: pointer;
        position: relative;
    }
    .sub-item:before {
        content: "";
        width: 4px;
        height: 4px;
        display: block;
        border-radius: 4px;
        background-color: #64666b;
        position: relative;
        right: 18px;
        transition: all 0.2s;
    }
}
.card-item {
    /* width: 800px; */
    margin: 10px;
}
:deep(.el-select .el-input__inner) {
    width: 300px;
}
:deep(.el-input__inner) {
    width: 300px;
}
</style>

学无止境,谦卑而行.

目录
相关文章
|
数据采集 数据挖掘 API
主流电商平台数据采集API接口|【Python爬虫+数据分析】采集电商平台数据信息采集
随着电商平台的兴起,越来越多的人开始在网上购物。而对于电商平台来说,商品信息、价格、评论等数据是非常重要的。因此,抓取电商平台的商品信息、价格、评论等数据成为了一项非常有价值的工作。本文将介绍如何使用Python编写爬虫程序,抓取电商平台的商品信息、价格、评论等数据。 当然,如果是电商企业,跨境电商企业,ERP系统搭建,我们经常需要采集的平台多,数据量大,要求数据稳定供应,有并发需求,那就需要通过接入电商API数据采集接口,封装好的数据采集接口更方便稳定高效数据采集。
|
前端开发
Promise.all()方法的作用是什么?
Promise.all()方法的作用是什么?
941 121
|
机器学习/深度学习 存储 运维
机器学习异常检测实战:用Isolation Forest快速构建无标签异常检测系统
本研究通过实验演示了异常标记如何逐步完善异常检测方案和主要分类模型在欺诈检测中的应用。实验结果表明,Isolation Forest作为一个强大的异常检测模型,无需显式建模正常模式即可有效工作,在处理未见风险事件方面具有显著优势。
1277 46
|
网络协议 算法 关系型数据库
解读 MySQL Client/Server Protocol: Connection & Replication(上)
解读 MySQL Client/Server Protocol: Connection & Replication
590 0
|
JavaScript
基于Vue2超简单实现DIY定制的步骤条
这篇文章介绍了如何在Vue 2中实现一个自定义设计的步骤条组件,包括子组件的设计和在父页面中的使用示例。
664 1
基于Vue2超简单实现DIY定制的步骤条
|
JSON JavaScript 定位技术
Echarts 绘制地图(中国、省市、区县),保姆级教程!
Echarts 绘制地图(中国、省市、区县),保姆级教程!
30112 154
|
JSON 前端开发 JavaScript
成功解决:[object Object]
这篇文章讨论了在JavaScript中打印对象时出现的"[object Object]"问题的原因,并提供了使用`JSON.stringify()`方法将对象转换为字符串以便于打印和调试的解决方案。
成功解决:[object Object]
|
移动开发 资源调度 JavaScript
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
这篇文章介绍了在Vue移动端网页中使用`pdfh5`和`vue-pdf`两个插件来实现PDF文件的预览,包括滚动查看、缩放、添加水印、分页加载、跳转指定页数等功能。
12189 1
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
|
Python
python实现学生信息管理系统(附源码 可供大作业或练习使用)
python实现学生信息管理系统(附源码 可供大作业或练习使用)
1368 2
python实现学生信息管理系统(附源码 可供大作业或练习使用)
|
小程序 前端开发 语音技术
开源!!!垃圾分类识别小程序--------附源码!!!
开源!!!垃圾分类识别小程序--------附源码!!!
787 0