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

LaurentC

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.

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.

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...]

So far I managed to change the pseudo-element content using this jQuery code and CSS code.

$('.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/

charlietfl

Use index() to determine the index of which element within the collection is being hovered

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Iterate each element row and compare with another elements in row

Print elements of array with strings and number with each element

how to iterate through matrix array to count the number of similar elements surrounding a particular element inside the matrix

Can't remove specific elements from array by index number from dynamic div element in react?

How to style each element inside the array with css?

How to iterate in each element?

How to use an array element as an index of another array

Rounding elements in numpy array to the number of the same index in another array

How to compare each element of array to other elements?

Iterate on the elements of nested list at particular index together with index of element of another normal list

How do I randomly get a certain number of elements of a numpy array with at least one element from each class?

How can I insert each of an arrays elements every other element in another array?

How to multiply elements in an array with each elements in another array using Python

how to get index of element in array after a sum equals some number of previous elements

How to count each element number in array in javascript

How to replace elements from array with another element

How to multiply elements of an array by elements from another array with the same index?

How do I make each each element in array another array?

How to Style each sentence differently to one placeholder

How to iterate through an array where each iteration is a subarray instead of an element

How to iterate over an array of object then remove each element in JavaScript

MongoDB update each element of an embedded array differently

How do add elements from array into div element where id is equal with array element dealState number in vue.js

How to iterate the div items in HTML, and assign the value to each one?

jQuery iterate through elements within another element

How do I iterate the array, then show all the elements in HTML?

How to number each element in column conditionally on elements in other column in a dataset

javascript - How to merge each element in array with other elements in different array?

How to get array index from html element?