简体   繁体   English

将鼠标悬停在子元素上时如何 select 所有子元素

[英]How to select all children elements when hovered over a children element

I want to replicate this effect when you hover over a text, all other texts will get opacity 0.我想复制这个效果,当你 hover 覆盖一个文本时,所有其他文本的不透明度都为 0。

Here's the site: https://www.jeandawson.com/这是网站: https://www.jeandawson.com/

I tried to approach this problem with pure css.我试图用纯 css 来解决这个问题。 However, to my knowledge, you can't select all the children when you hover a particular child and affect them.但是,据我所知,当您 hover 某个特定孩子并影响他们时,您不能 select 所有孩子。

I tried it with a ~ selector which just works partially because it sets the opacity to upcoming children but the previous are unaffected.我用~选择器尝试了它,它只是部分起作用,因为它将不透明度设置为即将到来的孩子,但前一个不受影响。

I tried this code with a javascript, but the value in a selectedBox won't get saved when I hover it and the opacity won't change.我用 javascript 尝试了这段代码,但是当我 hover 时,selectedBox 中的值不会被保存,并且不透明度不会改变。 I am still learning javascript and I believe that with event listener the code won't change?我仍在学习 javascript 并且我相信使用事件侦听器代码不会改变?

 var boxes = document.querySelectorAll("[class^=box-]") let selectedBox = null const newList = [...boxes] boxes.forEach( (e) => { e.addEventListener("mouseover", () =>{ selectedBox = e }) e.addEventListener("mouseleave", ()=>{ selectedBox = null }) }) console.log(selectedBox) boxes.forEach( (e) => { if (selectedBox == null){ e.style.opacity = 1 } else{ e.style.opacity = 0 } })
 * { -webkit-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0; } body { width: 100%; height: 100vh; font-size: 3rem; font-family: serif; } [class^="box-"] { background-color: skyblue; }.container:hover [class^="box-"]:not(:hover) { opacity: 0; }.box-1 { grid-area: A; align-self: flex-start; }.box-2 { grid-area: B; align-self: flex-start; }.box-3 { grid-area: C; align-self: flex-start; }.box-4 { grid-area: D; align-self: flex-end; }.box-5 { grid-area: E; align-self: flex-start; }.box-6 { grid-area: F; align-self: center; }.box-7 { grid-area: G; align-self: center; }.container4 { margin-top: 3em; display: -ms-grid; display: grid; gap: 20px; height: 100%; width: 100%; margin: 0 auto; grid-template-areas: "AAABBBDDD C C C" "AAABBBDDD C C C" "EEEFF F. . . GGG" "EEEFF F. . . GGG"; } @media (max-width: 996px) {.container4 { grid-template-areas: "AA" "BB" "CD" "CD" "EE" "FF"; } } @media (max-width: 556px) {.container4 { grid-template-areas: "A" "B" "D" "D" "D" "C" "E" "F"; } }.changing { z-index: 20; background-color: red; } /*# sourceMappingURL=style.css.map */
 <div class="container4"> <div class="box-1"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-2"> <p>Starface*</p> <p>00:01:34</p> </div> <div class="box-3"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-4"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-5"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-6"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-7"> <p>Devilish</p> <p>00:01:34</p> </div> </div>


