bytebazaar / script.js
Clings's picture
Now create the css and js
e284f77 verified
Raw History Blame Contribute Delete
6.52 kB
```javascript
// Initialize Vanta.js background animation
function initVantaBackground() {
VANTA.NET({
el: "#vanta-background",
mouseControls: true,
touchControls: true,
gyroControls: false,
minHeight: 200.00,
minWidth: 200.00,
scale: 1.00,
scaleMobile: 1.00,
color: 0x6366f1,
backgroundColor: 0xf8fafc,
points: 10.00,
maxDistance: 22.00,
spacing: 18.00
});
}
// Initialize Feather Icons
function initFeatherIcons() {
feather.replace();
}
// Cart functionality
let cart = [];
function updateCartCount() {
const count = cart.reduce((total, item) => total + item.quantity, 0);
document.querySelector('.cart-count').textContent = count;
count > 0
? document.querySelector('.cart-count').classList.remove('hidden')
: document.querySelector('.cart-count').classList.add('hidden');
}
function addToCart(productName, productPrice) {
const existingItem = cart.find(item => item.name === productName);
if(existingItem) {
existingItem.quantity++;
} else {
cart.push({
name: productName,
price: productPrice,
quantity: 1
});
}
updateCartCount();
showToast(`${productName} added to cart!`, 'success');
}
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 2000);
}, 100);
}
// Search functionality
function initSearch() {
const searchInput = document.querySelector('input[type="text"]');
if (searchInput) {
searchInput.addEventListener('keyup', function(e) {
if(e.key === 'Enter') {
const searchTerm = this.value.toLowerCase();
const products = document.querySelectorAll('.product-card');
products.forEach(product => {
const name = product.querySelector('h3').textContent.toLowerCase();
const category = product.querySelector('p.text-gray-500').textContent.toLowerCase();
if(name.includes(searchTerm) || category.includes(searchTerm)) {
product.style.display = 'block';
} else {
product.style.display = 'none';
}
});
}
});
}
}
// Mobile menu toggle
function initMobileMenu() {
const mobileMenuButton = document.querySelector('.mobile-menu-button');
const mobileMenu = document.querySelector('.mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', function() {
mobileMenu.classList.toggle('show-mobile-menu');
});
// Close mobile menu when clicking outside
document.addEventListener('click', function(e) {
if (!mobileMenu.contains(e.target) && e.target !== mobileMenuButton) {
mobileMenu.classList.remove('show-mobile-menu');
}
});
}
}
// Product hover animations
function initProductHover() {
document.querySelectorAll('.product-card').forEach(card => {
card.addEventListener('mouseenter', function() {
anime({
targets: this.querySelector('img'),
scale: 1.05,
duration: 300,
easing: 'easeOutQuad'
});
});
card.addEventListener('mouseleave', function() {
anime({
targets: this.querySelector('img'),
scale: 1,
duration: 300,
easing: 'easeOutQuad'
});
});
});
}
// Form handling
function initForms() {
// Login form
const loginForm = document.getElementById('loginForm');
if (loginForm) {
loginForm.addEventListener('submit', function(e) {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
// Here you would typically send this to your backend
console.log('Login attempt with:', { email, password });
// For demo purposes, we'll just redirect
window.location.href = 'index.html';
});
}
// Register form
const registerForm = document.getElementById('registerForm');
if (registerForm) {
registerForm.addEventListener('submit', function(e) {
e.preventDefault();
// Handle registration logic here
});
}
}
// Initialize all functionality when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
initVantaBackground();
initFeatherIcons();
initSearch();
initMobileMenu();
initProductHover();
initForms();
// Add cart icon to nav if it doesn't exist
if (!document.querySelector('.cart-icon')) {
const navCart = document.createElement('div');
navCart.className = 'cart-icon ml-4 relative cursor-pointer';
navCart.innerHTML = `
<i data-feather="shopping-bag" class="text-gray-500 hover:text-primary"></i>
<span class="cart-count absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full h-5 w-5 flex items-center justify-center hidden">0</span>
`;
document.querySelector('nav .hidden.sm\\:ml-6.sm\\:flex.sm\\:items-center').prepend(navCart);
feather.replace();
}
// Set up event listeners for all "Add to cart" buttons
document.querySelectorAll('.add-to-cart').forEach(button => {
button.addEventListener('click', function() {
const productCard = this.closest('.product-card');
const productName = productCard.querySelector('h3').textContent;
const productPrice = parseFloat(productCard.querySelector('.product-price').textContent.replace('$', ''));
addToCart(productName, productPrice);
});
});
});
// Reinitialize Feather Icons when new content is loaded (like AJAX)
document.addEventListener('feather-icons-update', function() {
feather.replace();
});
```