[英]Hide siblings on hover javascript
I'm trying to write a function that watches an element and when hovered or focused on, hides it's siblings. 我正在尝试编写一个监视元素的函数,当将其悬停或关注时,将其隐藏为兄弟姐妹。
I've written this 我写了这个
var hotSpotDiv = document.getElementsByClassName('custom-hotspot');
hotSpotDiv.addEventListener('mouseenter', function(e) {
this.classList.add('active');
})
hotSpotDiv.addEventListener('mouseleave', function(e) {
this.classList.remove('active');
})
Which works but I'm stuck trying to think of how I can hide the elements that do not have the active
class added on hover. 哪个可行,但我一直试图思考如何隐藏悬停时未添加
active
类的元素。
This code is ...not great.. and I made some presumptions about your scenario, but it should hopefully give you a good jumping off point. 这段代码不是很好。我对您的情况作了一些假设,但希望它可以给您一个很好的起点。
var hotSpotDivs = document.getElementsByClassName('custom-hotspot'); var i; function hotspotMouseEnter(e) { this.classList.add('active'); this.parentNode.classList.add('active'); } function hotspotMouseLeave(e) { this.classList.remove('active'); this.parentNode.classList.remove('active'); } for (i=0; i<hotSpotDivs.length; i++) { hotSpotDivs[i].addEventListener('mouseenter', hotspotMouseEnter); hotSpotDivs[i].addEventListener('mouseleave', hotspotMouseLeave); }
.the-container.active>.custom-hotspot { visibility: hidden; } .the-container.active>.custom-hotspot.active { visibility: visible; }
<div class="the-container"> <p class="custom-hotspot">hotspot 1</p> <p class="custom-hotspot">hotspot 2</p> <p class="custom-hotspot">hotspot 3</p> <p class="custom-hotspot">hotspot 4</p> <p class="custom-hotspot">hotspot 5</p> <p class="custom-hotspot">hotspot 6</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.