[英]Javascript: How do i target selected elements in a slideshow when hovering over one element?
Alright, so I'm working on my own javascript slideshow which consist of cards. 好吧,所以我正在制作自己的由卡片组成的javascript幻灯片。 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. 我需要能够定位用户选择的所有卡之前的所有卡(第1部分,请参见图片)以及之后的所有卡(第2部分,请参见图像)。 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. 基本上,第1部分中的卡片将获得一种样式,而第2部分中的卡片将获得另一种样式。 The chosen card will get its own styling. 所选卡将具有其自己的样式。
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. 我不想使用任何库,严格来说是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>
I would do this with CSS and it's sibling selector: 我会使用CSS及其同级选择器:
.card {
background-color: red;
}
.card:hover ~ .card {
background-color: green;
}
If you need to use JavaScript, use [...mouseEnterCard.parentElement.children].indexOf(mouseEnterCard)
to get the element index and then loop over the elements with a lower/higher index. 如果需要使用JavaScript,请使用[...mouseEnterCard.parentElement.children].indexOf(mouseEnterCard)
获取元素索引,然后使用较低/较高的索引循环遍历元素。
You can select the particular card and apply class name using jquery. 您可以选择特定的卡,并使用jquery应用类名称。
var cards = []; cards = document.querySelectorAll('.card'); for (var i = 0; i < cards.length; i++) { cards[i].addEventListener('mouseover', function() { //Do something $(this).addClass('selected'); console.log('Mouseover: Do something'); }); cards[i].addEventListener('mouseout', function() { //Do something $(this).removeClass('selected'); 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; } .selected{ background-color: blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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>
you can even use simple css which will be comman for all the card. 您甚至可以使用所有卡片通用的简单CSS。
when card class is hovered this css will execute. 当卡类悬停时,此CSS将执行。
.card:hover{
background-color: blue;
}
You can set class for group/part 1, current card and group/part 2 separately. 您可以分别为组/第1部分,当前卡和组/第2部分设置类别。
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. 如果您想保留颜色直到下一次悬停,只需删除mouseout侦听器,并将其逻辑放在if块之后立即开始mouseover侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.