:将鼠标悬停在一个元素上以更改另一个元素?

狂人

我有此条形图,当我悬停特定的条形图时,我想将“ test div”更改为不同的背景色和内容本身。

我在尝试制作一个元素时遇到了麻烦:将鼠标悬停更改为另一个元素。

我知道有关此主题的问题很多,但似乎没有一个对我有用。

HTML:

<div class="the-container">
    <ul>
        <li><a class="html" href="#"></a></li>
        <li><a class="css" href="#"></a></li>
        <li><a class="photoshop" href="#"></a></li>
        <li><a class="illustrator" href="#"></a></li>
        <li><a class="javascript" href="#"></a></li>
    </ul>
    <div class="test"><p>Hello</p></div>
</div>

CSS:

.the-container { width:300px; height:250px; background-color:#EEE; }

.the-container ul { margin:0; padding:0; list-style:none; }

.the-container li { width:40px; height:250px; margin:0 10px; position:relative; float:left;}

.the-container li a { position:absolute; bottom:0; width:100%; height:0; background-color:#ccc; border-top-left-radius: 5px; border-top-right-radius: 5px;}

.the-container li a.html:hover { background-color:#111; }
.the-container li a.css:hover { background-color:#ef3ef3; }
.the-container li a.photoshop:hover { background-color:#be3be3; }
.the-container li a.illustrator:hover { background-color:#cd3cd3; }
.the-container li a.javascript:hover { background-color:#888; }

div.test{width:300px; height: 30px; background-color:#BBB; position: relative; clear:both; line-height:2; margin-top:-16px;}

div.test p {text-align: center;}

a.html:hover ~ div.test{background-color:yellow;}

演示版

有任何想法吗?非常感激!

柯林·培根

不幸的是,您只能选择具有CSS的兄弟姐妹和孩子。为了能够定位,.test您需要:hoverul或容器套用div

可以在这里找到CSS中的子级和同级选择器的详细说明

更新

虽然我不喜欢语义,但是这里有一个POC示例,说明如何使用CSS并更改标记的结构。

<div class="the-container">
<ul>
    <li class="item-html"><a class="html" href="#"></a></li>
    <li><a class="css" href="#"></a></li>
    <li><a class="photoshop" href="#"></a></li>
    <li><a class="illustrator" href="#"></a></li>
    <li><a class="javascript" href="#"></a></li>
    <li><div class="test"><p>Hello</p></div></li>
</ul>
</div>

.item-html:hover ~ li div.test { background: yellow; }

小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将鼠标悬停在一个元素上可更改另一个元素的背景img

将鼠标悬停在一个元素上以更改另一个元素

将鼠标悬停在另一个元素上时删除<div>元素

将鼠标悬停在另一个元素上时如何更改元素的颜色?

CSS::将鼠标悬停在元素上并定位另一个

CSS:将鼠标悬停在另一个元素上

CSS,将鼠标悬停在一个元素上,影响另一个

如果悬停在另一个重叠元素上,请保持鼠标悬停

当我们将鼠标悬停在不同的元素上时,将一个图标更改为另一个

将鼠标悬停在另一个元素上时使元素出现并进行调整

当用户将鼠标悬停在另一个元素上时,无法显示弹出窗口元素

将鼠标悬停在某个元素上可以对不相邻或同级的另一个元素生效

WPF-将鼠标悬停在另一个元素上时隐藏元素

将鼠标悬停在一个元素上会改变另一个元素吗?

将鼠标悬停在其中的另一个元素时,将图像更改为黑白

将鼠标悬停在另一个div上时更改div

将鼠标悬停在图像上并更改另一个

将鼠标悬停在另一个元素上时如何激活 CSS 功能

将鼠标悬停在另一个元素上时如何向上滚动div

将鼠标悬停在另一个 div 上时更改一个 div 的样式

将鼠标悬停在另一个<li>上时更改一个<li>的背景

当将另一个元素悬停在元素上时,如何更改:hover状态?

将鼠标悬停在另一个元素上方的元素上时,CSS悬停不透明度转换会闪烁

将鼠标悬停在一个元素上以显示其他元素

将鼠标悬停在另一个元素上时如何使元素显示,如果没有则隐藏它。并让它一直占用空间

将鼠标悬停在其中一个元素上时如何更改其他元素的样式?

将鼠标悬停在 1 个 div 上以更改另一个 div 中的内容

将一个元素悬停在另一个元素上时更改其CSS

将鼠标悬停在ID上可显示另一个Div