搜索框

本文介绍了一个使用HTML, CSS和JavaScript实现的动态搜索框效果。搜索框初始状态下完全透明且不可见,当鼠标悬停在包含搜索图标的元素上时,搜索框将平滑地扩展到可见状态,并允许用户输入搜索查询。此效果利用了CSS的过渡属性和JavaScript的事件监听器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML

 <div id="searchDiv">
     <input type="search" placeholder="&nbsp;&nbsp;搜索" 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';
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值