Javascript: How do i target selected elements in a slideshow when hovering over one element?

Matt

Alright, so I'm working on my own javascript slideshow which consist of cards. Right now I'm adding/looping through the cards and adding an eventlistener (mouseover and mouseout) to check if the user hovered over chosen card.

Now to the problem. I need to be able to target all of the cards (part 1, see image) which comes before the chosen card of the user and also all of the cards (part 2, see image) which comes after. But I have to target them individually. Basically the cards in part 1 will get one kind of styling and the cards in part 2 will get another one. The chosen card will get its own styling.

Image example

This is what I have so far. If someone could point me in the right direction, that would be great, thanks. I don't want to use any libraries, strictly javascript.

var cards = [];
cards = document.querySelectorAll('.card');
for (var i = 0; i < cards.length; i++) {
  cards[i].addEventListener('mouseover', function() {
    //Do something
    console.log('Mouseover: Do something');
  });

  cards[i].addEventListener('mouseout', function() {
    //Do something
    console.log('Mouseout: Do something');
  });
}
.container {
  list-style: none;
  margin: 0px;
  padding: 0px;
}

.card {
  display: inline-block;
  background-color: #fff2cc;
  width: 100px;
  height: 150px;
  color: #000;
  text-align: center;
}
<ul class="container">
  <li class="card">Card-1</li>
  <li class="card">Card-2</li>
  <li class="card">Card-3</li>
  <li class="card">Card-4</li>
  <li class="card">Card-5</li>
</ul>

Ucho

You can set class for group/part 1, current card and group/part 2 separately.

You can possibly also listen to bubbling phase of event instead of multiple listener registration.

Check.

let ul = document.querySelectorAll('ul')[0];
ul.addEventListener('mouseover', function(e) {  
  
  if(e.target.className.indexOf("card") === -1) { return; }

  let currentFound = false;  
  document.querySelectorAll('.card').forEach(function(card) {
  
    if(card === e.target) { 
      card.classList.add("current-card");
      currentFound = true;
    }
    else
    if(currentFound) {
      card.classList.add("next-cards");
    } 
    else {
       card.classList.add("previous-cards");
    }});
});  


ul.addEventListener('mouseout', function() {
  document.querySelectorAll('.card').forEach(
    function(card) {
      card.classList.remove("previous-cards");
      card.classList.remove("next-cards"); 
      card.classList.remove("current-card");});
  });
.container {
  list-style: none;
  margin: 0px;
  padding: 0px;
}

.card {
  display: inline-block;
  background-color: #fff2cc;
  width: 100px;
  height: 150px;
  color: #000;
  text-align: center;
}

.previous-cards {
  background-color: crimson;
}

.next-cards {
  background-color: darkred;
}

.current-card {
  background-color: indianred;
}
<ul class="container">
  <li class="card">Card-1</li>
  <li class="card">Card-2</li>
  <li class="card">Card-3</li>
  <li class="card">Card-4</li>
  <li class="card">Card-5</li>
</ul>

If you would like to preserve the colors until next hovering just remove the mouseout listener and put its logic to start of mouseover listener right after if block.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I style two elements into different styles by hovering over one of them? Without JavaScript

How can I change one svg path ID element when hovering over another element

How to stop the elements from growing when hovering over an element?

How to change the style of other elements when hovering over one of them?

How do I trigger a click when hovering over a element for x seconds, using JQuery?

How do I change the class of several tds when hovering over one of them?

How do I display the value of a heatmap element by hovering the cursor over it?

How do I remove the space when hovering over a nav link?

How do I stop sound on mouseout when hovering over an image?

How do I show data on one card when hovering and not on all?

How to use tailwindcss to change the color of each element when hovering over descendant elements

How do I Center Images for a Javascript Slideshow when the width is unknown?

Only fire one onmouseover when hovering over multiple elements

How do I stop my <li> elements from moving while hovering over another <li>?

How to change color of element when hovering over another element?

How would I show text inside a span tag when hovering over a different element?

How do add a class to another div, when hovering over one div?

How Do I Scroll All Elements To Target Element

In Labview how can I change the mouse pointer when hovering over one of my boolean controls?

How do I push the text when I am hovering over an image?

How do I change the active <li> element when a new one is selected?

How to change a plot when hovering over elements in Shiny?

How to trigger two hovers when hovering over single element CSS

How to scroll up a div when hovering over another element

In a network graph, how do I highlight the network components when hovering over them?

How do I remove the zoom icon that appears when hovering over image?

How do I display the value of the square's id when hovering over it only?

How do I put a keyframe animation when hovering over an unordered list factor

How do I add a class while hovering over but not remove it after unhover using javascript?