英文:
Expandable box CSS margin-top
问题
我正在尝试制作一个基于CSS的可展开框,其中包含任意内容。我的方法如下:
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
margin-top: -100%;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
margin-top: 0;
}
<input type="checkbox" id="expandme" />
<label for="expandme">Expand this content</label>
<div class="expandContainer">
<div class="expandContents">
<p>Expanded Content</p>
</div>
</div>
当单击标签时,隐藏的输入框被选中,并.expandContents
的样式从margin-top: -100%
变为margin-top: 0
。
我不能使用display: none
,因为我想要使用transition
属性稍微动画化操作,尽管我在示例中没有包含它,以保持简单。
我发现的问题是,margin-top
基于元素的宽度而不是高度,因此如果内容太高或容器太窄,则在折叠时内容底部会可见。
是否有一种方法来解决这个问题,或者是否需要在某个地方使用JavaScript计算?
英文:
I'm trying to make a CSS based expandable box with arbitrary content. My approach is this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
margin-top: -100%;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
margin-top: 0;
}
<!-- language: lang-html -->
<input type="checkbox" id="expandme" />
<label for="expandme">Expand this content</label>
<div class="expandContainer">
<div class="expandContents">
<p>Expanded Content</p>
</div>
</div>
<!-- end snippet -->
When the label is clicked, the hidden input is checked and the style of .expandContents
goes from margin-top: -100%
to margin-top: 0
.
I can't use display: none
because I'd like to have the action be animated a little bit with a transition
property, which I didn't include in the example for simplicity.
The problem I discovered is that margin-top
is based on the width of the element, rather than the height, so if the contents are too tall or the container too narrow, then the bottom of the contents are visible when collapsed.
Is there a way to approach this or is this impossible without using a JavaScript calculation somewhere?
答案1
得分: 1
改变方法,不要使用边距,尝试在你的.expandContainer
父元素上使用position: relative;
,在你的.expandContents
上使用position:absolute;
,然后在你的CSS中分别使用left: -100%;
和left: 0;
,然后你可以为这个过渡添加动画。希望这有所帮助。
英文:
Change the aproach, instead of margin try playing with a position: relative;
parent(your .expandContainer
) and a position:absolute;
on your .expandContents
then use left: -100%;
and left: 0;
respectively on your css, and then you can add an animation to that transition. Hope this helps.
答案2
得分: 1
你可以使用opacity:0
来显示内容,而不是使用display:none
。这样,你可以使用@keyframes
动画来同时处理内容的可见性和平移效果。尝试将以下内容添加到你的代码中。你可能需要调整一些时间,但这应该可以解决问题。
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
margin-top: -100%;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
animation: slideDown .3s forwards;
}
@keyframes slideDown {
0% {
opacity: 0;
margin-top: -100%;
}
10% {
opacity: 1;
}
100% {
opacity: 1;
margin-top: 0;
}
}
<input type="checkbox" id="expandme" />
<label for="expandme">Expand this content</label>
<div class="expandContainer">
<div class="expandContents">
<p>Expanded Content</p>
</div>
</div>
希望这对你有帮助。
英文:
You could use an opacity:0
for the content instead of display:none
. In that way, you could use a @keyframe
animation that takes care both of making the content visible and translated. Try adding this tp your code. You may need to fix some timing, but it should make the trick
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-css -->
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
margin-top: -100%;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
animation: slideDown .3s forwards
}
@keyframes slideDown {
0% {
opacity: 0;
margin-top: -100%;
}
10% {
opacity:1
}
100% {
opacity: 1;
margin-top: 0;
}
}
<!-- language: lang-html -->
<input type="checkbox" id="expandme" />
<label for="expandme">Expand this content</label>
<div class="expandContainer">
<div class="expandContents">
<p>Expanded Content</p>
</div>
</div>
<!-- end snippet -->
Hope this helps
答案3
得分: 1
max-height
通常用于以可以进行动画的方式实现这一目标。
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
max-height: 0;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
max-height: 1000px;
}
<input type="checkbox" id="expandme" />
<label for="expandme">展开内容</label>
<div class="expandContainer">
<div class="expandContents">
<p>展开的内容</p>
</div>
</div>
英文:
max-height
is often used to achieve this in a way that can be animated.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
input {
display: none;
}
label {
display: block;
border: 1px solid;
}
.expandContainer {
overflow: hidden;
}
.expandContents {
max-height: 0;
}
input[type="checkbox"]:checked~label~.expandContainer .expandContents {
max-height: 1000px;
}
<!-- language: lang-html -->
<input type="checkbox" id="expandme" />
<label for="expandme">Expand this content</label>
<div class="expandContainer">
<div class="expandContents">
<p>Expanded Content</p>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论