50天50个前端页面练习Day4:隐藏搜索栏

198 阅读1分钟

tt0.top-887168.gif 今天来实现上图的小功能,当点击搜索按钮时实现搜索栏的伸缩。

1.HTML

首先是画出骨架,搜索栏有一个搜索栏和一个图标组成

<div class="search">
    <input type="text" class="input" placeholder="请输入搜索内容">
    <button class="btn">
        <i class="fas fa-search"></i>
    </button>
</div>

完整代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />
    <link rel="stylesheet" href="style.css">
    <title>MyHiddenSearchWidget</title>
</head>
<body>
    <div class="search">
        <input type="text" class="input" placeholder="请输入搜索内容">
        <button class="btn">
            <i class="fas fa-search"></i>
        </button>
    </div>
<script src="script.js"></script>
</body>
</html>

2.CSS

先使用flex布局使得搜索框居中显示

body {
    background-color: #7bdaf3;
    font-family: 'Muli', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

在给搜索框设置好默认样式

.search {
    position: relative;
    height: 50px;
}

.search .input{
    background-color: #f6f7fb;
    border: 0;
    font-size: 18px;
    padding: 15px;
    height: 50px;
    width: 50px;
    transition: width 0.3s ease;
}
.btn {
    background-color: #fff;
    border: 0;
    cursor: pointer;
    font-size: 24px;
    position: absolute;
    top: 0;
    left: 0;
    height: 50px;
    width: 50px;
    transition: transform 0.3s ease;
}

设置当点击后样式的样子

.search.active .input {
    width: 200px;
}

.search.active .btn {
    transform: translateX(198px);
}

至此CSS完成了,完整代码如下。

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

* {
    box-sizing: border-box;
}

body {
    background-color: #7bdaf3;
    font-family: 'Muli', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

.search {
    position: relative;
    height: 50px;
}

.search .input{
    background-color: #f6f7fb;
    border: 0;
    font-size: 18px;
    padding: 15px;
    height: 50px;
    width: 50px;
    transition: width 0.3s ease;
}
.btn {
    background-color: #fff;
    border: 0;
    cursor: pointer;
    font-size: 24px;
    position: absolute;
    top: 0;
    left: 0;
    height: 50px;
    width: 50px;
    transition: transform 0.3s ease;
}
.btn:focus,
.input:focus {
    outline: none;
}
.search.active .input {
    width: 200px;
}

.search.active .btn {
    transform: translateX(198px);
}

3.JS

通过dom操作获取对象,当点击btn时,给search加上active属性,并且使焦点位于输入框上。当存在active属性时,移除属性。

classList.add( newClassName ): 添加新的类名,如已经存在,取消添加

classList.contains( oldClassName ): 确定元素中是否包含指定的类名,返回值为true 、false

classList.remove( oldClassName ): 移除已经存在的类名

classList.toggle( className ): 如果classList中存在给定的值,删除它,否则,添加它

classList.replace( oldClassName,newClassName ): 类名替换

const search = document.querySelector('.search')
const btn = document.querySelector('.btn')
const input = document.querySelector('.input')

btn.addEventListener('click', () => {
    search.classList.toggle('active')
    input.focus()
})