如何使子索引在每个新的父节段中重置为0?

huangapple go评论135阅读模式
英文:

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

&lt;div&gt;
     &lt;div class=&quot;section section-1&quot;&gt;
          &lt;div class=&quot;item item-1&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;item item-2&quot;&gt;
          &lt;/div&gt;
     &lt;/div&gt;
     &lt;div class=&quot;section section-2&quot;&gt;
          &lt;div class=&quot;item item-1&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;item item-2&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;item item-3&quot;&gt;
          &lt;/div&gt;
          &lt;div class=&quot;item item-4&quot;&gt;
          &lt;/div&gt;
     &lt;/div&gt;
&lt;/div&gt;
const AnimatedElements = document.querySelectorAll(&quot;.section .item&quot;);

AnimatedElements.forEach((AnimatedElement, index) =&gt; {
	AnimatedElement.style.setProperty(&#39;--index-delay&#39;, ([index] * 250) + &quot;ms&quot;);
});

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

&lt;div&gt;
     &lt;div class=&quot;section section-1&quot;&gt;
          &lt;div class=&quot;column column-1&quot;&gt;
               &lt;div class=&quot;item item-1&quot;&gt;
               &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;column column-2&quot;&gt;
               &lt;div class=&quot;item item-2&quot;&gt;
               &lt;/div&gt;
          &lt;/div&gt;
     &lt;/div&gt;
     &lt;div class=&quot;section section-2&quot;&gt;
          &lt;div class=&quot;column column-1&quot;&gt;
               &lt;div class=&quot;item item-1&quot;&gt;
               &lt;/div&gt;
               &lt;div class=&quot;item item-2&quot;&gt;
               &lt;/div&gt;
               &lt;div class=&quot;item item-3&quot;&gt;
               &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;column column-2&quot;&gt;
               &lt;div class=&quot;item item-4&quot;&gt;
               &lt;/div&gt;
          &lt;/div&gt;
     &lt;/div&gt;
&lt;/div&gt;
const Sections = document.querySelectorAll(&quot;.section&quot;);
const Columns = document.querySelectorAll(&quot;.section .column&quot;);

Columns.forEach(Column =&gt; {
	const AnimatedElements = Column.querySelectorAll(&quot;.item&quot;);
	AnimatedElements.forEach((AnimatedElement, index) =&gt; {
		AnimatedElement.style.setProperty(&#39;--index-delay&#39;, (index * 250) + &quot;ms&quot;);
	});
});

Sections.forEach(Section =&gt; {
	const AnimatedColumns = Section.querySelectorAll(&quot;.column&quot;);
	AnimatedColumns.forEach((AnimatedColumn, index) =&gt; {
		AnimatedColumn.style.setProperty(&#39;--index-delay&#39;, (index * 250) + &quot;ms&quot;);
	});
});

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(&quot;.section&quot;);

sections.forEach(section =&gt; {
  const AnimatedElements = section.querySelectorAll(&quot;.item&quot;);
  console.log([...AnimatedElements]);
  AnimatedElements.forEach((AnimatedElement, index) =&gt; {
    AnimatedElement.style.setProperty(&#39;--index-delay&#39;, (index * 250) + &quot;ms&quot;);
  });
});

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div class=&quot;section section-1&quot;&gt;
    &lt;div class=&quot;item item-1&quot;&gt;1A
    &lt;/div&gt;
    &lt;div class=&quot;item item-2&quot;&gt;1B
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;section section-2&quot;&gt;
    &lt;div class=&quot;item item-1&quot;&gt;2A
    &lt;/div&gt;
    &lt;div class=&quot;item item-2&quot;&gt;2B
    &lt;/div&gt;
    &lt;div class=&quot;item item-3&quot;&gt;2C
    &lt;/div&gt;
    &lt;div class=&quot;item item-4&quot;&gt;2D
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 05:28:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定