Поисковая строка с автоподстановкой аля Google
Для создания поисковой строки с автоподстановкой, которая визуально напоминает строку поиска Google, с учётом всех ваших требований, включая тёмный фон страницы и отправку поискового запроса по нажатию клавиши Enter, можно использовать следующий код:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Темная Google-подобная поисковая строка</title>
<style>
body {
background-color: #121212;
color: #fff;
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.search-container {
position: relative;
width: 400px;
}
input[type="text"] {
width: 100%;
padding: 10px 40px;
font-size: 16px;
border: 1px solid #333;
border-radius: 24px;
outline: none;
background-color: #2c2c2c;
color: #fff;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
transition: box-shadow 0.3s ease-in-out;
}
input[type="text"]:focus {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.6);
}
.icon {
position: absolute;
top: 50%;
left: 12px;
transform: translateY(-50%);
font-size: 18px;
color: #aaa;
pointer-events: none;
}
.autocomplete-suggestions {
position: absolute;
top: 100%;
left: 0;
right: 0;
border: 1px solid #333;
background-color: #2c2c2c;
border-radius: 0 0 24px 24px;
color: #fff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.6);
z-index: 10;
max-height: 200px;
overflow-y: auto;
}
.autocomplete-suggestion {
padding: 10px 20px;
cursor: pointer;
font-size: 16px;
}
.autocomplete-suggestion:hover {
background-color: #3b3b3b;
}
</style>
</head>
<body>
<div class="search-container">
<span class="icon">🔍</span>
<input type="text" id="search" placeholder="Поиск в Google или введите URL" onkeydown="checkEnter(event)">
<div id="autocomplete-list" class="autocomplete-suggestions"></div>
</div>
<script>
const words = ["apple", "banana", "orange", "grape", "mango", "melon", "cherry", "strawberry"];
const input = document.getElementById('search');
const suggestionsContainer = document.getElementById('autocomplete-list');
input.addEventListener('input', function() {
const query = this.value.toLowerCase();
suggestionsContainer.innerHTML = '';
if (!query) return;
const suggestions = words.filter(word => word.toLowerCase().includes(query));
suggestions.forEach(suggestion => {
const suggestionElement = document.createElement('div');
suggestionElement.textContent = suggestion;
suggestionElement.className = 'autocomplete-suggestion';
suggestionElement.onclick = function() {
input.value = suggestion;
suggestionsContainer.innerHTML = '';
};
suggestionsContainer.appendChild(suggestionElement);
});
});
function checkEnter(event) {
if (event.key === 'Enter') {
performSearch();
}
}
function performSearch() {
alert(`Поиск: ${input.value}`);
}
</script>
</body>
</html>
Описание:
- Поисковая строка визуально напоминает строку Google с элементом лупы внутри текстового поля.
- Фон страницы установлен в тёмный цвет с белым текстом для светлых акцентов.
- Пользователь может выбрать автоподстановку слов из заданного списка, и запрос выполнится по нажатию клавиши Enter.
- При нажатии Enter отображается сообщение с введённым запросом. В реальном приложении вы могли бы перенаправить пользователя на поисковую страницу или выполнить другое действие.