Skip to content
Advertisements

JavaScript Search Box

Codepen:


HTML CODE:

 <div class="search-box">
        <input type="text" placeholder=" " /><span></span>
    </div>

CSS CODE:

html, body{
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    background: #1a1a1a;
}
body{
    display: flex;
    justify-content: center;
    align-items: center;
}
.search-box{
    display: inline-block;
    position: relative;
    border-radius: 50px;
    border: 5px solid #ccc;
}
.search-box span {
  width: 25px;
  height: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  bottom: -13px;
  right: -15px;
  transition: bottom 300ms ease-out 300ms, right 300ms ease-out 300ms;
}
.search-box span:before, .search-box span:after {
  content: '';
  height: 25px;
  border-left: solid 5px #ccc;
  position: absolute;
  transform: rotate(-45deg);
}
.search-box span:after {
  transform: rotate(45deg);
  opacity: 0;
  top: -20px;
  right: -10px;
  transition: top 300ms ease-out, right 300ms ease-out, opacity 300ms ease-out;
}
input {
    font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
    font-size: 20px;
    font-weight: bold;
    width: 54px;
    height: 54px;
    padding: 5px 40px 5px 10px;
    border: none;
    box-sizing: border-box;
    border-radius: 50px;
    background: #1a1a1a;
    transition: width 800ms cubic-bezier(0.5, -0.5, 0.5, 0.5) 600ms;
    color: #ccc;
}
.search-box input:focus {
    outline: none;
}
.search-box input:focus, .search-box input:not(:placeholder-shown) {
    width: 300px;
    transition: width 800ms cubic-bezier(0.5, -0.5, 0.5, 1.5);
}
.search-box input:focus + span, .search-box input:not(:placeholder-shown) + span {
    bottom: 13px;
    right: 10px;
    transition: bottom 300ms ease-out 800ms, right 300ms ease-out 800ms;
}
.search-box input:focus + span:after, .search-box input:not(:placeholder-shown) + span:after {
  top: 0;
  right: 10px;
  opacity: 1;
  transition: top 300ms ease-out 1100ms, right 300ms ease-out 1100ms, opacity 300ms ease 1100ms;
}

JS CODE:

  var searchBox = document.querySelectorAll('.search-box input[type="text"] + span');

        searchBox.forEach((elm) => {
            elm.addEventListener('click', () => {
                elm.previousElementSibling.value = '';
            });
        });
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: