我试图将鼠标悬停在段落的一部分上时显示隐藏的 div。段落有两个部分需要悬停效果,每个部分都有自己的 div,以在悬停时显示。但是,我无法让它工作。
当然我做错了什么,但我自己无法弄清楚。
我正在寻找最好不涉及JS的解决方案。
我有以下 HTML 代码:
<body>
<div class="div1">
<p class="show">
Lorem ipsum<span class="txt1"> dolor</span> sit amet,<span class="txt2"> consectetur</span> adipiscing elit.
</p>
<div class="hide1">
<table>
<tr>
<td rowspan="2"><img src="NULL" alt="test"></td>
<td><a href="">Hidden Text 1></a></td>
</tr>
<tr>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sodales.</td>
</tr>
</table>
</div>
<div class="hide2">
<table>
<tr>
<td rowspan="2"><img src="NULL" alt="logo"></td>
<td><a href="">Hidden Text 2 </a></td>
</tr>
<tr>
<td>Duis tellus ligula, volutpat et cursus in, euismod in magna.</td>
</tr>
</table>
</div>
<!-- there will be a form here */ -->
</div>
</body>
使用以下 CSS 代码:
body {
background-color: #eee;
}
.div1 {
height: 30px;
padding: 50px 0 60px 0;
margin: auto;
position: relative;
text-align: center;
}
.show {
padding: 0 0 15px 0;
margin: 0;
}
.hide1 {
display:none;
opacity:0;
background-color: #eee;
border: 1px #666 solid;
}
.hide2 {
display: none;
opacity:1;
background-color: #eee;
border: 1px #666 solid;
}
.txt1, .txt2 {
font-weight: bold;
color:#bc2f00;
}
.txt1:hover + .hide1 {
opacity: 1;
display: block !important;
}
.txt2:hover + .hide2 {
opacity: 1;
display: block !important;
}
我想说你应该重建你的 HTML,使得要悬停的文本和隐藏块重合并位于单个父项下。
看这个例子:
body {
background-color: #eee;
}
.div1 {
height: 30px;
padding: 50px 0 60px 0;
margin: auto;
position: relative;
text-align: center;
}
.show {
padding: 0 0 15px 0;
margin: 0;
}
.hide1 {
display: none;
opacity: 0;
background-color: #eee;
border: 1px #666 solid;
}
.hide2 {
display: none;
opacity: 1;
background-color: #eee;
border: 1px #666 solid;
}
.txt1,
.txt2 {
font-weight: bold;
color: #bc2f00;
}
.div1 .show .txt1:hover~.hide1 {
display: block;
background: blue;
cursor: pointer;
opacity: 1;
}
.div1 .show .txt2:hover~.hide2 {
display: block;
opacity: 1;
display: block !important;
}
<div class="div1">
<div class="show">
<p class="txt1">Hover over me</p>
<div class="hide1">
<table>
<tr>
<td rowspan="2"><img src="NULL" alt="test"></td>
<td><a href="">Hidden Text 1></a></td>
</tr>
<tr>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sodales.</td>
</tr>
</table>
</div>
<p class="txt2">Or over me</p>
<div class="hide2">
<table>
<tr>
<td rowspan="2"><img src="NULL" alt="logo"></td>
<td><a href="">Hidden Text 2 </a></td>
</tr>
<tr>
<td>Duis tellus ligula, volutpat et cursus in, euismod in magna.</td>
</tr>
</table>
</div>
</div>
</div>