<div class="search-box-container">
<div class="search-box-demo">
<div class="search-header">
<h2>Search Our Website</h2>
<p>Find what you're looking for quickly</p>
</div>
<div class="search-wrapper" id="searchWrapper">
<div class="search-box" id="searchBox">
<div class="search-icon">π</div>
<input type="text" class="search-input" id="searchInput" placeholder="Search products, articles, or topics...">
<button class="clear-btn" id="clearBtn">Γ</button>
<button class="search-btn" id="searchBtn">Search</button>
</div>
<div class="suggestions-dropdown" id="suggestionsDropdown">
<div class="suggestion-group">
<div class="group-title">Popular Searches</div>
<div class="suggestion-item" data-value="wireless headphones">
<span class="suggestion-icon">π§</span>
<span class="suggestion-text">Wireless Headphones</span>
</div>
<div class="suggestion-item" data-value="smartphone">
<span class="suggestion-icon">π±</span>
<span class="suggestion-text">Smartphone</span>
</div>
<div class="suggestion-item" data-value="laptop">
<span class="suggestion-icon">π»</span>
<span class="suggestion-text">Laptop</span>
</div>
</div>
<div class="suggestion-group">
<div class="group-title">Recent Searches</div>
<div class="suggestion-item" data-value="bluetooth speaker">
<span class="suggestion-icon">π</span>
<span class="suggestion-text">Bluetooth Speaker</span>
</div>
<div class="suggestion-item" data-value="smart watch">
<span class="suggestion-icon">β</span>
<span class="suggestion-text">Smart Watch</span>
</div>
</div>
</div>
</div>
</div>
</div>
.search-box-container {
background: linear-gradient(135deg, #f8f9ff 0%, #ffffff 100%);
padding: 40px 20px;
border-radius: 20px;
max-width: 600px;
margin: 0 auto;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.05);
}
.search-box-demo {
background: white;
border-radius: 15px;
padding: 30px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.03);
}
.search-header {
text-align: center;
margin-bottom: 30px;
}
.search-header h2 {
margin: 0 0 10px 0;
color: #333;
font-size: 2rem;
font-weight: 700;
}
.search-header p {
color: #666;
font-size: 1.1rem;
margin: 0;
}
.search-wrapper {
position: relative;
}
.search-box {
position: relative;
display: flex;
align-items: center;
background: #f8fafc;
border: 2px solid #e2e8f0;
border-radius: 12px;
padding: 5px;
transition: all 0.3s ease;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}
.search-box:focus-within {
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
background: white;
}
.search-box.focused {
border-radius: 12px 12px 0 0;
}
.search-icon {
font-size: 1.3rem;
color: #999;
padding: 0 15px;
transition: color 0.3s ease;
}
.search-box:focus-within .search-icon {
color: #667eea;
}
.search-input {
flex: 1;
padding: 15px 0;
border: none;
background: transparent;
font-size: 1.1rem;
color: #333;
outline: none;
}
.search-input::placeholder {
color: #999;
}
.search-input:focus::placeholder {
color: #cbd5e1;
}
.clear-btn {
width: 36px;
height: 36px;
border: none;
background: #f1f5f9;
color: #999;
border-radius: 50%;
font-size: 1.2rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
margin: 0 5px;
opacity: 0;
visibility: hidden;
transform: scale(0.8);
}
.search-input:not(:placeholder-shown) + .clear-btn,
.search-input:focus + .clear-btn {
opacity: 1;
visibility: visible;
transform: scale(1);
}
.clear-btn:hover {
background: #e2e8f0;
color: #666;
transform: scale(1.1);
}
.search-btn {
padding: 15px 25px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
margin-left: 5px;
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
.search-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}
.suggestions-dropdown {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: white;
border: 2px solid #e2e8f0;
border-top: none;
border-radius: 0 0 12px 12px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
z-index: 100;
max-height: 300px;
overflow-y: auto;
}
.search-box.focused + .suggestions-dropdown {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.suggestion-group {
padding: 15px 0;
border-bottom: 1px solid #f1f5f9;
}
.suggestion-group:last-child {
border-bottom: none;
}
.group-title {
padding: 0 20px 10px;
color: #667eea;
font-size: 0.85rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
}
.suggestion-item {
display: flex;
align-items: center;
gap: 15px;
padding: 12px 20px;
cursor: pointer;
transition: all 0.2s ease;
}
.suggestion-item:hover {
background: #f8fafc;
}
.suggestion-item.active {
background: #667eea;
color: white;
}
.suggestion-icon {
font-size: 1.2rem;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
}
.suggestion-text {
font-size: 1rem;
font-weight: 500;
}
.suggestion-item.active .suggestion-text {
color: white;
}
.recent-search {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 20px;
border-bottom: 1px solid #f1f5f9;
}
.recent-search:last-child {
border-bottom: none;
}
.remove-search {
background: none;
border: none;
color: #999;
cursor: pointer;
font-size: 1.2rem;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
transition: all 0.2s ease;
}
.remove-search:hover {
background: #fee2e2;
color: #ef4444;
}
/* Animations */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes slideInDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.search-header {
animation: fadeInUp 0.6s ease-out;
}
.search-box {
animation: slideInDown 0.6s ease-out 0.2s both;
}
.suggestion-item {
animation: fadeInUp 0.3s ease-out;
}
.suggestion-item:nth-child(1) { animation-delay: 0.1s; }
.suggestion-item:nth-child(2) { animation-delay: 0.2s; }
.suggestion-item:nth-child(3) { animation-delay: 0.3s; }
/* Responsive */
@media (max-width: 768px) {
.search-box-container {
padding: 30px 15px;
}
.search-box-demo {
padding: 25px;
}
.search-header h2 {
font-size: 1.7rem;
}
.search-input {
font-size: 1rem;
padding: 12px 0;
}
.search-btn {
padding: 12px 20px;
font-size: 0.9rem;
}
.search-icon {
font-size: 1.1rem;
padding: 0 12px;
}
.suggestions-dropdown {
max-height: 250px;
}
.suggestion-text {
font-size: 0.9rem;
}
}
@media (max-width: 480px) {
.search-box {
flex-direction: column;
gap: 10px;
padding: 15px;
}
.search-input {
width: 100%;
}
.search-controls {
display: flex;
width: 100%;
gap: 10px;
}
.clear-btn {
margin: 0;
}
.search-btn {
flex: 1;
margin: 0;
}
.suggestions-dropdown {
position: fixed;
top: auto;
bottom: 0;
left: 0;
right: 0;
height: 70vh;
border-radius: 20px 20px 0 0;
transform: translateY(100%);
}
.search-box.focused + .suggestions-dropdown {
transform: translateY(0);
}
}
document.addEventListener('DOMContentLoaded', function() {
// Get DOM elements
const searchWrapper = document.getElementById('searchWrapper');
const searchBox = document.getElementById('searchBox');
const searchInput = document.getElementById('searchInput');
const clearBtn = document.getElementById('clearBtn');
const searchBtn = document.getElementById('searchBtn');
const suggestionsDropdown = document.getElementById('suggestionsDropdown');
const suggestionItems = document.querySelectorAll('.suggestion-item');
const searchHeader = document.querySelector('.search-header');
let activeIndex = -1;
let recentSearches = [];
// Initialize search box
function initSearchBox() {
loadRecentSearches();
setupEventListeners();
}
// Setup event listeners
function setupEventListeners() {
// Focus events
searchInput.addEventListener('focus', function() {
searchBox.classList.add('focused');
loadRecentSearches();
});
searchInput.addEventListener('blur', function() {
// Delay hiding to allow clicking on suggestions
setTimeout(() => {
searchBox.classList.remove('focused');
activeIndex = -1;
clearActiveSuggestion();
}, 200);
});
// Input events
searchInput.addEventListener('input', function() {
const value = this.value.trim();
if (value) {
// Show suggestions based on input
filterSuggestions(value);
} else {
// Show default suggestions
showDefaultSuggestions();
}
});
// Key events
searchInput.addEventListener('keydown', function(e) {
handleKeyDown(e);
});
// Clear button
clearBtn.addEventListener('click', function() {
searchInput.value = '';
searchInput.focus();
showDefaultSuggestions();
});
// Search button
searchBtn.addEventListener('click', function() {
performSearch(searchInput.value.trim());
});
// Suggestion items
suggestionItems.forEach((item, index) => {
item.addEventListener('click', function() {
const value = this.getAttribute('data-value');
searchInput.value = value;
performSearch(value);
});
item.addEventListener('mouseenter', function() {
setActiveSuggestion(index);
});
});
// Click outside to close
document.addEventListener('click', function(e) {
if (!searchWrapper.contains(e.target)) {
searchBox.classList.remove('focused');
activeIndex = -1;
clearActiveSuggestion();
}
});
}
// Handle key events
function handleKeyDown(e) {
const items = Array.from(suggestionItems);
switch(e.key) {
case 'ArrowDown':
e.preventDefault();
activeIndex = (activeIndex + 1) % items.length;
setActiveSuggestion(activeIndex);
break;
case 'ArrowUp':
e.preventDefault();
activeIndex = (activeIndex - 1 + items.length) % items.length;
setActiveSuggestion(activeIndex);
break;
case 'Enter':
e.preventDefault();
if (activeIndex >= 0 && activeIndex < items.length) {
const value = items[activeIndex].getAttribute('data-value');
searchInput.value = value;
performSearch(value);
} else {
performSearch(searchInput.value.trim());
}
break;
case 'Escape':
searchBox.classList.remove('focused');
activeIndex = -1;
clearActiveSuggestion();
break;
}
}
// Set active suggestion
function setActiveSuggestion(index) {
clearActiveSuggestion();
if (index >= 0 && index < suggestionItems.length) {
suggestionItems[index].classList.add('active');
activeIndex = index;
}
}
// Clear active suggestion
function clearActiveSuggestion() {
suggestionItems.forEach(item => {
item.classList.remove('active');
});
}
// Filter suggestions based on input
function filterSuggestions(query) {
// In a real implementation, this would make an API call
// For demo purposes, we'll filter the existing suggestions
const lowerQuery = query.toLowerCase();
suggestionItems.forEach(item => {
const text = item.getAttribute('data-value').toLowerCase();
if (text.includes(lowerQuery)) {
item.style.display = 'flex';
} else {
item.style.display = 'none';
}
});
}
// Show default suggestions
function showDefaultSuggestions() {
suggestionItems.forEach(item => {
item.style.display = 'flex';
});
}
// Perform search
function performSearch(query) {
if (query) {
// Add to recent searches
addToRecentSearches(query);
// Show loading state
searchBtn.classList.add('loading');
searchBtn.disabled = true;
// Simulate search process
setTimeout(() => {
// In a real implementation, this would redirect to search results
alert(`Searching for: "${query}"\nIn a real application, this would show search results.`);
// Reset button state
searchBtn.classList.remove('loading');
searchBtn.disabled = false;
// Close dropdown
searchBox.classList.remove('focused');
activeIndex = -1;
clearActiveSuggestion();
}, 1500);
}
}
// Load recent searches
function loadRecentSearches() {
// In a real implementation, this would load from localStorage or API
recentSearches = [
'bluetooth speaker',
'smart watch',
'wireless earbuds'
];
// Update recent searches in DOM
updateRecentSearches();
}
// Update recent searches display
function updateRecentSearches() {
const recentGroup = document.querySelector('.suggestion-group:last-child');
const groupTitle = recentGroup.querySelector('.group-title');
if (recentSearches.length > 0) {
groupTitle.textContent = 'Recent Searches';
// Update recent search items
// (Implementation would depend on your specific requirements)
} else {
groupTitle.textContent = 'No Recent Searches';
}
}
// Add to recent searches
function addToRecentSearches(query) {
// Remove if already exists
const index = recentSearches.indexOf(query);
if (index > -1) {
recentSearches.splice(index, 1);
}
// Add to beginning
recentSearches.unshift(query);
// Keep only last 5
if (recentSearches.length > 5) {
recentSearches = recentSearches.slice(0, 5);
}
// Save to localStorage
try {
localStorage.setItem('recentSearches', JSON.stringify(recentSearches));
} catch (e) {
console.warn('Could not save recent searches to localStorage');
}
}
// Initialize the search box
initSearchBox();
});