html+css+js实现动态搜索框

html部分

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>动态搜索</title>

    <link rel="stylesheet" href="http://at.alicdn.com/t/c/font_3835490_k7nsdocsxd8.css">

    <link rel="stylesheet" href="动态搜索.css">

    <script src="动态搜索.js"></script>

</head>

<body>

    <div class="searchBar">

        <div class="icon">

            <i class="iconfont icon-sousuotubiao"></i>

        </div>

        <div class="textInput">

            <input class="inp" type="text" placeholder="请输入搜索关键字">

            <button class="goBtn">go</button>

            <div class="clear"><i class="iconfont icon-close"></i></div>

        </div>

    </div>

</body>

</html>

css部分

* {

    margin: 0;

    padding: 0;

    box-sizing: border-box;

}

body {

    width: 100vw;

    height: 100vh;

    background: linear-gradient(

        to bottom, skyblue, #003462);

    display: flex;

    justify-content: center;

    align-items: center;

}

.searchBar {

    width: 400px;

    height: 60px;

    background-color: #fff;

    box-shadow: 0 0 10px rgba(0, 0, 0, .4);

    border-radius: 60px;

    position: relative;

    overflow: hidden;

    transition: .5s;

}

.icon {

    width: 60px;

    height: 60px;

    display: flex;

    justify-content: center;

    align-items: center;

    cursor: pointer;

}

.icon i {

    color: dodgerblue;

    font-size: 30px;

}

.textInput {

    width: 320px;

    height: 60px;

    position: absolute;

    top: 0;

    left: 60px;

    display: flex;

    justify-content: center;

    align-items: center;

    background-color: blue;

}

.textInput input {

    width: 100%;

    height: 100%;

    border: none;

    outline: none;

    font-size: 18px;

}

.clear {

    width: 15px;

    height: 15px;

    position: absolute;

    right: 22%;

    top: 50%;

    transform: translateY(-50%);

    cursor: pointer;

    display: flex;

    justify-content: center;

    align-items: center;

}

.clear i {

    color: #999;

}

.goBtn {

    width: 12%;

    height: 60%;

    position: absolute;

    top: 20%;

    right: 0;

    border-radius: 8px;

    outline: none;

    border: none;

    color: #fff;

    box-shadow: 0 0 2px rgba(0, 0, 0, .4);

    background: linear-gradient(skyblue, deepskyblue);

    cursor: pointer;

}

.searchBar {

    width: 60px;

}

.changeWidth {

    width: 400px;

}

js部分

window.onload = function () {

    // 1.获取元素

    let searchIcon = document.querySelector(".icon")

    let clearIcon = document.querySelector(".clear")

    let searchBar = document.querySelector(".searchBar")

    let inp = document.querySelector(".inp")

    // 2.点击 切换类 动态设置宽度 实现展开

    searchIcon.addEventListener("click", () => {

        searchBar.classList.toggle("changeWidth")

    })

    // 3.点击清空

    clearIcon.addEventListener("click", () => {

        inp.value = ""

    })

}

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值