隐藏搜索小部件(hidden search widget)

在这里插入图片描述
在这里插入图片描述
来源github,参考链接50 Projects In 50 Days - HTML, CSS & JavaScript

描述:
点击放大镜可以展开收起搜索框

重点:
1、css操作宽度
2、类名操作

实现代码:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hidden search widget</title>
  <link rel="stylesheet" href="../css/index.css">
</head>
<body>
  <div class="search">
    <input type="text" class="input" placeholder="输入并搜索...">
    <button class="search-btn" id="searchBtn">
      <i class="search-icon"></i>
    </button>
  </div>
</body>
<script src="../js/index.js"></script>
</html>

index.css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #E9ECEF;
  height: 100vh;
}
.search {
  height: 45px;
  position: relative;
}
.search.active > .input {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}
.search.active > .search-btn {
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.search.active > .input {
  width: 300px;
}
.search.active > .search-btn {
  transform: translateX(298px);
}
.search > .search-btn,
.search > .input {
  border: none;
  width: 45px;
  height: 45px;
  outline: none;
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  border-radius: 10px;
}
.search > .input {
  background-color: #fff;
  display: inline-block;
  padding: 15px;
  font-size: 18px;
}
.search > .search-btn {
  background-color: #fff;
  text-align: center;
  line-height: 45px;
  cursor: pointer;
  position: absolute;
  left: 0;
  top: 0;
}
.search > .search-btn:hover .search-icon {
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transform: scale(1.2);
}
.search > .search-btn > .search-icon{
  background: url('../images/icon-search.svg')no-repeat center/cover;
  width: 20px;
  height: 20px;
  display: block;
  margin: auto;
}

index.js

const $ = v => document.querySelector(v);
const searchBtn = $("#searchBtn");
const searchContainer = $(".search");
const searchInput = $(".input");
searchBtn.addEventListener("click", () => {
  searchContainer.classList.toggle("active");
  searchInput.focus();
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值