<!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</title>

  <link rel="stylesheet" href="style.css">

</head>
<body>

  <div class="search">
    <input type="text" class="input" placeholder="Search...">
    <button class="btn">
      <i class="iconfont icon-search"></i>
    </button>
  </div>
  
  <script src="./script.js"></script>
</body>
</html>
@import "../assets//iconfont/iconfont.css";

*{
  box-sizing: border-box;
}

body{
  background-color: #8EC5FC;
  background-image: linear-gradient(156deg, #8EC5FC 0%, #E0C3FC 50%, #a5b4fc 100%);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
}

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

.input{
  width: 50px;
  height: 50px;
  padding: 15px;
  border: 0;
  background-color: #fff;
  font-size: 18px;
  transition: width 0.5s ease;
}

.btn{
  position: absolute;
  right: 0;
  top: 0;
  width: 50px;
  height: 50px;
  border: 0;
  font-size: 24px;
  font-weight: 700;
  background-color: #fff;
  cursor: pointer;
}

.btn:focus,
.input:focus
{
  outline: none;
}

.search.active .input{
  width: 200px;
}
const search = document.querySelector('.search')
const btn = document.querySelector('.btn')
const input = document.querySelector('.input')

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

下图为结果展示

20220611083720

20220611083730

Q.E.D.


一个二次元web开发咸鱼