【发布时间】:2016-04-15 06:36:31
【问题描述】:
从这个question 得到了非常模糊和不需要的答案,所以我决定发布一个明确的问题简报我面临的问题。
我使用的代码如下所示:
.flipClass {
font-size: Large;
background-color: #4399CD;
color:#fff;
text-align: left;
}
.flipClass:active{
background:#fff;
color:#4399CD;
cursor:pointer;
transition:all .1s ease-out;
}
.flipClass:active:before {
content:'\f102';
font-family:FontAwesome;
font-style:normal;
font-weight:normal;
text-decoration:inherit;
padding-left:6px;
padding-right:8px;
transition:all .3s;
}
单击元素后,如何获得由 .flipClass:active 定义的 CSS 样式?目前,它只会在短时间内变得活跃。
请看下面给出的sn-p:
$("div[id^=flip]").click(function() {
$(this).next("div[id^=panel]").stop().slideToggle("slow");
});
.panelClass,
.flipClass {
padding: 5px;
padding-left: 15px;
border: solid 1px #c3c3c3;
}
.flipClass {
font-size: Large;
background-color: #4399CD;
color: #fff;
text-align: left;
}
.flipClass:active {
background: #fff;
color: #4399CD;
cursor: pointer;
transition: all .1s ease-out;
}
.flipClass:active:before {
content: '\f102';
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
padding-left: 6px;
padding-right: 8px;
transition: all .3s;
}
.panelClass {
padding: 30px;
padding-top: 10px;
display: none;
background-color: #F3F5F6;
color: #000;
}
.flipClass:before {
content: '\f107';
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
padding-left: 6px;
padding-right: 8px;
transition: all .3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />Currently, the .flipClass:active CSS style sheet is working only for a brief moment (precisely until the duration of the mouseclick and maybe some small offset). Click and HOLD mouseclick when clicking on .flipClass to see the effect. I want it to stay!
<div class="flipClass" id="flip1">FIRST HEADING</div>
<div class="panelClass" id="panel1">How to keep the first heading 'active' when this panel is showing? If I could figure that out, I could change the arrow to a pullup arrow too :3</div>
<div class="flipClass" id="flip2">SECOND HEADING</div>
<div class="panelClass" id="panel2">If both Headings have been clicked, this Heading and any other heading that were clicked should also be active (or atleast remain in the CSS state defined by .flipClass:active here)</div>
目前,.flipClass:active CSS 样式表只工作一小段时间(精确到鼠标单击的持续时间,可能还有一些小的偏移量)。单击flipClass 后,我需要一种方法使其始终保持活动状态。我是否为此使用了不合适的伪选择器?
有什么解决方法吗?或者是否有其他专门用于此目的的伪选择器?我可以举个例子吗?
【问题讨论】:
-
改用
:focus -
一种方法是为他们使用隐藏的
checkboxen 和labels;将您的标题设为labels,将:active样式与:checked + .flipClass联系起来。另外,您也可以通过:checked + .flipClass + .panelClass将您的手风琴移动到 css 中。旁注,用“Class”后缀你的类名有点多余。 -
@Aziz
:focus只会在他们点击其他东西之前工作,这不是他想要的行为。他希望根据面板是否显示或在至少保持最近打开的一个:active -
如果你只希望最近的一个有风格,也可以通过
:target实现它;唯一的补充是需要一个自指向anchor