Author: battleKing warehouse: Github, CodePen blog: CSDN, nuggets feedback email: [email protected] Special statement: original is not easy, shall not be reproduced without authorization or plagiarism, if need to be reproduced can contact the author authorized
background
Placing a search box in the top menu bar is a very common scenario, but if the top menu bar is already crowded, putting a full search box in it doesn’t look pretty. So it’s also a pretty common practice to just put one search icon and display the whole search box when you need it. This is not only more beautiful and save a lot of display space, in the limited space to show as many things as possible, so it is favored by many enterprises.
Import the Font Awesome icon library
<script src="https://use.fontawesome.com/ab349f0a54.js" ></script>
Copy the code
Add HTML
<form>
<div class="search_box">
<input class="search" type="text" name="search" placeholder="Search...">
<i class="fa fa-search fa-lg" aria-hidden="true"></i>
</div>
</form>
Copy the code
Add the CSS
We need to set up a nice search box CSS style.
For the outermost.search_box we set.search and.fa-search in position: relative to position: absolute
for.search
Search box style
- Set the box model to
box-sizing: border-box
- The width is set to
width: 150px
- The border is set to
border: 5px solid #ccc
- Now web pages are popular with rounded corners, in order to keep the style consistent, we set the rounded corners to
border-radius: 5px
- Font size is set to
font-size: 16px
- The background color is usually set to the same as the navigation bar, but in this case it’s white
background-color: white
- Align the text with the search icon
padding: 12px 20px 12px 40px
- Sets the speed at which the animation changes
The transition: width 0.4 s ease - in-out
- Compatible with Chrome
- its - the transition: width 0.4 s ease - in-out
The core step is to set the width property to 100% when searching for focus
for.fa-search
Search icon style
- The purpose of setting up a prominent search icon is to inform the user that this is a search box
- The search icon is centered
top: 14px
和left: 14px
- Set the color of the search icon to the same as the text in the search box
color:#b2b2b2
.search_box{
position: relative;
}
.search {
width: 150px;
position: absolute;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 5px;
font-size: 16px;
background-color: white;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4 s ease-in-out;
transition: width 0.4 s ease-in-out;
}
.search:focus {
width: 100%;
}
.fa-search {
position: absolute;
top: 14px;
left: 14px;
color:#b2b2b2;
}
Copy the code
The final result
Add JS
If we want to implement something like Huawei search box, where other elements in the same row disappear when the search box is opened, then we need to use JS to manipulate the DOM.
Implementing this feature is simple
- We just need to add one of the following red box elements when the search box gets focus
class="hidden_box"
theclass
It containsvisibility:hidden
.- Delete when losing focus
class = "hidden_box"
.
New HTML section
<div class="nav_top">
<text>Home page</text>
<text>start</text>
<text>Mobile phone</text>
<text>The tablet</text>
<text>Laptop computer</text>
</div>
Copy the code
New CSS
.nav_top {
position: absolute;
right: 20px;
height: 46px;
line-height: 46px;
}
.hidden_box {
visibility: hidden;
}
Copy the code
JS part
document.querySelector(".search").addEventListener("focus",addClass);
document.querySelector(".search").addEventListener("blur",removeClass);
function addClass(){
document.querySelector(".nav_top").classList.add("hidden_box")}function removeClass(){
document.querySelector(".nav_top").classList.remove("hidden_box")}Copy the code
The final result
❤️ thank you
If this article is helpful to you, please support it by clicking a “like”. Your “like” is my motivation for writing.
If you like this article, you can “like” + “favorites” + “forward” to more friends.