css代码,样式的设置
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-image:inherit;
font-family: "微软雅黑";
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.search{
height: 50px;
position: relative;
}
.search input{
background-color: cornflowerblue;
border: 0;
font-size: 18px;
height: 50px;
width: 50px;
transition: width 0.3s ease;
}
.btn{
background-color: palegreen;
border: 0;
color: red;
cursor: pointer;
padding: 15px;
height: 50px;
width: 50px;
transition: transform 0.3 ease;
position: absolute;
left: 0;
top: 0;
font-size: 24px;
}
.btn :focus,input:focus{
outline: 0;
}
.search.active input{
width: 200px;
}
.search.active button{
transform: translateX(200px);
}
</style>```
**
全代码可以直接使用
**
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>搜索</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-image:inherit;
font-family: "微软雅黑";
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.search{
height: 50px;
position: relative;
}
.search input{
background-color: cornflowerblue;
border: 0;
font-size: 18px;
height: 50px;
width: 50px;
transition: width 0.3s ease;
}
.btn{
background-color: palegreen;
border: 0;
color: red;
cursor: pointer;
padding: 15px;
height: 50px;
width: 50px;
transition: transform 0.3 ease;
position: absolute;
left: 0;
top: 0;
font-size: 24px;
}
.btn :focus,input:focus{
outline: 0;
}
.search.active input{
width: 200px;
}
.search.active button{
transform: translateX(200px);
}
</style>
</head>
<body>
<div class="search">
<input type="text" class="input" placeholder="搜索" />
<button class="btn">
<i class="fas fa"></i>
</button>
</div>
<script>
const search=document.querySelector('.search');
const btn=document.querySelector('.btn');
const input =document.querySelector('.input');
btn.addEventListener('click',()=>{
search.classList.toggle('active');
input .focus();
})
</script>
</body>
</html>