巧妙解决鼠标悬停时图标被遮挡的问题
网站或应用中,鼠标悬停效果能提升用户体验。然而,有时悬停时背景图标会被新的背景颜色遮盖。本文通过案例分析,讲解如何解决此问题。
问题描述
一个搜索框,右侧图标在鼠标悬停时背景颜色改变,但图标却被新背景色遮挡:
相关CSS代码:
.tx_mmenu_together .donate-btn-header .lytop_search {
height: 40px;
margin: 0;
padding: 0;
float: left;
position: relative;
z-index: 1;
}
.tx_mmenu_together .donate-btn-header .lytop_search form {
width: 60px;
height: 40px;
margin: 0;
padding: 0;
position: relative;
}
.tx_mmenu_together .donate-btn-header .lytop_search form .sc_ipt {
width: 0;
height: 40px;
overflow: hidden;
margin: 0;
padding: 0;
position: absolute;
left: 0;
top: 0;
transition: all 0.5s;
}
.tx_mmenu_together .donate-btn-header .lytop_search form .sc_ipt input {
display: block;
width: 100%;
height: 40px;
overflow: hidden;
line-height: 40px;
color: #999999;
font-size: 14px;
margin: 0;
padding: 0 14px;
background-color: #fff;
border-radius: 20px 0 0 20px;
border: 1px solid #dfdfdf;
border-right: 0;
outline: none;
box-sizing: border-box;
}
.tx_mmenu_together .donate-btn-header .lytop_search form .sc_btn {
width: 60px;
height: 40px;
overflow: hidden;
margin: 0;
padding: 0;
border-radius: 0 20px 20px 0;
float: right;
}
.tx_mmenu_together .donate-btn-header .lytop_search form .sc_btn input {
display: block;
width: 60px;
height: 40px;
overflow: hidden;
margin: 0;
padding: 0;
background: #fff url('../img/home/search.png') center center no-repeat;
background-size: 40px 40px;
border: 0;
outline: none;
cursor: pointer;
}
.tx_mmenu_together .donate-btn-header .lytop_search:hover form .sc_ipt {
width: 260px;
left: -260px;
}
.tx_mmenu_together .donate-btn-header .lytop_search:hover form .sc_btn input {
background: #1a75bc url('../img/home/search-.png') center center no-repeat;
}
问题分析
悬停时,图标背景色从#fff变为#1a75bc,图片也从search.png变为search-.png。问题在于search-.png颜色设置可能与#1a75bc不匹配,导致图标不可见。
解决方案
只需确保悬停时背景图颜色与悬停背景色一致即可。修改后的CSS代码:
.tx_mmenu_together .donate-btn-header .lytop_search form .sc_btn input {
background: #fff url('../img/home/search.png') center center no-repeat;
background-size: 40px 40px; /* 添加背景大小设置 */
}
.tx_mmenu_together .donate-btn-header .lytop_search:hover form .sc_btn input {
background: #1a75bc url('../img/home/search-.png') center center no-repeat;
background-size: 40px 40px; /* 添加背景大小设置 */
}
通过确保search-.png颜色与#1a75bc匹配,悬停时图标清晰可见。 此处也补充了background-size属性,确保图片大小合适。
通过简单调整,即可解决图标被遮挡的问题,希望对您有所帮助。










