简体   繁体   中英

Apply CSS class on hover to other element

I've this HTML structure:

 <div class="container"> <div class="list-wrapper"> <ul> <li class="item-first"> </li> </ul> </div> <div class="description-wrapper"> <div class="description-first"></div> </div> </div>

So is it possible to set the .description-first to opacity: 1; when hovering .item-first ? Or is this just possible with JS? Thanks for your help!

As Strebler said, this is only possible with Javascript.

 function MouseOver(value) { document.getElementsByClassName("description-first")[0].setAttribute("style", "opacity: " + value + ";"); }
 .description-first { opacity:0; }
 <div class="container"> <div class="list-wrapper"> <ul> <li onmouseover="MouseOver('1');" onmouseout="MouseOver('0');" class="item-first">hover here </li> </ul> </div> <div class="description-wrapper"> <div class="description-first">to make this appear</div> </div> </div>

Not possible. The closest you will get in css is using the .list-wrapper on hover like this:

.list-wrapper:hover + .description-wrapper > .description-first {
  opacity: 1;
}

But that's not what you want I guess. :)

This is my solution. With my solution I can add a class depending on the index.

 jQuery("li").hover(function () { jQuery(".description-wrapper").children().eq(jQuery(this).index()).toggleClass("description-visible"); });
 .description-wrapper div { opacity: 0; } .description-visible { opacity: 1 !important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="list-wrapper"> <ul> <li class="item-first">First</li> <li class="item-second">Second</li> <li class="item-third">Third</li> </ul> </div> <div class="description-wrapper"> <div class="description-first">First</div> <div class="description-second">Second</div> <div class="description-third">Third</div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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