javascript - 在滚动时获取多个 div 的数据 ID
问题描述
问题
我有一系列 div,每个都有自己的data-id
,当用户滚动到该 div 时,我试图获取该 id。目前,该行为仅适用于该系列中的第一个,但不适用于所有十个。
客观的
- 当用户向下滚动页面时,确定他们的位置,以便我可以从他们正在查看
data-id
的特定位置获取对应的位置。photo__group
- 根据他们滚动过去的 div,将一个类添加
is-active
到相应的dot__border
.
密码笔
https://codepen.io/onlyandrewn/pen/jpmjaE?editors=1010
脚本.js
$(function(){
$(window).scroll(function(){
getGroupID();
});
function getGroupID() {
var scrollPosition = $(window).scrollTop();
var group = $(".photo__group");
var groupID = group.attr("data-id");
var groupBottom = group.offset().top + group.outerHeight();
// If a user scrolls down the page and their scroll position is greater than the bottom of the photo group, update the corresponding progress dots.
if (scrollPosition > groupBottom) {
updateProgressDots(groupID);
} else {
updateProgressDots(0);
}
}
// This function removes the `is-active` class from all `dot__border` elements, takes the groups `data-id` and adds a `is-active` class to that element in the series i.e. If the user is on the first group, then the first dot / dot border should have the `is-active` class.
function updateProgressDots(groupID) {
var dotBorder = $(".dot__border");
dotBorder.removeClass("is-active");
dotBorder.eq(groupID).addClass("is-active");
}
});
索引.html
<div class="dots">
<div class="dot__border is-active">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
</div>
<div class="photo__group" data-id="1">Photo group #1</div>
<div class="photo__group" data-id="2">Photo group #2</div>
<div class="photo__group" data-id="3">Photo group #3</div>
<div class="photo__group" data-id="4">Photo group #4</div>
<div class="photo__group" data-id="5">Photo group #5</div>
<div class="photo__group" data-id="6">Photo group #6</div>
<div class="photo__group" data-id="7">Photo group #7</div>
<div class="photo__group" data-id="8">Photo group #8</div>
<div class="photo__group" data-id="9">Photo group #9</div>
<div class="photo__group" data-id="10">Photo group #10</div>
解决方案
您必须使用 jQuery.each
来遍历所有匹配的元素。这是您的工作代码。
$(function(){
updateProgressDots(0);
$(window).scroll(function(){
getGroupID();
});
function getGroupID() {
var scrollPosition = $(window).scrollTop();
$(".photo__group").each(function(index){
var group = $(this);
var groupID = group.attr("data-id");
var groupBottom = group[0].offsetHeight+group[0].offsetTop;
// If a user scrolls down the page and their scroll position is greater than the bottom of the photo group, update the corresponding progress dots.
if (scrollPosition > groupBottom) {
updateProgressDots(groupID);
}
});
}
// This function removes the `is-active` class from all `dot__border` elements, takes the groups `data-id` and adds a `is-active` class to that element in the series i.e. If the user is on the first group, then the first dot / dot border should have the `is-active` class.
function updateProgressDots(groupID) {
var dotBorder = $(".dot__border");
dotBorder.removeClass("is-active");
dotBorder.eq(parseInt(groupID)).addClass("is-active");
}
});
body {
margin: 0;
}
.dots {
position: fixed;
right: 48px;
bottom: 48px;
}
.dot {
width: 5px;
height: 5px;
background: #000;
border-radius: 50%;
}
.dot__border {
border: 1px solid #000;
padding: 4px;
margin-bottom: 16px;
}
.dot__border.is-active {
border: 1px solid #c62828;
}
.dot__border.is-active .dot {
background: #c62828;
}
.photo__group {
width: 100vw;
height: 100vh;
border-bottom: 1px solid #c62828;
font-size: 48px;
font-family: "Open Sans";
font-weight: 700;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dots">
<div class="dot__border is-active">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
<div class="dot__border">
<div class="dot"></div>
</div>
</div>
<div class="photo__group" data-id="1">Photo group #1</div>
<div class="photo__group" data-id="2">Photo group #2</div>
<div class="photo__group" data-id="3">Photo group #3</div>
<div class="photo__group" data-id="4">Photo group #4</div>
<div class="photo__group" data-id="5">Photo group #5</div>
<div class="photo__group" data-id="6">Photo group #6</div>
<div class="photo__group" data-id="7">Photo group #7</div>
<div class="photo__group" data-id="8">Photo group #8</div>
<div class="photo__group" data-id="9">Photo group #9</div>
<div class="photo__group" data-id="10">Photo group #10</div>
推荐阅读
- .net - 来自子进程的 .NET Process.Start() 关闭事件
- swift - SwiftUI:导航到视图层次结构中的视图
- qt - Lubuntu 中的 Qt 虚拟键盘错误
- java - 当exec(i/j)和(i%j)时,jvm会做一两次计算?
- c# - 自定义 JsonConverter - ReadJson 永远不会被调用
- angular - 离子输入掩码不是已知元素
- flutter - 我可以在其他小部件的 PageView 小部件中使用“索引”吗?
- angular - 全局变量仍未定义
- java - Spring JDBC 调试日志未显示在控制台上
- python - 如何在python中检查文件夹中的新文件