gpt4 book ai didi

css - 如何切换 div 以使用 CSS 显示内容

转载 作者:搜寻专家 更新时间:2023-10-31 22:08:44 24 4
gpt4 key购买 nike

所以我有一个 div,它应该在切换时展开以显示一个列表。可以在这里看到之前和之后的状态 http://reversl.net/box/但是我需要在我的样式中添加什么才能实现这一点?我想使用 css transitions 来增加效果。我不担心浏览器兼容性,因为它只是为了学习目的。有什么建议吗?

<div class="box"><img src="http://www.placehold.it/250x300" alt="" />
<div class="section progress">
<h4>
<a href="#">Div Before</a>
</h4>
<div class="metrics">
<div class="meter">
<span style="width: 75%"></span>
</div><!--.meter-->
</div><!--.metrics-->
</div><!--.section-progress-->
</div><!--.box-->

<div class="box"><img src="http://www.placehold.it/250x300" alt="" />
<div class="section progress">
<h4>
<a href="#">Div After</a>
</h4>
<div class="metrics">
<div class="meter">
<span style="width: 75%"></span>
</div><!--.meter-->
</div><!--.metrics-->
</div><!--.section-progress-->
<div class="sub_section">
<ul class="list">
<li>Item 1</li>
<li class="last">Item 2</li>
</ul>
</div><!--.sub_section-->
</div><!--.box-->

.boxes {
width: 100%;
padding: 1%;
}

.box {
width: 20%;
padding: 1%;
background: #fff;
-moz-box-shadow: 2px 3px 4px 0px #e9e9e9;
-webkit-box-shadow: 2px 3px 4px 0px #e9e9e9;
box-shadow: 2px 3px 3px 0px #e9e9e9;
display: inline-block;
margin-top: 1em;
}

.box img {
margin-bottom: 1%;
}

.progress a {
margin: 0;
padding: 0;
color: #999;
text-decoration: none;
}

.metrics {
margin: -1em 0 0 0;
padding: 0;
background: #c0c0c0;

}
.accordion .progress .meter {
background: #555;
width: 100%;
position: relative;
}

.meter > span {
height: 10px;
display: block;
background-color: #777;
position: relative;
overflow: hidden;
}

.sub_section {
margin-top: 1em;
border-bottom: none;
}

.list {
padding: 0;
margin: 0;
}

.list li {
background: #dcdcdc url('http://placehold.it/40x40') no-repeat;
font-size: 11px;
color: #999;
list-style: none;
padding: 1.3em 1em 1.3em 4.5em;
margin-bottom: 5px;
}

.list .last {
border-bottom: none;
}

最佳答案

恐怕我无法在您的示例代码和图像中计算出您想要点击的内容以及您想要显示的内容。但是,以下代码适用于纯 CSS 的类似 onclick 的事件:

<div id="wrapper">
<ul id="top">
<li><a href="#one">One</a></li>
<li><a href="#two">Two</a></li>
</ul>
<div class="box" id="one">
<p>One</p>
<span><a href="#top">Close</a></span>
</div>
<div class="box" id="two">
<p>Two</p>
<span><a href="#top">Close</a></span>
</div>
</div>

CSS:

.box {
position: absolute;
top: 20%;
left: 50%;
width: 50%;
margin-left: -25%;
border: 4px solid #000;
background-color: #f90;
/* the CSS, above, is all for aesthetic purposes, what matters is the following */
opacity: 0;
-webkit-transition: all 1s linear;
-ms-transition: all 1s linear;
-moz-transition: all 1s linear;
-0-transition: all 1s linear;
transition: all 1s linear;
}

.box:target {
opacity: 1;
-webkit-transition: all 1s linear;
-ms-transition: all 1s linear;
-moz-transition: all 1s linear;
-0-transition: all 1s linear;
transition: all 1s linear;
}

JS Fiddle demo .

不幸的是(在 Chromium 17/Ubuntu 11.04 中)似乎无法从 display: none;display: block; 进行动画处理,类似地从 height 过渡: 0height: auto 也失败了(尝试任何一个都会导致突然出现而不是过渡。所以演示中的内容总是“在那里”,但只是隐藏(不透明)然后在点击相关链接后显示。

但是对于类似滑动切换的效果:

.box {
/* aesthetic stuff excised for brevity */
opacity: 0;
height: 0;
overflow: hidden;
-webkit-transition: all 1s linear;
-ms-transition: all 1s linear;
-moz-transition: all 1s linear;
-0-transition: all 1s linear;
transition: all 1s linear;
}

.box:target {
opacity: 1;
height: 3em;
-webkit-transition: all 1s linear;
-ms-transition: all 1s linear;
-moz-transition: all 1s linear;
-0-transition: all 1s linear;
transition: all 1s linear;
}

JS Fiddle demo .

关于css - 如何切换 div 以使用 CSS 显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10127618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com