63

我有一个这样的设置:

<div id="button">Button</div>

这对于 CSS:

#button {
    color: #fff;
    display: block;
    height: 25px;
    margin: 0 10px;
    padding: 10px;
    text-indent: 20px;
    width: 12%;
}

#button:before {
    background-color: blue;
    content: "";
    display: block;
    height: 25px;
    width: 25px;
}

有没有办法给伪元素一个悬停效果,例如#button:before:hover {...},是否也可以让伪元素悬停以响应另一个悬停的元素,如下所示:#button:hover #button:before {...}?只有 CSS 会很好,但 jQuery 也很好。

4

4 回答 4

95

您可以根据父级的悬停更改伪元素:

JSFiddle 演示

#button:before {
    background-color: blue;
    content: "";
    display: block;
    height: 25px;
    width: 25px;
}

#button:hover:before {
    background-color: red;
}

#button {    display: block;
    height: 25px;
    margin: 0 10px;
    padding: 10px;
    text-indent: 20px;
    width: 12%;}

#button:before { background-color: blue;
    content: "";
    display: block;
    height: 25px;
    width: 25px;}

#button:hover:before { background-color: red;}
<div id="button">Button</div>

于 2012-01-15T23:52:29.567 回答
27

为了澄清,你不能:hover一个伪元素。::after:hover截至 2018 年,CS​​S 中没有这样的东西。

于 2018-09-05T20:34:22.120 回答
11

#button:hover:before将更改伪元素以响应悬停的按钮。但是,如果您只想对伪元素做任何重要的事情,最好将实际元素放入 HTML 中。伪元素相当有限。

虽然 CSS 不会让您根据元素之后的元素来设置元素样式,但通常可以通过将 :hover 放在父节点上来装配具有相同基本效果的东西,即:

<div id="overbutton">
    <div id="buttonbefore"></div>
    <div id="button"></div>
</div>

#overbutton {
    margin-top: 25px;
    position: relative;
}

#buttonbefore {
    position: absolute;
    background-color: blue;
    width: 25px;
    height: 25px;
    top: -25px;
}

#overbutton:hover #buttonbefore {
    // set styles that should apply to buttonbefore on button hover
}

#overbutton:hover #buttonbefore:hover {
    // unset styles that should apply on button hover
    // set styles that should apply to buttonbefore on buttonbefore hover
}
于 2012-01-15T23:57:01.563 回答
0

如果只是出于设计目的,我认为最好在矩形框的任一侧创建伪元素并保持 HTML 尽可能简单:

HTML:

 <body>
    <div class="tabStyle">Rectangle</div>
    </body>

CSS:

 .tabStyle {
        border-style:solid;
        border-color: #D8D8D8;
        background : #D8D8D8;
        width:200px;
        height:93px;
        color: #000;
        position: relative;
        top: 10px;
        left: 49px;
        text-align:center;
    }
    .tabStyle:hover {
        background : #000;
        border-color: #000;
    }
    .tabStyle:hover::before {
        border-color: transparent #000 #000 transparent;
        border-style: solid;
        border-width: 0px 0px 100px 50px;
    }
    .tabStyle:hover::after {
        border-color: transparent transparent #000 #000;
        border-style: solid;
        border-width: 0px 50px 100px 0px;
    }
    .tabStyle::after {
        border-color: transparent transparent #D8D8D8 #D8D8D8;
        border-style: solid;
        border-width: 0px 50px 100px 0px;
        position: absolute;
        top: -4px;
        left:101%;
        content:"";
    }
    .tabStyle::before {
        border-color: transparent #D8D8D8 #D8D8D8 transparent;
        border-style: solid;
        border-width: 0px 0px 100px 50px;
        position: absolute;
        top: -4px;
        right: 101%;
        content:"";
    }

我已经修改了 CSS,结果如下所示:

http://jsfiddle.net/a3ehz5vu/

于 2014-12-29T18:32:21.793 回答