笔记系列之——页面滚动时,即时获取离可视区顶部最近的元素
作者:
临安linan | 来源:发表于
2019-03-31 17:05 被阅读0次
- 给需要进行比较判断的元素标签上加上特殊属性(eg.tag)
- let tag = document.querySelectorAll("[tag]");
- 监听滚动事件
// (xxx.getBoundingClientRect().top) 可以获取元素距离可视区顶部的距离
window.onscroll = function () {
let index = 0;
for (let i = 0; i < tag.length; i++) {
if (Math.abs(tag[i].getBoundingClientRect().top) < Math.abs(tag[index].getBoundingClientRect().top)){
index = i;
}
}
// tag[i]就是所找的元素
}
本文标题:笔记系列之——页面滚动时,即时获取离可视区顶部最近的元素
本文链接:https://www.haomeiwen.com/subject/gwaybqtx.html
网友评论