我想让一个 div 与其所有内容一起折叠。我成功了。 这是我的代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Div Collapsing</title>
<style type="text/css">
#con{
width:60%;
height:60px;
background:#333;
overflow:hidden;
padding:5px;
}
#text{
margin:5px;
margin-top:20px;
height:auto;
background:#FFF;}
#myPlus{
width:50px;
height:50px;
background:#FFF;
line-height:50px;
text-align:center;
font-size:36px;
cursor:pointer;}
@keyframes myCol{
from{height:60px;}
to{height:200px;}
}
</style>
<script type="text/javascript">
function my(){
document.getElementById("con").style.height = "200px";
document.getElementById("con").style.animationName = "myCol";
document.getElementById("con").style.animationDuration = "1s";
}
</script>
</head>
<body>
<div id="con">
<div onclick="my();" id="myPlus" >+</div>
<div id="text">
`Sample text<br />
Sample text<br />
Sample text<br />
Sample text<br />
Sample text<br />
Sample text<br />
</div>
</div>
</body>
</html>
当我点击 + 符号时,我的 #con
div 折叠了 200px,但我需要折叠这个 div,就像 div #text
的需要一样。例如,如果 div #text
需要 500px,则折叠 500px,如果 100px,则折叠 1000px;
怎么办?
抱歉英语不好
最佳答案
不幸的是,您无法从 height: auto
到特定像素值进行动画处理。但是,如果您从 height: auto
开始制作动画,则可以将其添加到 my()
var con = document.getElementById('con');
con.style.height = con.offsetHeight + 'px'
关于javascript - div折叠自动高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104776/