简体   繁体   English

将鼠标悬停在图像上会更改不同的 div

[英]Hovering over an image changes different div

so I have 3 images next to each other and a text box below.所以我有 3 张图像并排在一起,下面有一个文本框。 I have default text and when I hover over one of the images, I want it to change that text to something else, and then when I hover over the 2nd image it does the same but with different text etc. I'm unsure on how to do this, so any help will be very appreciated!我有默认文本,当我将鼠标悬停在其中一个图像上时,我希望它将该文本更改为其他内容,然后当我将鼠标悬停在第二个图像上时,它的作用相同,但文本不同等。我不确定如何要做到这一点,所以任何帮助将不胜感激!

Here's my HTML:这是我的 HTML:

 <div class="mak-item"> <div class="mak-item-container"> <img data-tilt data-tilt-scale="1.1" class="makitem1" src="images/mak-item-1.png"> <img data-tilt data-tilt-scale="1.1" class="makitem2" src="images/mak-item-2.png"> <img data-tilt data-tilt-scale="1.1" class="makitem3" src="images/mak-item-3.png"> </div> </div> <div class="mak-item-text"> <div class="mak-item-text-wrapper fade-in"> <div id="default-text"> <p class="mak-item-name">Item Name</p> <br> <p class="mak-subtypes">Subtypes</p> <br> <br> <p class="mak-cooldown">Cooldown</p> <p class="mak-cost">Cost</p> <p class="mak-size">Size</p> <br> <br> <p class="mak-effect-heading">Effect</p> <p class="mak-effect">Effect Here</p> <br> <p class="mak-passive-heading">Passive</p> <p class="mak-passive">Passive Here</p> </div> </div> </div>

With this solution, on image hover the desired text will be displayed.使用此解决方案,在图像悬停时将显示所需的文本。

Try this:尝试这个:

 const img = document.querySelectorAll('img'); const text = document.querySelectorAll('.text') img.forEach((element) => { element.addEventListener('mouseover', () => { switch (element.id) { case "img1": text[0].style.display = "block"; break; case "img2": text[1].style.display = "block"; break; case "img3": text[2].style.display = "block"; break; } }) element.addEventListener('mouseout', () => { switch (element.id) { case "img1": text[0].style.display = "none"; break; case "img2": text[1].style.display = "none"; break; case "img3": text[2].style.display = "none"; break; } }) });
 .text { display: none; }
 <div> <img src="na" id="img1"> <img src="na" id="img2"> <img src="na" id="img3"> </div> <div class="text"> <p> This is the first image text </p> </div> <div class="text"> <p> This is the second image text </p> </div> <div class="text"> <p> This is the three image text </p> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM