英文:
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;
&: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;
}
}
And this is my jsx code:
<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>
答案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 -->
<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>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论