【问题标题】:How to keep an element :active even after clicking如何保持元素:即使在单击后仍处于活动状态
【发布时间】: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

标签: html css


【解决方案1】:

对于接受用户输入焦点的元素,例如按钮元素,:focus 伪类接近您想要的。但在这种情况下,您似乎可以展开多个部分,因此:focus 不会在这里剪切。

相反,您可以只使用 JS 来应用一个额外的类,如下所示:

$("div[id^=flip]").click(function() {
   $(this).toggleClass("flipClass-active").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;
  cursor: pointer;
  transition: all .1s ease-out;
}
.flipClass-active,
.flipClass:active {
  background: #fff;
  color: #4399CD;
}
.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;
}
.flipClass-active::before,
.flipClass:active::before {
  content: '\f102';
}
.panelClass {
  padding: 30px;
  padding-top: 10px;
  display: none;
  background-color: #F3F5F6;
  color: #000;
}
<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>

【讨论】:

  • 完全符合这个问题。 +1。但我可以得到你对@abluejelly 的回答的意见。他/她使用了完全基于 CSS 的答案。至少可以说它很有创意,但我想知道它是否以任何方式使性能更好。如果这种方法在某个地方的支持有限?
  • 嗯,现在我注意到了,我想知道为什么下拉箭头保持不变......嗯......代码似乎就位。我想知道问题是什么
  • 您有一些规则乱序,非活动规则能够覆盖活动规则。我编辑了我的答案以解决问题。 @abluejay 的答案也应该有效,我不希望有任何性能差异,但维护起来可能会更棘手。 8 个月后,当有人发现或引入 bug 时,你还记得为什么会有隐藏的复选框和选中状态吗?这很聪明,但似乎将来需要重新弄清楚。
  • @emackey 这就是为什么你可以拍一个&lt;!-- comment --&gt; 来解释“静态手风琴”并链接回你从哪里得到这个想法的原因。你知道,就像你应该对所有 StackOverflow 代码做的那样,它更易于维护。相关,我的 no-js 解决方案的核心设计应该随着时间的推移变得更加普遍,因为人们意识到他们可以做到这一点,因为它的稀有性是网络对变化的厌恶和对倒退的渴望的混合兼容性(IE 8 仅在 今年 年停产)。抱歉回复晚了,是jelly 不是jay,我是以来自NetHack 的生物命名的,不是鸟哈哈
【解决方案2】:

抛弃 jQuery,利用 :checked 伪类、labels 和 + 选择器,我们实际上可以使用原生静态!

这意味着我们必须自己为展示面板的max-height 设置动画。因为我们必须对整个max-height 进行动画处理,而不仅仅是内容高度,所以我们必须欺骗眼睛让动画变得不那么极端。为此,还要为顶部/底部填充设置动画 - 让观看者的眼睛完成其余的工作。

此解决方案的缺点是,您必须能够可靠地设置面板高度的上限。在动画结束后添加overflow:auto; 可以在一定程度上缓解这种情况,但我会将如何做到这一点作为练习留给读者。

.panelClass,
.flipClass {
  display:block;
  padding: 5px;
  padding-left: 15px;
  border: solid 1px #c3c3c3;
}

.flipClass {
  font-size: Large;
  background-color: #4399CD;
  color: #fff;
  text-align: left;
  cursor:pointer;
}
.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;
}
:checked + .flipClass {
  background: #fff;
  color: #4399CD;
  transition: all .1s ease-out;
}
:checked + .flipClass: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 {
  /*display: none;/*We have to animate this ourselves now*/
  padding:0 30px;
  max-height:0;
  background-color: #F3F5F6;
  color: #000;
  transition:all 450ms ease-in-out;
  overflow:hidden;
}
:checked + .flipClass + .panelClass{
  max-height:900px;
  padding-top: 10px;
  padding-bottom: 30px;
}
<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!
<input type="checkbox" id="flip1" hidden />
<label class="flipClass" for="flip1">FIRST HEADING</label>
<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>
<input type="checkbox" id="flip2" hidden />
<label class="flipClass" for="flip2">SECOND HEADING</label>
<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>

另一种选择是使用:target (MDN) 来“存储”最近选择的选项...但是如果用户在打开页面后为页面添加书签,则会导致奇怪的行为,一些额外的逻辑来处理单击它以关闭它,并且只能标记一项。如果您想要只展示一个手风琴,这绝对是一个不错的选择。或者,您可以将上述代码中的复选框替换为单选按钮。

【讨论】:

  • 我喜欢@emackey 的回答,因为它符合我的目的,几乎没有什么变化。但是,在您出色的实现中,似乎没有使用 javascript 和扩展 jQuery。这是否意味着这会更快地加载我的页面?如果您能告诉我它是否会对我的网站产生任何负面影响,我愿意使用此实现。我更喜欢非本地的,因为它是可靠的。我想知道这种方法的可靠性。它会起作用吗。总是?有什么例外吗?
  • 我在我的博客上实现了它。会在网站上创造奇迹,但似乎这会在博客上产生错误,因为 的某些属性没有得到适当的支持,并且博客试图自动格式化代码......这会搞砸很多事情。尽管如此,这是一个很好的解决方案!
  • @lancel0t Native 意味着缺少 jQuery(或任何其他外部库),而这几乎总是会提高性能(jQ 以缓慢和臃肿而闻名)。删除 Javascript 意味着页面通常会更快(它是静态定义的),并且运行 NoScript 或类似工具的人不会遇到可访问性问题。缺点,IE 8 and earlier 会遇到可访问性问题(无法取消隐藏文本),IE 9 会缺少滑动动画。请注意,IE 8 自 2016 年 1 月中旬起已停产
【解决方案3】:

您不能在 CSS 中使用 :active 来执行此操作。保持链接处于活动状态的一种方法是使用您的脚本添加一个类,like this

$("div[id^=flip]").click(function() {
   $(this).toggleClass('active').next("div[id^=panel]").stop().slideToggle("slow");
 });

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

:active 只会在按住鼠标按钮时应用样式 下。应用样式并在点击时保持应用的唯一方法是 使用一点 JavaScript。

(此报价来源:Can I have an onclick effect in CSS?

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    相关资源
    最近更新 更多