CSS 问题:当其他元素位于“绝对”位置时,页面上的链接变得不可点击。

发布于 2024-12-09 17:47:53 字数 505 浏览 1 评论 0 原文

这是一个较长的故事,我想简短地讲一下。一般来说,我正在使用一个网站菜单,该菜单应该在部分透明的背景 gif 图像下部分滑动,并且仅在鼠标悬停时完全显示自己。为此,我在背景图像和菜单上使用 z-index 参数。但由于你不能在身体背景图像上使用 z-index,所以我使用“常规”图像,将其设置为 100% 宽度和高度 - 并且为了使 z-index 参数正常工作,我需要也指定“位置”。看起来,通过这个组合,我基本上创建了一个隐形的盾牌,使所有链接都无法触及。我将其简化为以下几行:

<style>
#style {
    position: absolute;
    width:100%;
    height:100%;
}
</style>

<div id="style"></div>
<a href="">test</a>

如果您尝试这样做,您将看到“测试”链接不可点击(跨浏览器)。

有谁知道我该如何解决这个问题?谢谢!

This is a longer story I'm trying to cut short. Generally I'm playing around with a website menu that is supposed to partly slide under a partly transparent background gif image, and fully reveal itself only upon mouseover. To do that, I'm using the z-index parameter on both the background image and the menu. But since you can't use z-index on a body background image, I'm using a "regular" image, which I'm setting to 100% width and height - AND for the z-index paramenter to work, I need to specify "position" as well. It seems though that with that combo, I'm basically creating an invisible shield that'll make all links untouchable. I've cooked it down to the following lines:

<style>
#style {
    position: absolute;
    width:100%;
    height:100%;
}
</style>

<div id="style"></div>
<a href="">test</a>

If you try this, you will see that the "test" link is unclickable (cross-browser).

Does anyone have an idea how I can solve this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥有 2024-12-16 17:47:53
<style>
#style {
    background-color:#ccc;
    position: absolute;
    width:100%;
    height:100%;
}
a {position:relative} /*won't change position of the link, but shows link above.*/
</style>



<div id="style"></div>
<a href="#">test</a>
<style>
#style {
    background-color:#ccc;
    position: absolute;
    width:100%;
    height:100%;
}
a {position:relative} /*won't change position of the link, but shows link above.*/
</style>



<div id="style"></div>
<a href="#">test</a>
梦萦几度 2024-12-16 17:47:53
<style>
#style {
    position: absolute;
    width:100%;
    height:100%;
    z-index: -1;
}

.test {
    z-index: 99;
}
</style>

<div id="style"></div>
<a href="" class="test">test</a>

也将与campino2k的答案一起工作。

<style>
#style {
    position: absolute;
    width:100%;
    height:100%;
    z-index: -1;
}

.test {
    z-index: 99;
}
</style>

<div id="style"></div>
<a href="" class="test">test</a>

Will work too, along with campino2k's answer.

心头的小情儿 2024-12-16 17:47:53

感谢您的回复,它们为我指明了正确的方向。看起来 div 确实创建了一个隐形的防护罩,并且当涉及到底层链接时,该防护罩(或多或少)是无法穿透的。

点击 DIV 到底层元素

@Logan:恐怕这种方法对我不起作用。您建议简单地将链接提升到 div 盾牌上方 - 然而,这违背了我上面描述的最初目的(带有背景图像和在其下方滑动的菜单的目的)。

@campino:我认为就是这样,但是向“样式”添加 z-index 定义再次破坏了它。您为整个 div 字段着色的事实帮助我理解了您显然已经知道的内容:只要 div 位于链接上方,它就不可点击,句号。

总而言之,我的结论是我的方法行不通。对于实际项目,我可能会将不对称背景图像分割成几块,因此 div 不会覆盖整个屏幕,而仅出现在我绝对需要它的地方。

Thanks for the replies, which pointed me in the right direction. It seems like the div does indeed create an invisible shield, and that shield is (more or less) inpenetrable when it comes to underlying links.

Click through a DIV to underlying elements

@Logan: I'm afraid that approach doesn't work for me. You're suggesting to simply raise the link above the div shield - that, however, defeats the original purpose I've described above (the one with the background image and the menu sliding underneath it).

@campino: I thought this was it, but adding a z-index definition to "style" broke it again. The fact that you colored the entire div field helped me understand what you obvously already knew: As long as the div is over the link, it's not clickable, period.

So all in all, I'm concluding that my approach doesn't work. For the actual project I'll probably cut up my asymmetric background image into several pieces, so the div doesn't cover the entire screen, and is only where I absolutely need it.

寄居者 2024-12-16 17:47:53

我认为设置 z-index 虽然可能有效,但并不能真正解决问题,而是一种可以实现您想要的效果的 hack。

无法点击链接的根本原因主要是元素通过浮动、显示或位置属性定位不当。此元素显示在链接的前景中,创建一个阻止您单击该链接的防护罩。

我发现的解决方案是使用 javascript/jqueryconsole.logalert id 或 <单击时前台元素的 code>class 。

$('*').click(function (){
   alert('class = ' + $(this).attr('class') + ' id = '+ $(this).attr('id')); 
});

上面将提醒前景中的元素。现在你知道了原因,看看它的风格。

I think setting a z-index, though it might work doesn't really address the problem but a kind of a hack that achieves what you want.

The root cause of unclickable links is mostly an element that is improperly positioned through floating, display, or position property. This element is displayed in the foreground of your link creating a shield that prevents you from clicking the link.

The solution to this I found is to use javascript/jquery to console.log or alert the id or class of the element in the foreground when you click.

$('*').click(function (){
   alert('class = ' + $(this).attr('class') + ' id = '+ $(this).attr('id')); 
});

above will alert the element in the foreground. Now that you know the cause look at its style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文