2016-03-14 65 views
2

我想完成的事情相当简单,但对我来说很复杂。我想徘徊在1个孩子上,而另外2个孩子则改变背景颜色。并为所有3个孩子做同样的事情。如何在悬停1个孩子时更改2个孩子的内容?

HTML代码:

<div class="servhold"> 
    <div class="serv"> 
     <h1>Clean</h1> 
     <p>test test test test test test test 
      test test test test test test test 
      test test test test test test test 
     </p> 
    </div> 
    <div class="serv"> 
     <h1>Creative</h1> 
     <p>test test test test test test test 
      test test test test test test test 
      test test test test test test test 
     </p> 
    </div> 
    <div class="serv"> 
     <h1>Responsive</h1> 
     <p>test test test test test test test 
      test test test test test test test 
      test test test test test test test 
     </p> 
    </div> 
</div> 

CSS代码(我想):

.servhold .serv:hover:nth-child(1) + .serv:nth-child(n+2){ 
    background-color: pink; 
} 
// hover 1 change background 2+3 
.servhold .serv:hover:nth-child(2) + .serv:nth-child(1) , .serv:nth-child(3){ 
    background-color: pink; 
} 
// hover 2 change background 1+3 
.servhold .serv:hover:nth-child(3) + .serv:nth-child(1) , .serv:nth-child(2){ 
    background-color: pink; 
} 
// hover 3 change background 1+2 

任何帮助,将不胜感激(Javascript或jQuery是没关系。)

谢谢。

+0

你的意思是,虽然'创意'在浩ver,'clean'和'responsive'会得到不同的背景吗? –

+0

是的,有点像。 – Graphicoding

回答

2

jQuery的方法是:
第一功能 - 改变siblings background
二级功能 - 重新启动siblings background -put那里initial

$('.servhold .serv').hover(function() { 
 

 
    $(this).siblings('.serv').css('background-color', 'pink'); 
 

 
}, function() { 
 

 
    $(this).siblings('.serv').css('background-color', 'initial'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="servhold"> 
 

 
    <div class="serv"> 
 
    <h1>Clean</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
    <div class="serv"> 
 
    <h1>Creative</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
    <div class="serv"> 
 
    <h1>Responsive</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
</div>

+1

这就像我想要的一样!我给你的答案是因为你提供了代码和代码片段。谢谢! – Graphicoding

4

我想你是说你要这样:

更好地在这里查看:https://jsfiddle.net/wsbLy0b2/1/

.servhold:hover .serv { 
 
    background-color: pink; 
 
} 
 

 
.servhold:hover .serv:hover { 
 
    background-color: transparent; 
 
}
<div class="servhold"> 
 

 
    <div class="serv"> 
 
    <h1>Clean</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
    <div class="serv"> 
 
    <h1>Creative</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
    <div class="serv"> 
 
    <h1>Responsive</h1> 
 
    <p>test test test test test test test test test test test test test test test test test test test test test 
 
    </p> 
 
    </div> 
 
</div>

:hover.servhold把它们所有的粉红色,但:hover.serv设置为transparent所以外部背景显示通过。如果需要,您可以将其设置为不同的颜色。

+0

谢谢你的回答,但我确实希望他们都以白色背景开头(不是粉红色)。如果你编辑你的答案,我会很感激,谢谢。 – Graphicoding

+0

@Graphicoding:他们确实以白色背景开始。 – 2016-03-15 15:04:43

+0

我的意思是我不想悬停在'.servhold'上,并让我的'.serv'变成粉红色。但感谢您的帮助! – Graphicoding

1

你原来background-color相反,你可以绑定悬停事件类“.serv”,然后添加粉红色背景的所有元素,并使背景透明的元素上悬停功能被称为

$('.serv').hover(function(){ 
    $('.serv').css({'background-color': 'pink'}); 
    $(this).css('background', 'transparent'); 
}); 
+0

好的概念,会尽快尝试,并向我认为有最佳答案的人声明答案。谢谢。 – Graphicoding