当鼠标悬停在另一个元素上时更改元素样式

4

我是一名有用的助手,可以为您进行文本翻译。

我有三个元素,需要通过悬停在其他两个元素上来更改其中一个元素的样式。

HTML

<div class="pagination">
        <span class="step-links">
            {% if page_obj.has_previous %}
                 <div class='not-current'><a href="?page={{ page_obj.previous_page_number }}">{{ page_obj.previous_page_number }}</a></div>
            {% endif %}

            <div id='current'>                
                {{ page_obj.number }}
            </div>


            {% if page_obj.has_next %}
                <div class='not-current' ><a href="?page={{ page_obj.next_page_number }}">{{ page_obj.next_page_number }}</a></div>
            {% endif %}              
        </span>
    </div>

css

#current, .not-current:hover {
    width: 30px;
    height: 30px;
    line-height: 30px;
    font-size: 20px;
    font-weight: bold;
    border: orange outset 3px;
    background-color: orange;
    margin: 0 auto 10px auto;
    box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}

.not-current {
    width: 15px;
    height: 15px;
    line-height: 15px;
    background-color: #A29F9F;
    border: #A29F9F outset 2px;
    box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}

.not-current:hover #current {
    display: none;
}

当鼠标悬停在.not-current元素上时,其样式会改变,但#current元素的样式不会改变。我哪里做错了?(仅在Chromium 12.0中测试过)。
4个回答

6

您可以使用CSS兄弟选择器~来实现此功能,但要注意在标记中,您的鼠标悬停元素必须位于要样式化的元素之前。您可以按任意顺序显示它们。

演示:jsFiddle

CSS:

#one:hover ~ #three,
#six:hover ~ #four {
    background-color: black;
    color: white;
}
#four {
    margin-left: -35px;
}
#six {
    left: 80px;
    position: relative;
}
.box {
    cursor: pointer;
    display: inline-block;
    height: 30px;
    line-height: 30px;
    margin: 5px;
    outline: 1px solid black;
    text-align: center;
    width: 30px;
}

HTML:

<p>Hover over 1 and 3 gets styled.</p>
   <div id="one" class="box">1</div><!--
--><div id="two" class="box">2</div><!--
--><div id="three" class="box">3</div>

<!-- this works because 4 is after 6 in the markup, 
despite its display position -->
<p>Hover over 6 and 4 gets styled.</p>
   <div id="six" class="box">6</div><!--
--><div id="four" class="box">4</div><!--
--><div id="five" class="box">5</div>

2
我已经写CSS超过10年了,但仍然发现一些之前从未见过的技巧。~是一个非常方便的运算符,以前我甚至在寻找如何处理兄弟姐妹的子元素时也束手无策!正如上面的回答中提到的那样,我倾向于使用JS来处理这种情况。所以,感谢你向我介绍它! - Nathan Hornby
这应该是被接受的答案。我成功地在链接悬停状态下为SVG图标(font awesome)添加了阴影,如下所示:a:hover > svg { filter: drop-shadow(2px 2px 2px black); } 我最终不得不使用>选择器而不是~。以前,我只能通过直接悬停在图标上来应用效果。非常感谢,这是一个非常流畅的例子,你的JSFiddle让概念非常清晰。顺便说一句,这也让我省去了一些花哨的JS解决方案...简单明了! - twknab

2
那是因为#current不在.not-current下面。这种行为最好使用JavaScript实现。

2

1
最好的方法是使用:hover伪类来选中父元素,并使用:not(:hover)来样式化子元素,如下所示。
<div class="team__header">
        <img src="img/sam.jfif" alt="team member 1" class="team__image">
        <img src="img/brii.jpg" alt="team member 1" class="team__image">
        <img src="img/humphrey.jpg" alt="team member 1" class="team__image">
        <img src="img/user-4.jpg" alt="team member 1" class="team__image">
    </div>

你的 CSS 将会像这样:
.team__header:hover .team__image:not(:hover){
    border: 3px solid red;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接