Jes*_*ica 6 javascript css animation
我正在尝试使用css为元素高度设置动画.我这样做的方式是,我为元素添加了一个触摸事件.该函数将a添加className到应该隐藏的元素,即获得高度为0.
问题是,当单击元素时,div假设高度为0暂停一秒,然后获得所需的高度.似乎动画持续时间越长,它在动画之前等待的时间就越长.
这是相关的代码:
transition: max-height 2s ease-in-out;
Run Code Online (Sandbox Code Playgroud)
var heading = document.getElementById('heading'),
body = document.getElementById('body');
heading.addEventListener('click', hide);
body.addEventListener('transitionend', hideCallback);
function hide() {
body.className = 'hide';
}
function hideCallback(e) {
console.log(e.propertyName);
}Run Code Online (Sandbox Code Playgroud)
#wrapper {
margin: auto;
background-color: blue;
width: 470px;
text-align: center;
overflow: hidden;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.17);
max-height: 400px;
max-width: 600px;
padding: 0;
}
#buttonContainer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
align-content: center;
flex-direction: column;
margin: auto;
width: 100px;
height: 100px;
color: white;
}
#buttonIcon {
width: 36px;
height: 36px;
line-height: 36px;
font-size: 35px;
font-weight: bold;
display: inline-block;
}
#buttonLabel {
font-size: 20px;
}
#content {
width: 100%;
height: 100%;
text-align: left;
transform: translateY(0px);
}
#heading {
color: white;
text-align: right;
margin: 10px;
font-size: 17px;
}
#body {
color: #000;
color: rgba(0, 0, 0, 0.87);
background-color: #FFF;
width: 100%;
max-height: 500px;
padding: 10px 0;
box-sizing: border-box;
}
#body.hide {
transition: max-height 2s ease-in-out;
max-height: 0;
}
#innerBody {
height: 200px;
background-color: orange;
}Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
<div id="buttonContainer"><span id="buttonIcon">My</span><span id="buttonLabel">self</span>
</div>
<div id="content">
<div id="heading">Press Me</div>
<div id="body">
<div id="innerBody"></div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
你是在动画max-height而不是在动画height。动画max-height是一种用于对高度未知的元素的高度进行动画处理的技巧。您指定一个非常大的max-height,只要未知高度小于 max-height ,它就可以工作。唯一的缺点是,由于您制作的动画比您的实际高度大,因此会出现延迟。在这种情况下,你的max-height高度是 500px,高度(包括填充)只有 220px,这意味着超过时间(约 1.1 秒)只是将最大高度减少到 220px,当它到达那里时,视觉开始动画化。
如本例所示,如果您知道元素的实际高度 (220 = padding 10 + insideBody height 200),则可以为确切的高度设置动画。
如果您事先不知道高度,请尝试降低max-height估计值并使用快速启动的缓动,例如ease-out或使用 javascript 设置开始和结束高度。
已知(全屏height: 220px观看):
var heading = document.getElementById('heading'),
body = document.getElementById('body');
heading.addEventListener('click', hide);
body.addEventListener('transitionend', hideCallback);
function hide() {
body.className = 'hide';
}
function hideCallback(e) {
console.log(e.propertyName);
}Run Code Online (Sandbox Code Playgroud)
#wrapper {
margin: auto;
background-color: blue;
width: 470px;
text-align: center;
overflow: hidden;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.17);
max-height: 400px;
max-width: 600px;
padding: 0;
}
#buttonContainer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
align-content: center;
flex-direction: column;
margin: auto;
width: 100px;
height: 100px;
color: white;
}
#buttonIcon {
width: 36px;
height: 36px;
line-height: 36px;
font-size: 35px;
font-weight: bold;
display: inline-block;
}
#buttonLabel {
font-size: 20px;
}
#content {
width: 100%;
height: 100%;
text-align: left;
transform: translateY(0px);
}
#heading {
color: white;
text-align: right;
margin: 10px;
font-size: 17px;
}
#body {
color: #000;
color: rgba(0, 0, 0, 0.87);
background-color: #FFF;
width: 100%;
height: 220px;
padding: 10px 0;
box-sizing: border-box;
}
#body.hide {
transition: height 2s ease-in-out;
height: 0;
}
#innerBody {
height: 200px;
background-color: orange;
}Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
<div id="buttonContainer"><span id="buttonIcon">My</span><span id="buttonLabel">self</span>
</div>
<div id="content">
<div id="heading">Press Me</div>
<div id="body">
<div id="innerBody"></div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
使用 javascript 未知高度 - 在动画开始之前设置实际高度,等待下一帧,并将其更改为 0(全屏观看):
var heading = document.getElementById('heading'),
body = document.getElementById('body');
heading.addEventListener('click', hide);
body.addEventListener('transitionend', hideCallback);
function hide() {
body.style.height = body.scrollHeight + 'px'; /** sample actual height, and set it on element **/
requestAnimationFrame(function() { // wait for next frame
body.style.height = 0; // change height to 0
});
}
function hideCallback(e) {
console.log(e.propertyName);
}Run Code Online (Sandbox Code Playgroud)
#wrapper {
margin: auto;
background-color: blue;
width: 470px;
text-align: center;
overflow: hidden;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.17);
max-height: 400px;
max-width: 600px;
padding: 0;
}
#buttonContainer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
align-content: center;
flex-direction: column;
margin: auto;
width: 100px;
height: 100px;
color: white;
}
#buttonIcon {
width: 36px;
height: 36px;
line-height: 36px;
font-size: 35px;
font-weight: bold;
display: inline-block;
}
#buttonLabel {
font-size: 20px;
}
#content {
width: 100%;
height: 100%;
text-align: left;
transform: translateY(0px);
}
#heading {
color: white;
text-align: right;
margin: 10px;
font-size: 17px;
}
#body {
color: #000;
color: rgba(0, 0, 0, 0.87);
background-color: #FFF;
width: 100%;
padding: 10px 0;
box-sizing: border-box;
transition: height 2s ease-in-out;
}
#innerBody {
height: 200px;
background-color: orange;
}Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
<div id="buttonContainer"><span id="buttonIcon">My</span><span id="buttonLabel">self</span>
</div>
<div id="content">
<div id="heading">Press Me</div>
<div id="body">
<div id="innerBody"></div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)