简体   繁体   English

z-index 和变换问题

[英]Issue with z-index and transform

I am trying to create a dropdown menu with secondary and tertiary sub menus.我正在尝试创建一个带有二级和三级子菜单的下拉菜单。 The sub menus are supposed to slide out from underneath the parent menus.子菜单应该从父菜单下方滑出。 The actual code has several divs nested in multiple levels.实际代码有多个嵌套在多个级别中的 div。

I have added a transform of translateY(700px) to the secondary-menu element to make it slide from underneath the primary-menu.我已将translateY(700px)的转换添加到辅助菜单元素,以使其从主菜单下方滑动。 However that causes the tertiary-menu to slide over the secondary-menu and not underneath it.但是,这会导致三级菜单滑过二级菜单,而不是在其下方。

If the transform is removed the tertiary-menu works fine.如果删除了转换,则第三级菜单可以正常工作。 A z-index of -1 was provided on tertiary-menu but that does not help.在三级菜单上提供了 -1 的z-index但这无济于事。 I have tried other combination of z-index on secondary and tertiary menus but it does not work.我在二级和三级菜单上尝试了 z-index 的其他组合,但它不起作用。

There are quite a few related questions on this site but they don't help in my case.这个网站上有很多相关的问题,但对我来说没有帮助。 Here is the stripped down version of code to recreate the issue:这是重新创建问题的精简版代码:

 .primary-menu { position: relative; margin-left: 50px; z-index: 100; background-color: grey; height: 50px; padding: 20px; }.primary-menu:hover~.secondary-menu { transform: translateY(0); visibility: visible; height: 500px; }.secondary-menu:hover { transform: translateY(0); visibility: visible; height: 500px; /* actually height is not known in advance, this value is put here just for this example */ }.secondary-menu { position: absolute; width: 150px; top: 50px; left: 50px; background-color: red; transform: translateY(-700px); transition: transform 1s; visibility: hidden; }.category { padding: 20px; position: relative; }.tertiary-menu { position: absolute; left: 100%; visibility: hidden; padding: 20px; top: 0; left: 0; transition: left 1s; z-index: -1; background-color: hotpink; }.category:hover.tertiary-menu { visibility: visible; left: 100%; }
 <div class="primary-menu">Menu</div> <div class="secondary-menu"> <div class="category"> <h4>Mobiles</h4> <div class="tertiary-menu"> <div class="sub-category"> <h4>Smartphones</h4> <div class="product-types">Mobile Cases, Covers</div> <div class="product-types">Power Banks</div> </div> <div class="sub-category"> <h4>Feature Phones</h4> <div class="product-types">Samsung</div> </div> </div> </div> </div>

The sandbox link is here沙盒链接在这里

The 'z-index' you put in the '.tertiary-menu' is working.您放入“.tertiary-menu”的“z-index”正在工作。

The reason it doesn't seem to work is because '.tertiary-menu' is a children of '.category'.它似乎不起作用的原因是因为“.tertiary-menu”是“.category”的子级。

The background of '.category' is transparent, so the '.tertiary-menu' below is visible. “.category”的背景是透明的,所以下面的“.tertiary-menu”是可见的。

Simply add the background color and height to '.category' and it will look like this.只需将背景颜色和高度添加到“.category”,它就会看起来像这样。

 * { margin: 0; padding: 0; box-sizing: border-box; }.primary-menu { position: relative; margin-left: 50px; z-index: 100; background-color: grey; height: 50px; padding: 20px; }.primary-menu:hover~.secondary-menu { transform: translateY(0); visibility: visible; height: 500px; }.secondary-menu:hover { transform: translateY(0); visibility: visible; height: 500px; /* actually height is not known in advance, this value is put here just for this example */ }.secondary-menu { position: absolute; width: 150px; top: 50px; left: 50px; background-color: red; transform: translateY(-700px); transition: transform 1s; visibility: hidden; }.category { padding: 20px; position: relative; height: 100%; background-color: blue; }.tertiary-menu { position: absolute; left: 100%; visibility: hidden; padding: 20px; top: 0; left: 0; transition: left 1s; z-index: -1; background-color: hotpink; }.category:hover.tertiary-menu { visibility: visible; left: 100%; }
 <div class="primary-menu">Menu</div> <div class="secondary-menu"> <div class="category"> <h4>Mobiles</h4> <div class="tertiary-menu"> <div class="sub-category"> <h4>Smartphones</h4> <div class="product-types">Mobile Cases, Covers</div> <div class="product-types">Power Banks</div> </div> <div class="sub-category"> <h4>Feature Phones</h4> <div class="product-types">Samsung</div> </div> </div> </div> </div>

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

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