用html和js写一个搜索框可以自定义搜索引擎简约风扁平化
时间: 2023-05-27 18:02:13 浏览: 90
<!DOCTYPE html>
<html>
<head>
<title>自定义搜索引擎</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
#search-wrapper {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
#search-box {
display: flex;
align-items: center;
padding: 10px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 5px #ccc;
margin-bottom: 20px;
width: calc(100% - 40px);
max-width: 500px;
}
#search-box input[type="text"] {
flex: 1;
border: none;
margin-left: 10px;
font-size: 16px;
width: 100%;
outline: none;
}
#search-box button {
padding: 5px 10px;
background-color: #007bff;
border: none;
color: #fff;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
margin-left: 10px;
transition: background-color 0.2s ease-in-out;
}
#search-box button:hover {
background-color: #0069d9;
}
#search-engines {
display: flex;
flex-direction: column;
align-items: center;
}
.engine-item {
display: flex;
align-items: center;
padding: 5px 10px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 5px #ccc;
margin-bottom: 10px;
max-width: 500px;
width: calc(100% - 40px);
cursor: pointer;
transition: background-color 0.2s ease-in-out;
text-align: center;
}
.engine-item:hover {
background-color: #f2f2f2;
}
.engine-item img {
width: 30px;
height: auto;
margin-right: 10px;
}
.engine-item p {
margin: 0;
flex: 1;
text-align: left;
}
</style>
</head>
<body>
<div id="search-wrapper">
<div id="search-box">
<select id="engine-select">
<option value="https://www.google.com/search?q={searchTerm}">Google</option>
<option value="https://www.baidu.com/s?wd={searchTerm}">百度</option>
<option value="https://cn.bing.com/search?q={searchTerm}">Bing</option>
</select>
<input type="text" id="search-input" placeholder="请输入搜索内容">
<button id="search-btn">搜索</button>
</div>
<div id="search-engines">
<div class="engine-item" data-value="https://www.google.com/search?q={searchTerm}">
<img src="https://www.google.com/favicon.ico" alt="Google">
<p>Google</p>
</div>
<div class="engine-item" data-value="https://www.baidu.com/s?wd={searchTerm}">
<img src="https://www.baidu.com/favicon.ico" alt="百度">
<p>百度</p>
</div>
<div class="engine-item" data-value="https://cn.bing.com/search?q={searchTerm}">
<img src="https://cn.bing.com/favicon.ico" alt="Bing">
<p>Bing</p>
</div>
</div>
</div>
<script type="text/javascript">
let searchInput = document.getElementById("search-input");
let searchBtn = document.getElementById("search-btn");
let engineSelect = document.getElementById("engine-select");
let engineItems = document.querySelectorAll(".engine-item");
let selectedEngine = engineSelect.value;
// 搜索按钮点击事件
searchBtn.addEventListener("click", function() {
let searchTerm = searchInput.value.trim();
if (searchTerm !== "") {
let searchUrl = selectedEngine.replace("{searchTerm}", searchTerm);
window.location.href = searchUrl;
}
});
// 搜索引擎选择器变化事件
engineSelect.addEventListener("change", function() {
selectedEngine = this.value;
});
// 搜索引擎列表项点击事件
engineItems.forEach(function(item) {
item.addEventListener("click", function() {
selectedEngine = this.getAttribute("data-value");
engineSelect.value = selectedEngine;
});
});
</script>
</body>
</html>
阅读全文