覆盖文本阻挡了背景图片的过渡。

huangapple go评论85阅读模式
英文:

Overlay text is blocking transition of background image

问题

如何阻止叠加文本阻挡背景图的悬停效果?

悬停效果正常运行
(https://i.stack.imgur.com/B5Rkb.jpg)

...只要我不正好悬停在叠加文本上
(https://i.stack.imgur.com/AWFty.jpg)

这是我的 SCSS 代码:

.home__featured-element {
    width: 30%;
    height: 100%;
    position: relative;

    img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: all 0.4s ease-in-out;
        &:hover {
            cursor: pointer;
            filter: brightness(70%);
        }
    }
    
    &::after {
        content: "ADD TO CART";
        position: absolute;
        opacity: 0;
        color: white;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    &:hover::after {
        opacity: 1;
    }
}

这是我的 JSX 代码:

<div className="home__featured-products-wrapper">
    <div className="home__featured-element">
        <img src={items[0].imgSrc} />
        <div>{items[0].name}</div>
        <div>${items[0].price}</div>
    </div>
    <div className="home__featured-element">
        <img src={items[4].imgSrc} />
        <div>{items[4].name}</div>
        <div>${items[4].price}</div>
    </div>
    <div className="home__featured-element">
        <img src={items[2].imgSrc} />
        <div>{items[2].name}</div>
        <div>${items[2].price}</div>
    </div>
    <div className="home__featured-element">
        <img src={items[3].imgSrc} />
        <div>{items[3].name}</div>
        <div>${items[3].price}</div>
    </div>
</div>
英文:

How can I stop an overlay text from blocking hover effect of a background image?

Hover effect works just fine
(https://i.stack.imgur.com/B5Rkb.jpg)

...As long as I don't hover exactly on the overlay text
(https://i.stack.imgur.com/AWFty.jpg)

This is my scss code:

.home__featured-element {
         width: 30%;
         height: 100%;
         position: relative;

                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;

                    transition: all 0.4s ease-in-out;
                    &amp;:hover {
                        cursor: pointer;
                        filter: brightness(70%);
                    }
                }
                
                &amp;::after {
                    content: &quot;ADD TO CART&quot;;
                    position: absolute;
                    opacity: 0;
                    color: white;
                    left: 50%;
                    top: 50%;
                    transform: translate(-50%, -50%);
                }
                &amp;:hover::after {
                    opacity: 1;
                }
            }

And this is my jsx code:

&lt;div className=&quot;home__featured-products-wrapper&quot;&gt;
            &lt;div className=&quot;home__featured-element&quot;&gt;
              &lt;img src={items[0].imgSrc} /&gt;
              &lt;div&gt;{items[0].name}&lt;/div&gt;
              &lt;div&gt;${items[0].price}&lt;/div&gt;
            &lt;/div&gt;
            &lt;div className=&quot;home__featured-element&quot;&gt;
              &lt;img src={items[4].imgSrc} /&gt;
              &lt;div&gt;{items[4].name}&lt;/div&gt;
              &lt;div&gt;${items[4].price}&lt;/div&gt;
            &lt;/div&gt;
            &lt;div className=&quot;home__featured-element&quot;&gt;
              &lt;img src={items[2].imgSrc} /&gt;
              &lt;div&gt;{items[2].name}&lt;/div&gt;
              &lt;div&gt;${items[2].price}&lt;/div&gt;
            &lt;/div&gt;
            &lt;div className=&quot;home__featured-element&quot;&gt;
              &lt;img src={items[3].imgSrc} /&gt;
              &lt;div&gt;{items[3].name}&lt;/div&gt;
              &lt;div&gt;${items[3].price}&lt;/div&gt;
            &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 0

你可以在伪元素 ::after 上添加 pointer-events: none,这样悬停效果会被图片“看到”。

这里是一个使用纯CSS/HTML演示的简化代码片段。

<style>
  .home__featured-element {
    width: 30%;
    height: 100%;
    position: relative;
  }
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: all 0.4s ease-in-out;
  }
  
  img:hover {
    cursor: pointer;
    filter: brightness(70%);
  }
  
  .home__featured-element::after {
    content: "ADD TO CART";
    position: absolute;
    opacity: 0;
    color: white;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  
  .home__featured-element:hover::after {
    opacity: 1;
    pointer-events: none;
  }
</style>
<div class="home__featured-products-wrapper">
  <div class="home__featured-element">
    <img src="https://picsum.photos/id/1016/300/300" />
    <div>name</div>
    <div>price</div>
  </div>
</div>

注意:你可能需要考虑这种方式在所有情况下是否有效——例如,屏幕阅读器用户可能无法获取相关信息。

英文:

You can put a pointer-events: none on the after pseudo element so the hover is 'seen' by the img.

Here's a simplified snippet using pure CSS/HTML to demonstrate.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;style&gt;
  .home__featured-element {
    width: 30%;
    height: 100%;
    position: relative;
  }
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: all 0.4s ease-in-out;
  }
  
  img:hover {
    cursor: pointer;
    filter: brightness(70%);
  }
  
  .home__featured-element::after {
    content: &quot;ADD TO CART&quot;;
    position: absolute;
    opacity: 0;
    color: white;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  
  .home__featured-element:hover::after {
    opacity: 1;
    pointer-events: none;
  }
&lt;/style&gt;
&lt;div class=&quot;home__featured-products-wrapper&quot;&gt;
  &lt;div class=&quot;home__featured-element&quot;&gt;
    &lt;img src=&quot;https://picsum.photos/id/1016/300/300&quot; /&gt;
    &lt;div&gt;name&lt;/div&gt;
    &lt;div&gt;price&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Note: You may like to consider whether this way of showing the text works well in all circumstances- screen reader users may not get told about it for example.

huangapple
  • 本文由 发表于 2023年6月29日 00:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定