搜索框

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

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';
});

 

<think>我们正在讨论HTML搜索框的实现和使用。根据用户提供的引用,特别是引用[1]中展示了一个基础的表单提交方式的搜索框,而引用[2]提到了使用querySelector和addEventListener方法来实现交互式搜索栏。用户的问题集中在“HTML搜索栏实现使用”,因此我们需要结合之前提供的详细回答和引用内容,给出一个更贴合用户需求的解答。###核心思路:1.**基础表单实现**:直接使用HTML的form表单,通过GET方法提交到搜索引擎(如必应)。2.**样式自定义**:使用CSS美化搜索框。3.**交互功能**:使用JavaScript实现实时搜索建议、自定义搜索引擎等。###具体实现步骤:####1.基础表单搜索(引用[1]的方式)```html<!DOCTYPEhtml><html><head><title>搜索</title></head><body><formaction="https://cn.bing.com/search"method="get"><inputtype="text"name="q"placeholder="请输入关键词"><inputtype="submit"value="搜索"></form></body></html>```-说明:这种方式是最简单的,用户输入关键词后,点击提交按钮,页面会跳转到必应搜索的结果页面。####2.美化搜索框样式(使用CSS)我们可以为上面的表单添加样式,使其更加美观。```html<!DOCTYPEhtml><html><head><title>搜索</title><style>.search-container{display:flex;width:500px;margin:50pxauto;}.search-containerinput[type="text"]{flex:1;padding:10px;font-size:16px;border:2pxsolid#4CAF50;border-radius:4px004px;}.search-containerinput[type="submit"]{padding:10px20px;background:#4CAF50;color:white;border:none;border-radius:04px4px0;cursor:pointer;}</style></head><body>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值