
本文旨在解决在HTML Card组件中集成SVG时,Hover效果失效的问题。通过分析问题代码,确定了`z-index`属性是导致Hover效果失效的根本原因,并提供了详细的解决方案,帮助开发者在Card组件中正确实现SVG的Hover效果。
在网页开发中,我们经常需要在Card组件中添加一些交互元素,例如SVG图标,并为其添加Hover效果以增强用户体验。然而,有时我们会遇到这样的问题:在独立的SVG元素上,Hover效果工作正常,但将其集成到Card组件中后,Hover效果却失效了。本文将深入探讨这个问题的原因,并提供详细的解决方案。
问题分析
根据提供的信息,问题的关键在于,当SVG元素被放置在Card组件中时,.card__heart:hover .svg__heart--filled选择器无法正确触发。这通常是由于CSS层叠、定位或z-index等因素引起的。
立即学习“前端免费学习笔记(深入)”;
解决方案
经过分析,问题代码中z-index:-1 属性是导致Hover效果失效的根本原因。当.center__div--heartFilled元素设置了z-index:-1时,它会被放置在其他元素的下方,从而无法接收到Hover事件。
移除z-index:-1
解决这个问题最直接的方法就是从.center__div--heartFilled类中移除z-index:-1属性。
修改后的CSS代码如下:
.center__div--heartFilled {
/* z-index: -1; Remove this line */
}完整代码示例
以下是完整的HTML和CSS代码示例,展示了如何在Card组件中正确实现SVG的Hover效果:
HTML:
<div class="card restaurant__card">
<a href="#">
<div class="card__image">
<img src="https://zupimages.net/up/21/31/oomv.jpg">
</div>
<div class="card__body">
<div class="card__title">
<h3>À la française</h3>
<p>Cité Rouge</p>
</div>
<div class="card__heart">
<div class="center__div center__div--heartFilled">
<svg class="svg__heart--filled" width="29" height="29" viewBox="0 0 16 16">
<defs>
<linearGradient id="grad1" x1="0%" y1="100%" x2="50%" y2="0%">
<stop offset="0%" class="color-primary" />
<stop offset="100%" class="color-secondary" />
</linearGradient>
</defs>
<path d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8 1.314z" fill="url(#grad1)"></path>
</svg>
</div>
<div class="center__div">
<svg class="svg__heart--outline" width="30" height="30" fill="currentColor" viewBox="0 0 16 16">
<path d="m8 2.748-.717-.737C5.6.281 2.514.878 1.4 3.053c-.523 1.023-.641 2.5.314 4.385.92 1.815 2.834 3.989 6.286 6.357 3.452-2.368 5.365-4.542 6.286-6.357.955-1.886.838-3.362.314-4.385C13.486.878 10.4.28 8.717 2.01L8 2.748zM8 15C-7.333 4.868 3.279-3.04 7.824 1.143c.06.055.119.112.176.171a3.12 3.12 0 0 1 .176-.17C12.72-3.042 23.333 4.867 8 15z" />
</svg>
</div>
</div>
</div>
</a>
</div>CSS:
body {
background: black;
}
body a {
text-decoration: none;
color: black;
}
.card {
width: 90%;
height: 225px;
margin-bottom: 25px;
border-radius: 15px;
overflow: hidden;
}
.card__image {
height: 160px;
}
.card__image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.card__body {
height: 65px;
width: 100%;
display: flex;
align-items: center;
flex-flow: row wrap;
justify-content: space-between;
background: white;
}
.card__title {
padding-left: 15px;
display: flex;
flex-flow: column wrap;
gap: 3px;
}
.card__title h3 {
margin-top: 0px;
margin-bottom: 0px;
}
.card__title p {
margin-top: 0px;
margin-bottom: 0px;
}
.card__heart {
margin-right: 40px;
border: solid black 1px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 32px;
height: 32px;
}
/* styliser les svg heart */
.center__div {
width: 32px;
height: 32px;
position: absolute;
cursor: pointer;
}
.center__div--heartFilled {
/* z-index: -1; Remove this line */
}
.svg__heart--filled {
opacity: 0;
transition: all 500ms ease-in-out;
}
.card__heart:hover .svg__heart--filled {
opacity: 1;
}
/*.
.svg__heart--filled {
opacity: 1;
}
*/
.svg__heart--outline {
background: rgba(0, 0, 0, 0);
}
.color-primary {
stop-color: yellow;
stop-opacity: 1;
}
.color-secondary {
stop-color: red;
stop-opacity: 1;
}注意事项
总结
当SVG的Hover效果在独立元素中有效,但在Card组件中失效时,通常是由于CSS层叠、定位或z-index等因素引起的。通过移除不必要的z-index属性,并确保CSS选择器的优先级正确,可以解决这个问题,并在Card组件中正确实现SVG的Hover效果。在实际开发中,需要仔细分析问题的具体情况,并根据需要调整CSS代码,才能达到最佳效果。
以上就是解决CSS Hover效果在独立SVG中有效,但在Card组件中失效的问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号