简体   繁体   English

在 z 轴上将图像夹在孩子和父母之间

[英]Sandwich an image between a child and a parent on the z-axis

Right now I'm trying to set up a section with a hidden image that displays on hovering an a tag.现在我正在尝试设置一个带有隐藏图像的部分,该图像在悬停 a 标签时显示。 The problem is this hidden image should be vertically sandwiched between the child a tag and the parent h2 tag.问题是这个隐藏的图像应该垂直夹在子标签和父标签 h2 之间。

A quick mockup of the behavior I'm trying to get: mockup我试图获得的行为的快速模型: mockup

and here's how I have it set up right now:这是我现在的设置方式:

.about a {
pointer-events:auto;
}

.about-img {
pointer-events:none;

.about-img:hover {
background-image:url(about-image.jpeg);
background-size:36%;
background-repeat:no-repeat;
background-position:center center;

<section class="about-img">
    <h2>
      <a href="#">Hover Trigger</a>lorem ipsum.
    </h2>
</section>

This works getting me the desired behavior of the image only being visible when the user hovers over the a tag.这可以让我获得所需的图像行为,仅当用户将鼠标悬停在 a 标签上时才可见。 But I can't figure out a way to get the image underneath the a tag but on top of the rest of the text in the h2.但是我想不出一种方法可以将图像放在 a 标签下方,而是放在 h2.txt 中其余文本的顶部

afaik, you can't give a background-image a z-index. afaik,你不能给背景图像一个 z-index。 But I don't know how else to get the image to sit flush in the middle, underneath the child a tag but also above the parent h2 tag?但我不知道还有什么方法可以让图像齐平地坐在中间,在子标签下方但也在父 h2 标签上方?

I was trying to avoid using javascript for this, but I'm totally ok with using js if it can get me the behavior I'm after.我试图避免为此使用 javascript,但我完全可以使用 js,如果它能让我得到我所追求的行为。

Thanks for any advice you can offer!感谢您提供的任何建议!

You could try to set the z index of the image to one higher than the text's z index.您可以尝试将图像的 z 索引设置为比文本的 z 索引高一。 You would then have to set the anchor tag's z index higher than the one selected for the image.然后,您必须将锚标记的 z 索引设置为高于为图像选择的索引。

You would need to use an actual img tag instead of a background image and position it absolutely within their common container.您需要使用实际的 img 标签而不是背景图像,并将其绝对放置在它们的公共容器中。

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

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