【问题标题】:CSS hide/show effect works on Firefox but not on Chrome/SafariCSS 隐藏/显示效果适用于 Firefox,但不适用于 Chrome/Safari
【发布时间】:2014-10-30 18:16:07
【问题描述】:

我正在研究 CSS 隐藏/显示效果。它在 Firefox 上运行良好。你点击它,Hello World 文本就会出现。然后你再次点击,它就消失了。

但在 Chrome/Safari 上,您必须点击并按住按钮才能使其显示。

如何让它在 Chrome/Safari 上像在 Firefox 上一样正常工作?

这里是jsfiddle。

HTML:

<div class="formatting_show" id="formatting_show"></div>

<span id="formatting_content"> Hello World </span>

CSS:

#formatting_show {
    cursor: pointer; 
    cursor: hand;
    float: right;
    font-size: 30px;
    height: 15px;
    text-align: right;
    width: 74px;
    color: #000;
}

#formatting_content {
    display: block;
    -webkit-transition: opacity 1s ease-out;
    transition: opacity 1s ease-out;
    opacity: 0; 
    height: 0;
    font-size: 0;
    overflow: hidden;
}
#formatting_show:before {
    content: "Formatting «"
}
#formatting_show:active.formatting_show:before {
    content: "Formatting »"
}
#formatting_show:active ~ span#formatting_content {
    font-size: 30px;
    height: 70px;
    opacity: 1;
}

#formatting_content {
    float: left;
}

【问题讨论】:

  • 我猜:active在不同浏览器中的实现方式不同
  • @Huangism 你知道如何调整它以使其通用吗?如果你这样做,请提供一个用于测试的小提琴。
  • 我会使用 js 来实现这种点击行为,在 webkit 浏览器中,只有在项目上按住鼠标按钮时才会添加活动状态。我认为使用 css 是不可行的

标签: html css google-chrome hide show


【解决方案1】:

我同意@Huangism 在评论中写道,:active 伪类不可能做到这一点。但是,您可以使用 HTML 复选框、该复选框的 标签 和 CSS 中的 general sibling selector (see also)。

这是所有的 HTML 代码:

<input  id="formatting_show" type="checkbox" />
<label for="formatting_show">Formatting</label>

<div class="formatting_content">Hello World</div>

以及完整的 CSS 代码:

#formatting_show {
    display: none;
}
.formatting_content {
    display: none;
}
#formatting_show:checked ~ .formatting_content {
    display: block;
}

你可以看看a running example。

【讨论】:

  • 好主意!除了将复选框移出视口之外,您还可以使用 display: none; 或 visibility: hidden; 将其隐藏。
  • @Jay,谢谢你的建议;我已经更新了我的答案。 Henrik 很高兴你喜欢它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2018-05-05
相关资源
最近更新 更多