负 z 索引会淘汰链接
我正在尝试向我的页面添加侧边栏。页面上的主体容器有一个盒子阴影,所以我希望侧边栏看起来好像它是从容器下方出来的,这样阴影就会在它的上面。我将侧边栏 div 设为主体容器的直接子级(其位置:相对),并将其位置设置为绝对位置,然后使用顶部和右侧位置值对其进行定位。我把它放在正确的位置,然后应用负 z 索引,使其位于身体下方。问题是,这使得我放在侧边栏中的任何链接在除 IE9 之外的所有浏览器中都无法单击。我不知道在不删除链接的情况下我还能如何完成此任务。有什么想法吗?
我会发布一个指向显示示例的页面的链接,但我正在积极对其进行更改,因此当您单击它时,您可能不会看到我要做什么。我会尽力解释得更好。
正文容器的宽度为 720 像素,并具有自动边距,因此它显示在页面的中心。它是相对定位的。
侧边栏是主体容器的直接子级(第一个子级)。它具有固定宽度、绝对位置、填充等,并应用了顶部和右侧位置,以及 -100 的 z 索引。
这是一个链接:
I'm trying to add a sidebar to my page. The main body container on the page has a box-shadow, so I want the sidebar to appear as though it's coming out from underneath the container so the shadow will be on top of it. I made my sidebar div a direct child of the body container (which has position: relative), and set it's position to absolute, then positioned it using the top and right position values. I got it in the right place, then applied a negative z-index so that it would be under the body. The problem is, this is making any links that I put in the sidebar unclickable in all but IE9. I don't know how else I can accomplish this without knocking out the links. Any ideas?
I would post a link to a page showing an example, but I'm actively making changes to it, so by the time you clicked it you probably wouldn't see what I'm going for. I'll try to explain better.
The body container is 720px wide and has an auto margin so that it appears centered in the page. It is positioned relative.
The sidebar is a direct child (the first child) of the body container. It has a fixed width, position absolute, padding, etc. and has a top and right position applied, along with a z-index of -100.
Here's a link:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以删除负
z-index
并提供内阴影 到侧边栏,与.body
元素的外部阴影相同。您必须尝试一下,看看它如何影响侧边栏的边框。
You can remove the negative
z-index
and give an inner shadow to the sidebar that is the same as the outer shadow of the.body
element.You´d have to try it to see how it affects the border of the sidebar.
我不完全理解需要什么效果,但也许这个简单的小提琴可以给出一些关于如何解决此类问题的提示。
jsfiddle
让链接正常工作的方法是将 z-index 切换回正数。您的 CSS 将如下所示:
您的 JS 应该是:
单击
#div-on-top
会将其移开,显示#div-on-bottom
及其还将把#div-on-bottom
带到顶部,使链接可点击。我还在
#div-on-top
上应用了阴影,它看起来不错(对我来说;参见 jsfiddle)。I don't fully understand what effect is desired but maybe this barebones fiddle can give some hints as for how to approach problems of such kind.
jsfiddle
The way to get links to work is to toggle z-index back to a positive number. Your CSS will look like:
and your JS should be:
Clicking on
#div-on-top
will move it out of the way revealing#div-on-bottom
and it will also bring#div-on-bottom
to the top, making links clickable.I also applied shadow to the
#div-on-top
and it looked ok (to me; see jsfiddle).