Javascript 在dropbox上单击后隐藏图像

Javascript 在dropbox上单击后隐藏图像,javascript,html,css,Javascript,Html,Css,在我的导航栏中单击dropbox后,我需要隐藏图像。单击后如何隐藏它? 这是我的剧本: var menuList=document.getElementById(“menuList”); menuList.style.maxHeight=“0px”; 函数切换菜单(){ 如果(menuList.style.maxHeight==“0px”){ menuList.style.maxHeight=“130px”; }否则{ menuList.style.maxHeight=“0px”; } } 使

在我的导航栏中单击dropbox后,我需要隐藏图像。单击后如何隐藏它? 这是我的剧本:


var menuList=document.getElementById(“menuList”);
menuList.style.maxHeight=“0px”;
函数切换菜单(){
如果(menuList.style.maxHeight==“0px”){
menuList.style.maxHeight=“130px”;
}否则{
menuList.style.maxHeight=“0px”;
}
}

使用
menuList.style.display
更改对象的可见性,而不是更改高度

if(menuList.style.maxHeight==“0px”)
>
if(menuList.style.display==“无”)
menuList.style.maxHeight=“130px”>
menuList.style.display=“无”
menuList.style.maxHeight=“0px”>
menuList.style.display=“block”(或
menuList.style.display=“”;


看看

在处理这种行为时,我建议使用类

比如说

const menuList=document.getElementById('menuList');
const-button=document.getElementById('menu-button');
const openClass='菜单列表--打开';
函数切换(){
if(menuList.classList.contains(openClass)){
button.style.display=“内联块”;
menuList.classList.remove(openClass)
}
否则{
button.style.display=“无”;
menuList.classList.add(openClass)
}
}
addEventListener('单击',()=>toggle())
menuList.addEventListener('click',()=>toggle())
。菜单列表{
最大高度:0;
过渡:.3s缓进缓出最大高度;
背景色:红色;
高度:130像素;
}
.菜单列表--打开{
最大高度:130像素;
}
#菜单按钮{
位置:绝对位置;
顶部:20px;
}
点击我

使用一些html来了解您要做的事情会很有用。这取决于您是否希望它具有动画效果。id=“menu button”是“我的图像”按钮的id。我忘了包括它。我需要图像按钮在单击时不可见。因此,如果我单击图像,dropbox将下降,图像将消失