Responsive Expandable Search Bar Animation

Responsive Expandable Search Bar Animation

Hello Coders! Welcome to another JavaScript Blog. In this, we're going to see how to create a responsive expandable search bar. Search feature is the common feature of any website which helps the user search for a particular thing. Here is a simple smooth animation of the search icon which you can use in your website.

Here's a preview -

search-bar.gif

That being said, let us get started.

Step - 1: Like always, create 3 files - index.html, style.css and script.js.

Step - 2: Copy the below HTML code and paste it into your code editor.

HTML


<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Expandable Search Bar - Animation</title>
      <script src="https://kit.fontawesome.com/aa09fdd343.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="./style.css">

</head>
<body>

<div class="container">
   <div>
      <input id="input" type="search" placeholder="type here whatever you want to search">
      <i class="fas fa-search"></i>
   </div>
</div>

  <script  src="./script.js"></script>

</body>
</html>

Step - 3: Below is the CSS code for styling.

CSS


* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
  transition: all cubic-bezier(0.68, -0.55, 0.27, 1.55) 520ms;
  outline: none;
  font-family: "Poppins";
}
::placeholder{
  color: #f6f7fc;
  opacity: 0.5;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  font-size: 15px;
}
.container {
  width: 100%;
  height: 100vh;
  background: #f6f7fc;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.container div {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.container div i.fas {
  position: absolute;
  display: flex;
  color: #f6f7fc;
}
.container div input {
  max-width: 500px;
  background: #232323;
  width: 70px;
  height: 70px;
  border: none;
  border-radius: 5px;
  padding: 20px 35px;
  color: #ffffff;
  font-family: "Poppins";
  cursor: pointer;
}
.container div input:focus {
  width: 40%;
  border-radius: 50px;
  box-shadow: 2px 31px 35px -15px rgba(0,0,0,0.64);
  transform: scale(1.18);
}
.container div input:focus i {
  display: none;
}
.container div input:not(:focus) {
  width: 70px;
  border-radius: 50px;
}
.container div input:hover {
  transform: scale(1.18);
}

Step - 4: Below is the JavaScript code which is the most important part in this animation. Some of the animation we're doing in this is coming from javascript.

JS


var input = document.getElementById("input");
var message = document.getElementsByClassName("fas")[0];
var animationTime = 400;
input.addEventListener("focus", function () {
  message.style.display = "none";
  message.style.transition = `cubic-bezier(0.68, -0.55, 0.27, 1.55)
                        ${animationTime}ms`;
});
input.addEventListener("focusout", function () {
  setTimeout(() => {
    message.style.display = "flex";
  }, animationTime);
});

And that's it. You're done.

Let me know in the comments if you have any doubt related to this.

Follow @creocodigo for more projects and web related content.

If you find this useful, below are some other posts that I am sure you'll love.