js 实现简易点击切换显示或隐藏
程序员文章站
2022-07-03 08:52:01
点击切换显示隐藏
关闭
打开
* {margin:0;padding:0;}#header {width:100%;height....<div id="header">
<p>点击切换显示隐藏</p>
<div class="close" onclick="closeTask()">关闭</div>
</div>
<div class="open" onclick="openTask()">打开</div>
* {
margin:0;
padding:0;
}
#header {
width:100%;
height:50px;
background-color:cadetblue;
text-align:center;
color:white;
position:relative;
overflow:hidden;
transition:all 0.5s;
}
#header>p {
line-height:50px;
}
.close {
position:absolute;
right:5%;
top:0;
padding:10px;
line-height:30px;
cursor:pointer;
}
.open {
position:absolute;
right:5%;
padding:10px;
line-height:30px;
background-color:cadetblue;
cursor:pointer;
transition:all 0.5s;
color:white;
}
var oDiv = document.getElementById("header");
var oOpen = document.getElementsByClassName("open");
var oDivheight = oDiv.offsetHeight;
var oOpenheight = oOpen[0].offsetHeight;
oOpen[0].style.top = `-${oOpenheight}px`;
function closeTask() {
oDiv.style.height = "0";
oOpen[0].style.top = "0";
}
const openTask = () => {
oDiv.style.height = `${oDivheight}px`;
oOpen[0].style.top = `-${oOpenheight}px`;
}
本文地址:https://blog.csdn.net/qq_42363032/article/details/110225783