【问题标题】:Why is the hover effect overlapping?为什么悬停效果重叠?
【发布时间】:2017-11-21 06:01:26
【问题描述】:

当我将鼠标悬停在按钮上时,悬停效果与按钮的图标和标签重叠。我试图通过放置 z-index: 999; 将图标/标签 div 的图层移动到悬停效果之上,但它不起作用。

HTML

<div class="service-wrapper">
    <div class="services">
        <a href="#"><div class="button">
            <img src="https://png.icons8.com/?id=42205&size=36" class="iconBtn">
            <div class="serv-name">TECHNICAL SUPPORT</div>
        </div></a>
    </div>
    <div class="services">
        <a href="#"><div class="button">
            <img src="https://png.icons8.com/?id=7495&size=36" class="iconBtn"> 
            <div class="serv-name">CUSTOMER SERVICE</div>
        </div></a>
    </div>
</div>

CSS

.button {
    height: 40px;
    width: 230px;
    margin: 10px;
    padding: 5px;
    padding-right: 15px;
    padding-left: 15px;
    border-radius: 5px;
    background: #ffbb11; 
    text-align: center;
    color: #000000;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: space-around;
    overflow: hidden;
    position: relative;
}

 a {
    text-decoration:  none;
} 

.button::before,
.button::after {
    background: rgba(255, 255, 255, 1.0);
    content: '';
    position: absolute;
}

.button::after {
    height: 70px;
    left: -22%;
    top: 0;
    transform: skew(50deg);
    transition-duration: 0.3s;
    transform-origin: top;
    width: 0;
    z-index: 0;
}

.button:hover:after {
   height: 60px;
   width: 325px;
}

.iconBtn{
   max-height: 85%;
   max-width: 85%;
}

img:hover: {
   z-index: 999;
}

JSFIDDLE

【问题讨论】:

    标签: html css


    【解决方案1】:

    首先,您将z-index:999 附加到img:hover,这意味着您必须悬停图像才能应用它。您还在img:hover 之后添加了一个额外的:,所以它就像img:hover:

    此外,在标签中添加z-index:999 确实可以解决问题。你可以在这里看到结果:

    .button {
    	height: 40px;
    	width: 230px;
     	margin: 10px;
        padding: 5px;
    	padding-right: 15px;
    	padding-left: 15px;
    	border-radius: 5px;
        background: #ffbb11; 
    	text-align: center;
    	color: #000000;
    	font-weight: bold;
    	display: flex;
        align-items: center;
    	justify-content: space-around;
    	overflow: hidden;
      	position: relative;
    }
     a {
      text-decoration:  none;
    }
    .button::before,
    .button::after {
    	background: rgba(255, 255, 255, 1.0);
    	content: '';
    	position: absolute;
    }
    
    .button::after {
    	height: 70px;
    	left: -22%;
    	top: 0;
    	transform: skew(50deg);
      	transition-duration: 0.3s;
      	transform-origin: top;
    	width: 0;
      	z-index: 0;
    }
    .button:hover:after {
    	height: 60px;
    	width: 325px;
    }
    .iconBtn{
    	max-height: 85%;
    	max-width: 85%;
        z-index: 999;
    }
    .serv-name{
      z-index:999;
    }
        <div class="service-wrapper">
        <div class="services">
            <a href="#"><div class="button">
                <img src="https://png.icons8.com/?id=42205&size=36" class="iconBtn">
                <div class="serv-name">TECHNICAL SUPPORT</div>
            </div></a>
        </div>
        <div class="services">
            <a href="#"><div class="button">
                <img src="https://png.icons8.com/?id=7495&size=36" class="iconBtn"> 
                <div class="serv-name">CUSTOMER SERVICE</div>

    【讨论】:

    • 是的,这行得通。我最初有.button .iconBtn{ z-index: 999;}.button .serv-name{ z-index: 999;},但也没有用。非常感谢清理!
    • 好吧,我不明白为什么这不起作用 - 也许它们被放置在 img:hover: 部分或其他可能导致故障的错误之后
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2014-01-20
    • 2015-01-12
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多