HTML
<div id="searchDiv">
<input type="search" placeholder=" 搜索" id="search">
<span id="serachIcon"><i class="fa fa-search"></i></span>
</div>
CSS
/*搜索*/
#searchDiv{
float: left;
margin-left: 50px;
width: 240px;
height: 40px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
}
#search{
padding-left: 20px;
width: 0px;
transition: all .3s linear ;
-moz-transition: all .3s linear;
-o-transition: all .3s linear;
-webkit-transition: all .3s linear;
opacity: 0; //完全透明
visibility: hidden; //不可见
display:inline-block;
height: 40px;
border-radius: 30px;
border-width: 0px;
background-color: #EEEEEE;
}
#search:focus{
outline:none;//设置获取焦点时外边框不亮
}
#searchDiv i{
display: inline-block;
font-size: 18px;
color:#B6B6B6;
width: 30px;
height: 30px;
line-height: 30px;
margin: 5px;
text-align: center;
border-radius: 50%;
background-color: #969696;
position: absolute;
top: 0px;
}
JS
//搜索框
document.getElementById("searchDiv").addEventListener("mouseover",function(){
document.getElementById('search').style.width='200px';
document.getElementById('search').style.opacity='1';
document.getElementById('search').style.visibility='visible';
});
document.getElementById("searchDiv").addEventListener("mouseout",function(){
document.getElementById('search').style.width='0px';
document.getElementById('search').style.opacity='0';
document.getElementById('search').style.visibility='hidden';
});
本文介绍了一个使用HTML, CSS和JavaScript实现的动态搜索框效果。搜索框初始状态下完全透明且不可见,当鼠标悬停在包含搜索图标的元素上时,搜索框将平滑地扩展到可见状态,并允许用户输入搜索查询。此效果利用了CSS的过渡属性和JavaScript的事件监听器。
2636

被折叠的 条评论
为什么被折叠?



