vue监听页面中的某个div的滚动事件,并判断滚动的位置
在开发中常常会遇到这样一个vue页面,页面分为左右两部分,左边是目录树,右边是一个类名为xq-box的div,在xq-box中多个div上下并列布局,每个div中的内容就对应着左边目录树中的相应节点,现在的目标是,要监听这个xq-box滚动事件,右边一旦开始滚动,就要知道滚动到哪个子div,并让左边的目录树中对应的节点高亮显示。要怎么做呢?
1、首先,先写好大概的页面布局,这里要注意,右边xq-box的子div要绑定"'xqItem'+序号"的id,为了下面用js能获取到匹配的dom元素:

<template>
<div class="container">
<div class="left-box">
<div class="menu-box">
<div class="menu-title">
<p>目录</p>
</div>
<div
class="menu-item"
v-for="(menu, index) in menuList"
:key="index"
:class="{ 'active': menuActive === index }"
@click="chooseMenu(menu.name, index)"
>
<img :src="menu.icon" class="menu-icon" />
<p>{{ menu.name }}</p>
</div>
</div>
</div>
<div class="right-box">
<div class="xq-box" ref="xqBox">
<div
class="xq-item"
:id="'xqItem' + index"
v-for="(item, index) in xqConList"
:key="index"
>
<!--这里渲染出目录内容-->
<div class="xq-item-name">
{{ item.name }}
</div>
<div class="xq-item-con">
{{ item.content }}
</div>
</div>
</div>
</div>
</div>
</template>
2、然后,在css里给xq-box高度,设置其超出能滚动:
<style lang="stylus" scoped>
.right-box
height 600px
.xq-box
height 100%
overflow-y auto
<style>
3、接着,在计算属性获取到这个ref="xqBox"的dom元素,写一个函数handleScroll()获取滚动距离并判断滚动到哪两个子div之间,并在页面渲染完后监听这个xq-box的滚动事件。
export default {
name: "menuList",
data() {
return {
menuActive: 0, //左侧高亮的item
menuList: [], //左侧目录树
xqConList: [] //右侧目录内容列表
}
},
computed: {
xqBox() {
return this.$refs.xqBox;
}
},
mounted() {
this.$nextTick(() => {
// //监听这个dom的scroll事件
// this.xqBox.onscroll = () => {
// console.log("on scroll");
// this.handleScroll();
// };
//监听这个dom的scroll事件
this.xqBox.addEventListener("scroll", this.handleScroll);
});
},
methods: {
handleScroll() {
//获取dom滚动距离
const scrollTop = this.xqBox.scrollTop;
//获取可视区高度
const offsetHeight = this.xqBox.offsetHeight;
//获取滚动条总高度
const scrollHeight = this.xqBox.scrollHeight;
//xqConList 为目录内容列表
for (let i = 0; i < this.xqConList.length - 1; i++) {
let offset_before = this.$el.querySelector("#xqItem" + i).offsetTop; //offsetTop: 获取当前元素到其定位父级(offsetParent)的顶部距离
let offset_after = this.$el.querySelector("#xqItem" + (i + 1))
.offsetTop;
//根据xqItem离顶部距离判断滚动距离落在哪两个item之间
if (scrollTop >= offset_before && scrollTop < offset_after) {
// console.log("offset", offset_before, offset_after, scrollTop);
// console.log("scrollHeight", scrollTop, offsetHeight, scrollHeight);
//判断是否滚动到了底部
if (scrollTop + offsetHeight >= scrollHeight) {
// 把距离顶部的距离加上可视区域的高度 等于或者大于滚动条的总高度就是到达底部
// console.log("已滚动到底部");
if (this.menuActive < i) {
this.menuActive = i;
}
} else {
this.menuActive = i;
}
break;
}
}
},
}
};
经查询得知,Vue组件在patch阶段结束时会把this.$el赋值为挂载的根dom元素,我们可以直接使用$el的querySelector, querySelectorAll等方法获取匹配的元素。因1中每个内容块子div已经绑定id,所以此处可以用 this.$el.querySelector("#xqItem" + i) 获取到每个子div。
还有一个要注意的是,这里之所以要判断是否滚动到了底部,是因为xq-box一旦滚动到底部,就可以看到最后几个目录对应的子div,此时的滚动距离scrollTop只会落在这最后几个子div的第一个子div(序号即当前本次循环中的i)的离顶部距离位置上,这个时候如果左侧目录树高亮的正好是这最后几个目录的其中任意一个,则无需更改高亮;但是如果此时 this.menuActive 的值还比最后几个目录的第一个子div的序号要小,即比本次循环的 i 要小,则需要更改为当前的 i 值。
4、如果要点击左边目录树,右边xq-box也要自动滚动到相应的目录内容,则要增加以下方法:
chooseMenu(name, index) {
this.menuActive = index;
// //可以用scrollIntoView
// document.querySelector("#xqItem" + index).scrollIntoView({
// block: "start",
// behavior: "smooth"
// });
let offsetTop = this.$el.querySelector("#xqItem" + index).offsetTop;
console.log("#xqItem" + index + " offsetTop: " + offsetTop);
this.xqBox.scrollTop = this.$el.querySelector(
"#xqItem" + index
).offsetTop;
},
这样,“监听这个xq-box滚动事件,右边一旦开始滚动,就要知道滚动到哪个子div,并让左边的目录树中对应的节点高亮显示”这个功能便实现了。
vue监听页面中的某个div的滚动事件,并判断滚动的位置的更多相关文章
- 监听页面中的某个div的滚动事件,并将其滚动距离保存到cookie
在html中,写一个id为type的div: <div class="type" id="type"></div> css: .type ...
- vue 监听页面宽度变化 和 键盘事件
vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { scr ...
- vue监听页面大小变化重新刷新布局
在项目中由于某些div元素在布局的时候需要初始化宽高,因为当浏览器缩小屏幕的时候需要重新刷新页面视图. 分析思路: 1.在store中创建state,用于保存当前浏览器的宽.高值. 2.在mounte ...
- vue监听数组中某个属性,计算其他属性问题
今天在项目开发中遇到一个根据数组中某个属性变化同时更新另一个属性变化的问题,刚开始代码如下 this.weekList1=r.data.roomProducts; this.weekList1.map ...
- vue 监听store中的数值
computed: { isFollow () { return this.$store.state.demo.id; //需要监听的数据 } }, watch: { isFo ...
- js给页面添加滚动事件并判断滚动方向
<script> var scrollFunc = function (e) { var direct = 0; e = e || window.event; if (e.wheelDel ...
- Vue - 监听页面刷新和关闭
一,在 created中 注册 页面刷新和关闭事件 created() { window.addEventListener('beforeunload', e => this.test(e)) ...
- vue中监听页面滚动和监听某元素滚动
①监听页面滚动 在生命周期mounted中进行监听滚动: mounted () { window.addEventListener('scroll', this.scrollToTop) }, 在方法 ...
- vue 中监听页面滚动
监听页面滚动 在methods中定义一个方法 handleScroll() { //获取滚动时的高度 let scrollTop = window.pageYOffset || document.do ...
随机推荐
- VC 获取多个mac地址
转载请注明来源:https://www.cnblogs.com/hookjc/ #include <IPHlpApi.h>#include <iostream>#pragma ...
- python基础1-类属性和实例属性
类属性就是类对象所拥有的属性,它被所有类对象的实例对象所共有,在内存中只存在一个副本,这个和C++中类的静态成员变量有点类似.对于公有的类属性,在类外可以通过类对象和实例对象访问 类属性 class ...
- 一款免费在线转pdf的工具 和 window免费镜像
PDF爱好者的在线工具 完全免费的PDF文件在线管理工具,其功能包括:合并PDF文件.拆分PDF文件.压缩PDF文件.Office文件转换为PDF文件.PDF文件转换为JPG图片.JPG图片转换为PD ...
- 4、前端--浮动、定位、是否脱离文档流、溢出属性、z-index、透明度、JavaScript简介
浮动 # ps:html代码时没有缩进一说的 全部写在一行也可以 """浮动主要就是用于页面布局的!!!""" # 浮动带来的负面影响 &q ...
- Solution -「APIO/CTSC 2007」「洛谷 P3620」数据备份
\(\mathcal{Description}\) Link. 给定升序序列 \(\{x_n\}\) 以及整数 \(k\),在 \(\{x_n\}\) 中选出恰 \(k\) 对 \((x_i, ...
- verification TLM传输数据导致多线程访问同一个数据
TLM传输数据导致多线程访问同一个数据 原因 TLM发送数据跟mailbox类似,都是发送的引用,这样发送端和接收端的引用都指向同一个数据,这样就会出现发送端修改数据会影响到接收端,比如发送的时候数据 ...
- Linux性能优化之磁盘I/O调优
I/O指标已介绍,那么如何查看系统的这些指标呢? 一.根据工具查性能 二.根据性能找工具 三.磁盘I/O观察实例 iostat 是最常用的磁盘 I/O 性能观测工具,它提供了每个磁盘的 使用率 . I ...
- Android SugarORM(3)
Android Sugar ORM (3) Android Sugar ORM 查询 我们在此之前介绍了一些关于Sugar ORM的简单操作, 现在我们就查询来具体说一下 Sugar ORM中的fin ...
- gulp更新4.0后的报错(gulp报Did you forget to signal async completion?)
本文首发于青云工作室 原文链接为 https://qystudio.ltd/posts/55153.html 缘起 今天我升级了gulp到4.0,在git三件套之后,网站并没有更新,我便登录了gith ...
- node / npm安装、启动报错
1. 系统禁止运行脚本 a. 在系统中找到Windos PowerShell[可以按win健,然后搜powershell]--以管理员身份打开 b. 在打开的窗口输入set-ExecutionPoli ...