[英]Accordion close animation not working. It opens (with animation) and closes (with no animation)
I'm pretty new to javascript, so this might be a dumb question. 我对javascript很陌生,所以这可能是一个愚蠢的问题。
My open Animation works great, but the close animation doesn't work/fire. 我的开放动画效果很好,但封闭动画无法工作/触发。 it should just be as simple as reversing the animation, but that doesn't seem to work.
它应该和反转动画一样简单,但这似乎行不通。
I have to be missing something here. 我必须在这里丢失一些东西。 Is there a better way to do this?
有一个更好的方法吗?
Been trying to figure this out all day. 一直试图解决这一问题。
HTML 的HTML
<div class="divTable">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell">
<button type="button" id="id" onclick="btnDetails_Click('id')">
<p id="id-ButtonText">Details ▼</p>
</button>
</div>
<div class="divTableCell">
<p>Name</p>
</div>
<div class="divTableCell">
<p>Details</p>
</div>
</div>
<div class="divTableFoot" style="display:none" id="id-DetailsPanel">
<div class="divTableCell" style="display:flex; height:0; overflow:hidden" id="id-DetailsPanel2">
<div style="margin-right:20px">
<img style="height:400px" src="http://via.placeholder.com/200x400" />
</div>
<div style="width:auto">
<p>Description</p>
</div>
</div>
</div>
</div>
</div>
CSS 的CSS
/* DivTable.com */
.divTable {
display: table;
width: 100%;
}
.divTableRow {
display: table-row;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
}
.divTableCell,
.divTableHead {
border: 1px solid #999999;
display: table-cell;
padding: 3px 10px;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
font-weight: bold;
}
.divTableFoot {
width:100%;
}
.divTableBody {
display: table-row-group;
}
JavaScript 的JavaScript
function btnDetails_Click(id) {
document.getElementById("id-DetailsPanel").style = "display:inherit";
document.getElementById("id").setAttribute("onclick", "btnDismiss_Click('id')");
document.getElementById("id-ButtonText").innerHTML = "Details ▲";
expand(id)
}
function btnDismiss_Click(id) {
contract(id)
document.getElementById("id-ButtonText").innerHTML = "Details ▼";
document.getElementById("id").setAttribute("onclick", "btnDetails_Click('id')");
document.getElementById("id-DetailsPanel").style = "display:none";
}
function expand(id) {
var detailsBox = document.getElementById("id-DetailsPanel2");
var boxHeight = 0;
var int = setInterval(animate, 8);
function animate() {
if (boxHeight == 425) {
clearInterval(int);
} else {
boxHeight = boxHeight + 25;
detailsBox.style.height = boxHeight + 'px';
}
}
}
function contract(id) {
var detailsBox = document.getElementById("id-DetailsPanel2");
var boxHeight = 425;
var int = setInterval(animate, 8);
function animate() {
if (boxHeight == 0) {
clearInterval(int);
} else {
boxHeight = boxHeight - 25;
detailsBox.style.height = boxHeight + 'px';
}
}
}
here's the JSFiddle: https://jsfiddle.net/1zryo2Lp/ 这是JSFiddle: https ://jsfiddle.net/1zryo2Lp/
Because before the setInterval
got a chance to execute document.getElementById("id-DetailsPanel").style = "display:none";
因为在
setInterval
之前有机会执行document.getElementById("id-DetailsPanel").style = "display:none";
happened. 发生了 A simple fix would be to move
display:none
line after clearInterval(int);
一个简单的解决方法是将
display:none
移到clearInterval(int);
function btnDetails_Click(id) { document.getElementById("id-DetailsPanel").style = "display:inherit"; document.getElementById("id").setAttribute("onclick", "btnDismiss_Click('id')"); document.getElementById("id-ButtonText").innerHTML = "Details ▲"; expand(id) } function btnDismiss_Click(id) { contract(id) document.getElementById("id-ButtonText").innerHTML = "Details ▼"; document.getElementById("id").setAttribute("onclick", "btnDetails_Click('id')"); // document.getElementById("id-DetailsPanel").style = "display:none"; } function expand(id) { var detailsBox = document.getElementById("id-DetailsPanel2"); var boxHeight = 0; var int = setInterval(animate, 8); function animate() { if (boxHeight == 425) { clearInterval(int); } else { boxHeight = boxHeight + 25; detailsBox.style.height = boxHeight + 'px'; } } } function contract(id) { var detailsBox = document.getElementById("id-DetailsPanel2"); var boxHeight = 425; var int = setInterval(animate, 8); function animate() { if (boxHeight == 0) { clearInterval(int); document.getElementById("id-DetailsPanel").style = "display:none"; } else { boxHeight = boxHeight - 25; detailsBox.style.height = boxHeight + 'px'; } } }
.divTable { display: table; width: 100%; } .divTableRow { display: table-row; } .divTableHeading { background-color: #EEE; display: table-header-group; } .divTableCell, .divTableHead { border: 1px solid #999999; display: table-cell; padding: 3px 10px; } .divTableHeading { background-color: #EEE; display: table-header-group; font-weight: bold; } .divTableFoot { width:100%; } .divTableBody { display: table-row-group; }
<div class="divTable"> <div class="divTableBody"> <div class="divTableRow"> <div class="divTableCell"> <button type="button" id="id" onclick="btnDetails_Click('id')"> <p id="id-ButtonText">Details ▼</p> </button> </div> <div class="divTableCell"> <p>Name</p> </div> <div class="divTableCell"> <p>Details</p> </div> </div> <div class="divTableFoot" style="display:none" id="id-DetailsPanel"> <div class="divTableCell" style="display:flex; height:0; overflow:hidden" id="id-DetailsPanel2"> <div style="margin-right:20px"> <img style="height:400px" src="http://via.placeholder.com/200x400" /> </div> <div style="width:auto"> <p>Description</p> </div> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.