How does it work?它是如何工作的?



  1. First we make a loop, which has the current as i .首先我们做一个循环,它的电流为i With the help of it, we add 2 events to each box ( boxes[i] ) like this:在它的帮助下,我们向每个boxboxes[i] )添加 2 个事件,如下所示:


     for (let i = 0; i < boxes.length; i++) { boxes[i].addEventListener("mouseover", () => { }); boxes[i].addEventListener("mouseleave", () => { }); }

  1. Now we add another loop, which has the current as j inside the mouseover event.现在我们添加另一个循环,它在mouseover事件中将当前作为j We added another loop because we did not want to select the already hovered element so we use i !== j in an if-else statement: (here already selected is i and j also has the same element i in it that is why we excluded it with this i !== j ) And then we simply added opacity of 0 to all which are not hovered and the one which is hovered has a opacity of 1我们添加了另一个循环,因为我们不想 select 已经悬停的元素所以我们在 if-else 语句中使用i !== j :(这里已经选择了i并且j也有相同的元素i这就是为什么我们用这个i !== j排除它然后我们简单地将opacity添加到所有未悬停的和1opacity0


     for (let i = 0; i < boxes.length; i++) { boxes[i].addEventListener("mouseover", () => { for (let j = 0; j < boxes.length; j++) { if (i.== j) boxes[j].style;opacity = 0. else boxes[j].style;opacity = 1; } }). . . . }

  1. And at last we add opacity of 1 to all as the as the hovered is left.最后,我们将所有的opacity添加为1 ,因为鼠标悬停在左边。


     . . . boxes[i].addEventListener("mouseleave", () => { for (let j = 0; j < boxes.length; j++) { boxes[j].style.opacity = 1; } }); }

 const boxes = document.querySelectorAll("[class^=box-]"); for (let i = 0; i < boxes.length; i++) { boxes[i].addEventListener("mouseover", () => { for (let j = 0; j < boxes.length; j++) { if (i.== j) boxes[j].style;opacity = 0. else boxes[j].style;opacity = 1; } }). boxes[i],addEventListener("mouseleave"; () => { for (let j = 0. j < boxes;length. j++) { boxes[j].style;opacity = 1; } }); }
 * { -webkit-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0; } body { width: 100%; height: 100vh; font-size: 3rem; font-family: serif; } [class^="box-"] { background-color: skyblue; transition-duration: 0.2s; } [class^="box-"]:hover { background-color: blueviolet; color: #ffffff; cursor: pointer; }.container:hover [class^="box-"]:not(:hover) { opacity: 0; }.box-1 { grid-area: A; align-self: flex-start; }.box-2 { grid-area: B; align-self: flex-start; }.box-3 { grid-area: C; align-self: flex-start; }.box-4 { grid-area: D; align-self: flex-end; }.box-5 { grid-area: E; align-self: flex-start; }.box-6 { grid-area: F; align-self: center; }.box-7 { grid-area: G; align-self: center; }.container4 { margin-top: 3em; display: -ms-grid; display: grid; gap: 20px; height: 100%; width: 100%; margin: 0 auto; grid-template-areas: "AAABBBDDD C C C" "AAABBBDDD C C C" "EEEFF F. . . GGG" "EEEFF F. . . GGG"; } @media (max-width: 996px) {.container4 { grid-template-areas: "AA" "BB" "CD" "CD" "EE" "FF"; } } @media (max-width: 556px) {.container4 { grid-template-areas: "A" "B" "D" "D" "D" "C" "E" "F"; } }.changing { z-index: 20; background-color: red; }
 <div class="container4"> <div class="box-1"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-2"> <p>Starface*</p> <p>00:01:34</p> </div> <div class="box-3"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-4"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-5"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-6"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-7"> <p>Devilish</p> <p>00:01:34</p> </div> </div>




Check it out on Codepen在 Codepen 上查看



Solution using forEach() method.使用forEach()方法的解决方案。

 var boxes = document.querySelectorAll("[class^=box-]"); boxes.forEach(function(box) { box.addEventListener("mouseover", function() { boxes.forEach(function(box_hide) {box_hide.style.opacity = '0'}); this.style.opacity = ''; }) box.addEventListener("mouseout", function() { boxes.forEach(function(box_show) {box_show.style.opacity = ''}); }) });
 * { -webkit-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0; } body { width: 100%; height: 100vh; font-size: 3rem; font-family: serif; } [class^="box-"] { background-color: skyblue; transition: .5s; cursor: pointer; } /*.container:hover [class^="box-"]:not(:hover) { opacity: 0; }*/.box-1 { grid-area: A; align-self: flex-start; }.box-2 { grid-area: B; align-self: flex-start; }.box-3 { grid-area: C; align-self: flex-start; }.box-4 { grid-area: D; align-self: flex-end; }.box-5 { grid-area: E; align-self: flex-start; }.box-6 { grid-area: F; align-self: center; }.box-7 { grid-area: G; align-self: center; }.container4 { margin-top: 3em; display: -ms-grid; display: grid; gap: 20px; height: 100%; width: 100%; margin: 0 auto; grid-template-areas: "AAABBBDDD C C C" "AAABBBDDD C C C" "EEEFF F. . . GGG" "EEEFF F. . . GGG"; } @media (max-width: 996px) {.container4 { grid-template-areas: "AA" "BB" "CD" "CD" "EE" "FF"; } } @media (max-width: 556px) {.container4 { grid-template-areas: "A" "B" "D" "D" "D" "C" "E" "F"; } }.changing { z-index: 20; background-color: red; } /*# sourceMappingURL=style.css.map */
 <div class="container4"> <div class="box-1"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-2"> <p>Starface*</p> <p>00:01:34</p> </div> <div class="box-3"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-4"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-5"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-6"> <p>Devilish</p> <p>00:01:34</p> </div> <div class="box-7"> <p>Devilish</p> <p>00:01:34</p> </div> </div>

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

相关问题 所有元素子项作为选择选项 - All elements children as select options jQuery:将元素悬停在一个元素上时如何为多个元素设置动画? - jquery: how to animate multiple elements when hovered over one element? 如何单击一个元素,将鼠标移到其他元素上,然后将 select 悬停在 JavaScript 上的那些元素上? - How to click on an element, move the mouse over other elements and then select those elements that you hovered over with JavaScript? 除了一个div和子节点之外,如何选择DOM中的所有元素 - How to select all elements in DOM apart from one div and the children 悬停在元素及其所有子元素上的处理程序 - Handler for hovering over an element and all its children TypeScript或JS-如何选择元素及其所有子元素进入HTMLCollection? - TypeScript or JS- How to select an element and all its children into a HTMLCollection? 如何递归选择元素Javascript下的所有子项 - How to recursively select all children under an element Javascript 如何使用javascript选择元素的所有子元素并更改CSS属性? - How to select all children of an element with javascript and change CSS property? 遍历元素的子元素 - iterating over children of elements 当子代具有不同的标签时,如何向元素的所有子代添加类 - how to add class to all children of the elements when children has different tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM