css - div rollover 让子 div 改变颜色
问题描述
我在父 div 中有 3 个子 div,目前只有部分子 div 在鼠标悬停时改变颜色,我希望所有子 div 在部分鼠标悬停时改变颜色。谢谢
我试过下面的代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.container {
display: inline-block;
cursor: pointer;
width: 35px;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.bar1:hover, .bar2:hover, .bar3:hover {
width: 35px;
height: 5px;
background-color: #e5e5e5;
margin: 6px 0;
transition: 0.4s;
}
</style>
</head>
<body>
<div style=" text-align: right; padding-right: 30px;">
<div class="container">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</body>
</html>
解决方案
您可以通过在悬停时选择父节点的子节点来完成此操作。
您当前的代码:
.bar1:hover, .bar2:hover, .bar3:hover {
width: 35px;
height: 5px;
background-color: #e5e5e5;
margin: 6px 0;
transition: 0.4s;
}
... 将单独应用于每个元素(即,.bar3
需要将其自身悬停以触发该状态)。
相反,您要选择的子元素.container:hover
:
.container:hover .bar1, .container:hover .bar2, .container:hover .bar3 {
width: 35px;
height: 5px;
background-color: #e5e5e5;
margin: 6px 0;
transition: 0.4s;
}
这会将样式应用于:
.bar1
,container
悬停时.bar2
,container
悬停时.bar3
,container
悬停时
编辑:起初我没有意识到这bar1
是另外两个的兄弟姐妹。更新了我的答案以应用基于container:hover
.
推荐阅读
- prolog - 如果已经存在 [a,b],如何从列表中删除 [b,a]
- unix - 本地转发错误的 zsh 命令
- c# - Windows PPKG 配置文件的签名和加密格式/规范
- javascript - 使用超时并基于 JSON url 构建 html 元素
- c# - 在 Autofac 中自动处理子生命周期范围
- reactjs - 自动化 ReactJS 应用程序中的超时问题
- ubuntu - xspec:加载共享库时出错:libXSPlot.so:无法打开共享对象文件:没有这样的目录
- python - Pandas - 从样本数据的随机选择中生成新值
- c++ - 如何抑制未使用的(void **arg)参数?
- javascript - `xhr.getAllHeaders()` 在浏览器扩展上下文中缺少一些标题?