简体   繁体   English

尝试使用HTML5 / CSS3 / JS创建带有z-index的堆叠菜单

[英]Trying to create a stacked menu with z-index' with HTML5/CSS3/JS

I have looked all over for a solution but can't find anything specific and I know it is very doable but I think my lack of experience with JavaScript is limiting me here. 我到处都在寻找解决方案,但是找不到任何具体的解决方案,我知道这是非常可行的,但是我认为我对JavaScript的缺乏经验限制了我的工作。 Essentially I have 5 divs, each one acts like a button. 本质上,我有5个div,每个div的行为都像一个按钮。 I want each button to make another div unrelated to the 5 buttons to appear. 我希望每个按钮都使另一个div与出现的5个按钮无关。 I called them "Menubox1/2/3/4" and each one is linked to each one. 我称它们为“ Menubox1 / 2/3/4”,每个都链接到每个。 My first idea was to have all the menus stacked on top of each other with a blank one on top with a z-index of 5. 我的第一个想法是将所有菜单堆叠在一起,空白菜单的z索引为5。

And then by using the "onclick" method on the div button, I would tell it to get the element of its respective menu and increase its z-index to 6, to go on top of the pile. 然后,通过使用div按钮上的“ onclick”方法,我将告诉它获取其相应菜单的元素,并将其z-index增大到6,以使其位于堆的顶部。 I would also tell it to make all the other menus go to 4, to go below the rest. 我还要告诉它使其他所有菜单都变为4,使其低于其余菜单。

So effectively each button would make the menu associated with it appear on top with the highest z-index and also hide the other menu boxes so that no other box is on the same level. 如此有效地使每个按钮使与其关联的菜单以最高的z-index出现在顶部,并且还隐藏其他菜单框,以使其他框都不在同一级别上。

I have found a way to make it work with this JS 我找到了一种使其与此JS一起工作的方法

//Shows the current menu on top
  function showstuff(boxid) {
            document.getElementById(boxid).style.zIndex="5";
        }
//Hides the rest of them.
        function hidestuff(boxid) {
            document.getElementById(boxid).style.zindex="3";
        }



<div onclick="showstuff('Menu1'); hidestuff('Menu2'); hidestuff('Menu3');" ;style="cursor:pointer;">

And by putting the respective boxid in the "onclick" , it all works but the problem is it only works once, and I need it to work every time each one is clicked. 通过将相应的boxid放在“ onclick”中,所有方法都可以使用,但问题是它只能使用一次,每次单击时我都需要它起作用。

If anyone needs more information I an provide. 如果有人需要更多信息,请提供。

Why don't you just use visibility:hidden/visible on the panes you want to hide/display? 您为什么不只使用visibility:hidden/visible要隐藏/显示的窗格上的visibility:hidden/visible It would require resetting that attribute on each pane on each click, but you don't have to worry about how browsers render z-index, or if there are any times in rendering where you get a weird display of multiple divs rendering when only one should. 这将需要在每次单击时在每个窗格上重置该属性,但是您不必担心浏览器如何渲染z-index,或者在渲染中是否有任何时候只有一次一个显示多个div渲染的怪异显示。应该。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM