Search Button Animation in CSS & JS

Search Button Animation in Css & JS
Code Snippet:Search
Author: Soumyajit Pathak
Published: January 12, 2024
Last Updated: January 22, 2024
Downloads: 580
License: MIT
Edit Code online: View on CodePen
Read More

This CSS & JS code snippet helps you to create a stylish search button animation. The search button consists of a circular element and a dash. When clicked, it smoothly transforms, revealing a search bar. The animation is achieved through CSS transitions, and the JavaScript function toggles between the closed and opened states.

You can use this code on your website’s search bar for a modern and eye-catching animation.

How to Create Search Button Animation in CSS & JS

1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document.

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

2. Place the given HTML code within the <body> of your document. This includes a container div with a circular element and a hidden search bar.

<div class="container">
  <div class="closed" id="circle">
    <div class="cross"></div>
  </div>
  <input class="closed" id="dash"/>
</div>

3. Copy and paste the following code into your stylesheet or within a <style> tag. This CSS handles the button’s appearance and animation effects.

* {
  border: 0;
  margin: 0;
  padding: 0;
  border-radius: 0;
  box-sizing: border-box !important;
}

html,
body {
  background: #889;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}
html .container,
body .container {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 300px;
  height: 300px;
}
html .container #circle,
body .container #circle {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50px;
  height: 50px;
  border: solid 4px #fff;
  background: rgba(255, 255, 255, 0);
  border-radius: 50%;
  cursor: pointer;
  transition: all ease 0.3s;
}
html .container #circle .cross,
body .container #circle .cross {
  display: block;
  position: absolute;
  top: 50%;
  right: 0;
  z-index: 20;
  width: 30px;
  height: 30px;
  cursor: pointer;
  transform: translateY(-50%);
}
html .container #circle .cross:before, html .container #circle .cross:after,
body .container #circle .cross:before,
body .container #circle .cross:after {
  content: "";
  position: absolute;
  display: block;
  right: 7px;
  height: 4px;
  width: 4px;
  border-radius: 1px;
  transition: all 0.25s ease;
}
html .container #circle .cross:before,
body .container #circle .cross:before {
  top: 0px;
  background-color: #889;
  transform: rotate(-45deg);
  transform-origin: top right;
  transition-delay: 0.1s;
}
html .container #circle .cross:after,
body .container #circle .cross:after {
  bottom: 0px;
  background-color: #889;
  transform: rotate(45deg);
  transform-origin: bottom right;
}
html .container #circle.opened,
body .container #circle.opened {
  background: white;
  transform: translate(-250%, -50%);
}
html .container #circle.opened .cross:before, html .container #circle.opened .cross:after,
body .container #circle.opened .cross:before,
body .container #circle.opened .cross:after {
  width: 25px;
  right: 13px;
}
html .container #circle.opened .cross:before,
body .container #circle.opened .cross:before {
  top: 5px;
  transition-delay: 0.25s;
}
html .container #circle.opened .cross:after,
body .container #circle.opened .cross:after {
  bottom: 5px;
  transition-delay: 0.3s;
}
html .container #dash,
body .container #dash {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 0px;
  border-bottom: solid 4px #fff;
  border-radius: 5px;
  cursor: pointer;
  padding-left: 5px;
  transform-origin: left center;
  transition: all ease 0.3s;
  outline: rgba(255, 255, 255, 0);
}
html .container #dash.closed,
body .container #dash.closed {
  width: 25px;
  transform: translate(1em, 1em) rotate(45deg);
}
html .container #dash.opened,
body .container #dash.opened {
  width: 200px;
  height: 45px;
  border-radius: 0;
  transform: translate(-4.2em, -1.4em) rotate(0deg);
}

4. Finally, insert the following JavaScript code just before the closing </body> tag. This script enables the search button animation by toggling between the closed and opened states.

const $ = document.querySelector.bind(document);
const searchItems = [$('#circle'), $('#dash')];
const search = () => searchItems.forEach(x => x.className === 'closed' ? x.className = 'opened' : x.className = 'closed');
searchItems[0].addEventListener('click', search);

That’s all! hopefully, you have successfully created a search button animation on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X