简体   繁体   English

如何迭代 HTML 数组<div>元素并为每个索引号对另一个元素设置不同的样式?

[英]How to iterate an array of HTML <div> elements and style another element differently for each index number?

I would like the content value of a div 's pseudo-element ::after to change following the index number of 6 child div elements, on hover over each of these 6 divs.我希望div的伪元素::aftercontent值在 6 个子div元素的索引号::after更改,悬停在这 6 个 div 中的每一个上。

I am stuck at creating a first array of the 6 divs and another array containing the 6 infos to display with the same corresponding index number.我坚持创建 6 个 div 的第一个数组和另一个包含 6 个信息的数组,以使用相同的相应索引号显示。

Array #1 would be [.hov-sq:nth-child(1), .hov-sq:..] and array #2 would be the content for the 'data-content' attribute to change at each hover ['digital nomad','digital developer','superman','etc...]数组 #1 将是 [.hov-sq:nth-child(1), .hov-sq:...] 数组 #2 将是 'data-content' 属性的内容,在每次悬停 ['digital游牧者','数字开发者','超人','等等...]

So far I managed to change the pseudo-element content using this jQuery code and CSS code.到目前为止,我设法使用此 jQuery 代码和 CSS 代码更改了伪元素内容。

 $('.hov-sq').hover(function() { $('.c-1').attr('data-content', 'frontend developer'); });
 .landing-hov-s { width: 100vw; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; } .hov-sq { width: 33.3333333vw; height: 50vh; z-index: 5000; } .c-1::after { /* other styling not relevant to issue */ content: attr(data-content) ''; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="landing-hov-s"> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> </div> <div class="c-1"> <h1>Laurent<br>&nbsp;&nbsp;&nbsp;Chevrette</h1> </div>

The page is live here for a proper display: http://vmax.laurentchevrette.digital/该页面在这里正确显示: http : //vmax.laurentchevrette.digital/

Use index() to determine the index of which element within the collection is being hovered使用index()来确定集合中哪个元素被悬停的索引

 const content = ['Item 1','Item 2','Item 3','Item 4','Item 5','Item 6']; const $sq = $('.hov-sq').hover(function() { const idx = $sq.index(this); $('.c-1').attr('data-content', content[idx]); }, function(){ // remove when mouse leaves if wanted $('.c-1').attr('data-content','') });
 .landing-hov-s { width: 100vw; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; } .hov-sq { width: 33.3333333vw; height: 50vh; z-index: 5000; } .c-1::after { /* other styling not relevant to issue */ content: attr(data-content) ''; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="landing-hov-s"> <div class="hov-sq">One</div> <div class="hov-sq">Two</div> <div class="hov-sq">Three</div> <div class="hov-sq">Four</div> <div class="hov-sq">Five</div> <div class="hov-sq">Six</div> </div> <div class="c-1"> <h1>Laurent<br>&nbsp;&nbsp;&nbsp;Chevrette</h1> </div>

jQuery's index method jQuery 的索引方法

If you are using jQuery, you have access to the index() method .如果您使用 jQuery,则可以访问index()方法

Also I've added the function that will run once mouse is out of the hovered div.此外,我还添加了鼠标out悬停 div 后将运行的功能。

That said, be wary that you cannot select pseudoelements y Javascript, so you should use attribute selectors in CSS as shown belown to style them.也就是说,请注意您不能选择伪元素 y Javascript,因此您应该在 CSS 中使用属性选择器,如下所示来设置它们的样式。

 $('.hov-sq').hover(function() { $('.c-1') .attr('data-content', 'frontend developer') .attr('data-index', $(this).index()); console.log($(this).index()) }, function() { $('.c-1') .attr('data-content', '') .attr('data-index', ''); });
 .landing-hov-s { width: 100vw; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; } .hov-sq { width: 33.3333333vw; height: 50vh; z-index: 5000; } .c-1::after { /* other styling not relevant to issue */ content: attr(data-content) ''; } .c-1[data-index="0"]::after { /* Style for index 0 */ } /* Rest of the styles */
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="landing-hov-s"> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> <div class="hov-sq"></div> </div> <div class="c-1"> <h1>Laurent<br>&nbsp;&nbsp;&nbsp;Chevrette</h1> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM