英文:
How do I make the child index reset to 0 for each new parent section?
问题
我正在使用项目的“index”来为我的网站上的元素添加动画延迟。
以下是HTML和JS代码:
<div>
<div class="section section-1">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
</div>
<div class="section section-2">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
<div class="item item-3">
</div>
<div class="item item-4">
</div>
</div>
</div>
const AnimatedElements = document.querySelectorAll(".section .item");
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', (index * 250) + "ms");
});
现在,这确实起作用。然而,我遇到的问题是JS将项目索引视为一个数组(即[0, 1, 2, 3, 4, 5]),而不是它们各自部分内的两个单独的数组(即第1部分[0, 1],第2部分[0, 1, 2, 3])。
是否有办法让索引在每个新部分中重置为0?否则,随着页面滚动越远,动画延迟会变得越来越长。
编辑:我已经解决了问题。但是,现在我已将JS应用于部分内的列以及项目,我现在遇到了与以前相同的问题。
以下是更新后的HTML和JS:
<div>
<div class="section section-1">
<div class="column column-1">
<div class="item item-1">
</div>
</div>
<div class="column column-2">
<div class="item item-2">
</div>
</div>
</div>
<div class="section section-2">
<div class="column column-1">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
<div class="item item-3">
</div>
</div>
<div class="column column-2">
<div class="item item-4">
</div>
</div>
</div>
</div>
const Sections = document.querySelectorAll(".section");
const Columns = document.querySelectorAll(".section .column");
Columns.forEach(Column => {
const AnimatedElements = Column.querySelectorAll(".item");
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
Sections.forEach(Section => {
const AnimatedColumns = Section.querySelectorAll(".column");
AnimatedColumns.forEach((AnimatedColumn, index) => {
AnimatedColumn.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
如前所述,问题与以前完全相同,即JS将列索引视为一个数组(即[0, 1, 2, 3]),而不是它们各自部分内的两个单独的数组(即第1部分[0, 1],第2部分[0, 1])。
如有任何帮助解决此问题的方法,将不胜感激。谢谢!
英文:
I'm using the index
of an item to add an animation delay to elements on my website.
Here's the HTML and JS
<div>
<div class="section section-1">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
</div>
<div class="section section-2">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
<div class="item item-3">
</div>
<div class="item item-4">
</div>
</div>
</div>
const AnimatedElements = document.querySelectorAll(".section .item");
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', ([index] * 250) + "ms");
});
Now, this does work. However, The issue I am having is the JS reads the item index as one Array (I.E [0, 1, 2, 3, 4, 5]) rather than two separate arrays within their respective sections (I.E section 1 [0, 1], Section 2 [0, 1, 2, 3]).
Is there any way I can make the index reset to 0 for each new section? As otherwise the animation delay just gets longer and longer the further you scroll down the page.
Edit: I've managed to get the issue fix. However, I have now applied the JS to the columns within the section as well as the items and I am now experiencing the same issue as before.
Here's the updated HTML and JS
<div>
<div class="section section-1">
<div class="column column-1">
<div class="item item-1">
</div>
</div>
<div class="column column-2">
<div class="item item-2">
</div>
</div>
</div>
<div class="section section-2">
<div class="column column-1">
<div class="item item-1">
</div>
<div class="item item-2">
</div>
<div class="item item-3">
</div>
</div>
<div class="column column-2">
<div class="item item-4">
</div>
</div>
</div>
</div>
const Sections = document.querySelectorAll(".section");
const Columns = document.querySelectorAll(".section .column");
Columns.forEach(Column => {
const AnimatedElements = Column.querySelectorAll(".item");
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
Sections.forEach(Section => {
const AnimatedColumns = Section.querySelectorAll(".column");
AnimatedColumns.forEach((AnimatedColumn, index) => {
AnimatedColumn.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
As said before the issue is doing the exact same thing that it did before where the JS reads the column index as one Array (I.E [0, 1, 2, 3]) rather than two separate arrays within their respective sections (I.E section 1 [0, 1], Section 2 [0, 1]).
Any help to fix this issue would be greatly appreciated. Thanks
答案1
得分: 2
请使用嵌套循环。外部循环用于遍历各个部分,内部循环用于遍历各个项。每个部分中的项索引将从0开始。
const sections = document.querySelectorAll(".section");
sections.forEach(section => {
const AnimatedElements = section.querySelectorAll(".item");
console.log([...AnimatedElements]);
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
<div>
<div class="section section-1">
<div class="item item-1">1A</div>
<div class="item item-2">1B</div>
</div>
<div class="section section-2">
<div class="item item-1">2A</div>
<div class="item item-2">2B</div>
<div class="item item-3">2C</div>
<div class="item item-4">2D</div>
</div>
</div>
英文:
Use nested loops. The outer loop is for the sections, the inner loop is for the items. The item indexes will start at 0 in each section.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const sections = document.querySelectorAll(".section");
sections.forEach(section => {
const AnimatedElements = section.querySelectorAll(".item");
console.log([...AnimatedElements]);
AnimatedElements.forEach((AnimatedElement, index) => {
AnimatedElement.style.setProperty('--index-delay', (index * 250) + "ms");
});
});
<!-- language: lang-html -->
<div>
<div class="section section-1">
<div class="item item-1">1A
</div>
<div class="item item-2">1B
</div>
</div>
<div class="section section-2">
<div class="item item-1">2A
</div>
<div class="item item-2">2B
</div>
<div class="item item-3">2C
</div>
<div class="item item-4">2D
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论