写一款jQuery+CSS3实现的带有form标签和动画效果的搜索框代码,能创意展开收缩搜索框特效,每行代码添加注释,并提供框内放大镜图标的下载地址,谢谢
时间: 2024-06-06 08:06:34 浏览: 139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Search Box</title>
<style>
body {
background-color: #f1f1f1;
}
.search-container {
position: relative;
margin: 50px auto;
width: 300px;
height: 50px;
overflow: hidden;
}
.search-container input[type="text"] {
width: 100%;
height: 100%;
font-size: 16px;
padding: 5px 10px;
border: none;
outline: none;
background-color: #fff;
box-sizing: border-box;
transition: all 0.3s ease-in-out;
}
.search-container input[type="text"]:focus {
width: 200%;
}
.search-container input[type="submit"] {
position: absolute;
right: 0;
top: 0;
width: 50px;
height: 50px;
background-color: #27ae60;
color: #fff;
font-size: 16px;
border: none;
outline: none;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
.search-container input[type="submit"]:hover {
background-color: #2ecc71;
}
.search-container .search-icon {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
width: 20px;
height: 20px;
background-image: url('magnifier.png');
background-repeat: no-repeat;
background-size: contain;
}
</style>
</head>
<body>
<div class="search-container">
<form>
<input type="text" placeholder="Search...">
<input type="submit" value="Go">
<div class="search-icon"></div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var form = $('.search-container form');
var input = form.find('input[type="text"]');
var submit = form.find('input[type="submit"]');
var icon = form.find('.search-icon');
// 点击放大镜图标展开搜索框
icon.click(function() {
input.focus();
});
// 输入框失去焦点时收缩搜索框
input.blur(function() {
if (input.val() == '') {
form.removeClass('active');
}
});
// 输入框获得焦点时展开搜索框
input.focus(function() {
form.addClass('active');
});
// 提交表单
form.submit(function(e) {
e.preventDefault();
alert('You searched for: ' + input.val());
});
});
</script>
</body>
</html>
<!-- 放大镜图标下载地址:https://www.flaticon.com/free-icon/search_1498528 -->
阅读全文