
本文旨在解决在将SVG hover效果集成到HTML card组件中时,hover效果失效的问题。通过分析问题代码,明确了失效原因在于z-index属性的设置。文章提供了修改方案,移除了`.center__div--heartFilled`的`z-index: -1`属性,从而使hover效果能够正常工作。同时,文章也展示了相关的HTML和CSS代码,方便读者理解和实践。
在开发Web应用时,经常会遇到需要在Card组件中添加交互效果的需求,例如,当鼠标悬停在Card的某个元素上时,改变其样式。本文将针对一种常见的问题进行详细讲解:当尝试在Card组件中集成SVG的hover效果时,效果可能无法正常工作。
问题分析
问题的核心在于,当SVG元素被放置在Card组件中,并且使用了z-index属性来控制元素的层叠顺序时,hover效果可能会失效。这通常是因为z-index属性的设置,导致鼠标事件无法正确地被SVG元素捕获。
立即学习“前端免费学习笔记(深入)”;
解决方案
解决这个问题的方法是移除导致hover效果失效的z-index属性。具体来说,我们需要检查CSS代码中,哪个元素设置了z-index: -1,并将其移除。
在提供的代码中,问题的根源在于.center__div--heartFilled元素上可能存在的z-index: -1属性(虽然在提供的CSS代码中没有明确出现,但根据问题描述推测可能存在)。
具体步骤
修改后的CSS代码示例
.center__div--heartFilled {
/* z-index: -1; 移除此行 */
}完整代码示例
下面是完整的HTML和CSS代码,其中已经移除了可能导致问题的z-index属性:
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; 移除此行 */
}
.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;
}注意事项
总结
通过移除.center__div--heartFilled元素上的z-index: -1属性,可以解决SVG hover效果在Card组件中失效的问题。在实际开发中,需要仔细分析问题代码,并根据具体情况进行调整。理解CSS的层叠上下文和事件冒泡机制,有助于更好地解决类似的问题。
以上就是解决CSS Hover效果在Card组件中失效的问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号