悬停 2 个孩子时如何更改 1 个孩子的内容
How to change content of 2 childs when hover 1 child?
我想要完成的事情相当简单,但对我来说却很复杂。我正在尝试将鼠标悬停在 1 个孩子身上,而其他 2 个孩子会改变背景颜色。对所有 3 个孩子都做同样的事情。
网页代码:
<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也很好)。
谢谢。
我想你是说你想要这个:
更好的视图: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>
.servhold
上的:hover
将它们全部变成粉红色,但.serv
上的:hover
将特定的transparent
设置为,以便外部背景显示出来。如果需要,您可以将其设置为其他颜色。
jQuery的方式是:
首款功能变更siblings background
.
第二个功能 - 重新启动siblings background
- 把你的原始background-color
放在那里,而不是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>
您可以将悬停事件绑定到类 '.serv',然后为所有元素添加粉红色背景,并使背景在调用悬停函数的元素上透明
$('.serv').hover(function(){
$('.serv').css({'background-color': 'pink'});
$(this).css('background', 'transparent');
});
相关文章:
- 当鼠标悬停在文本中的单词上时显示警报
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 手风琴可点击并悬停
- 悬停功能触发器
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- css(或jQuery)悬停时淡入淡出
- jquery快速悬停问题
- 如何在悬停时流畅地更改单词
- jQuery:当屏幕大小改变时,如何更改默认图像和悬停图像
- 在ReactJS中重新渲染孩子3次可以接受吗
- 悬停下拉菜单即使在鼠标移出后也保持活动状态
- 努力让第n个有悬停的孩子在IE工作
- 悬停菜单关闭孩子时,当我尝试将鼠标移动到它时.为什么?[JsFiddle]
- 悬停 2 个孩子时如何更改 1 个孩子的内容
- 如何在鼠标悬停时仅悬停父母李而不是孩子
- 当鼠标悬停在孩子动态创建的地方时,防止onmouseout
- jquery toggleClass的孩子悬停