简体   繁体   English

悬停CSS更改另一个div的图像

[英]CSS on hover change image from another div

HTML part HTML部分

   <div class="test">
      <ul>
         <li class="yeah"><a href="#">Link 1</a></li> 
    </div>

    <div class="subject">
      <p>hello</p>
    </div>

CSS 的CSS

.test:hover + .subject{
  background-color:green;
}

So if I do this, it works. 因此,如果我这样做,它会起作用。 But what I really want to do is using the item inside the list to target the div "subject" and make changes that way. 但是,我真正想做的是使用列表中的项目定位div“主题”并以这种方式进行更改。 The reason why I'm doing it like this is because I'll have more than one item inside that list and I want each of them to do a certain thing. 之所以这样做,是因为在该列表中我将有多个项目,并且我希望每个人都做某件事。

My failed attempt: 我失败的尝试:

.test li.yeah:hover + .image{
  background-color:green;
}

Is this even possible? 这有可能吗? Thanks. 谢谢。

Right now it's only possible to target directly related elements on CSS, so what you want is not possible yet. 现在,只能将直接相关的元素作为CSS上的目标,因此您还无法实现所需的目标。

With javascript that can be easily done with jQuery: 使用jQuery可以轻松完成的javascript:

 $sub = $('.subject'); $('.yeah').on({ mouseenter: function() { var i = $(this).index(); $sub.addClass('li-'+i); }, mouseleave: function() { $sub.removeClass().addClass('subject'); } }) 
 .li-0{ background-color:green; } .li-1{ background-color:red; } .li-2{ background-color:blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <ul> <li class="yeah"><a href="#">Link 1</a></li> <li class="yeah"><a href="#">Link 1</a></li> <li class="yeah"><a href="#">Link 1</a></li> </ul> </div> <div class="subject"> <p>hello</p> </div> 

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

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