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

发布于 2024-11-28 03:08:16 字数 1414 浏览 1 评论 0原文

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

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 中测试)。

I have a three elements and I need to change style of one element by hover on other two.

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;
}

Styles of hovered element (.not-current) are changed but styles of #current element aren't changed. Where am I wrong in there? (Tested only in Chromium 12.0).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悸初 2024-12-05 03:08:16

您可以使用 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>

You can do this with the CSS sibling selector ~, with the caveat that your hover element must come before the elements you want to style in the markup. You can display them in any order you like, though.

Demo: 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>
冬天的雪花 2024-12-05 03:08:16

这是因为 #current 不在 .not-current 下。使用 JavaScript 可以更好地实现此行为。

That's because #current is not under .not-current. This behaviour is better implemented using JavaScript.

月朦胧 2024-12-05 03:08:16

.not-current:hover #current 表明 #current 位于 .not-current 下,正如 @silverstrike 所说。

在这个上使用 JavaScript: http://www.w3schools.com/js/js_events.asp< /a>

jQuery:http://api.jquery.com/mouseover/

.not-current:hover #current states that #current is under .not-current as @silverstrike said.

Go with JavaScript on this one: http://www.w3schools.com/js/js_events.asp

or

jQuery: http://api.jquery.com/mouseover/

当爱已成负担 2024-12-05 03:08:16

最好的办法是使用 :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;
}

the best thing to do is to target the parent with the :hover pseudo-class and style the child using :not(:hover) as shown

<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>

your css will look like this

.team__header:hover .team__image:not(:hover){
    border: 3px solid red;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文