File size: 5,542 Bytes
fc287fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | // Main JavaScript for CodeBloom Rose Petals
document.addEventListener('DOMContentLoaded', function() {
// Initialize feather icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
window.scrollTo({
top: targetElement.offsetTop - 80,
behavior: 'smooth'
});
}
});
});
// Project card interactions
const projectCards = document.querySelectorAll('.project-card');
projectCards.forEach(card => {
card.addEventListener('mouseenter', () => {
card.classList.add('hover-card');
});
card.addEventListener('mouseleave', () => {
card.classList.remove('hover-card');
});
});
// Search functionality (would connect to API)
const searchInput = document.querySelector('#navSearch');
if (searchInput) {
searchInput.addEventListener('input', debounce(function() {
const query = this.value.trim();
if (query.length > 2) {
performSearch(query);
}
}, 300));
}
// Theme toggle (light/dark - placeholder for future implementation)
const themeToggle = document.querySelector('#themeToggle');
if (themeToggle) {
themeToggle.addEventListener('click', function() {
document.documentElement.classList.toggle('dark');
// Save preference to localStorage
const isDark = document.documentElement.classList.contains('dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
// Show notification
showNotification('Theme switched to ' + (isDark ? 'dark' : 'light') + ' mode');
});
}
// Initialize dark mode from localStorage
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.documentElement.classList.add('dark');
}
});
// Utility function for debouncing
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func.apply(this, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Search function (placeholder - would connect to your backend/API)
async function performSearch(query) {
console.log('Searching for:', query);
// Here you would typically fetch from an API
// Example:
// const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`);
// const results = await response.json();
// displayResults(results);
}
// Notification system
function showNotification(message, type = 'info') {
// Create notification element
const notification = document.createElement('div');
notification.className = `fixed top-24 right-6 px-6 py-3 rounded-xl shadow-xl z-50 transform translate-x-full transition-transform duration-300 ${type === 'info' ? 'bg-rose-600' : 'bg-red-600'} text-white`;
notification.textContent = message;
// Append to body
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 10);
// Remove after 3 seconds
setTimeout(() => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}
// API integration for project data
async function fetchProjects() {
try {
// Example using public API - would replace with your actual API
const response = await fetch('https://api.github.com/search/repositories?q=topic:frontend&sort=stars&order=desc&per_page=6');
const data = await response.json();
return data.items || [];
} catch (error) {
console.error('Error fetching projects:', error);
return [];
}
}
// Form submission handler
document.addEventListener('submit', function(e) {
if (e.target.matches('form')) {
e.preventDefault();
const formData = new FormData(e.target);
const data = Object.fromEntries(formData);
// Show loading state
const submitBtn = e.target.querySelector('button[type="submit"]');
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Processing...';
submitBtn.disabled = true;
// Simulate API call
setTimeout(() => {
// Reset button
submitBtn.textContent = originalText;
submitBtn.disabled = false;
// Show success notification
showNotification('Action completed successfully!', 'info');
}, 1000);
}
});
// Window resize handling for responsive adjustments
let resizeTimer;
window.addEventListener('resize', function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
// Handle responsive adjustments here
console.log('Window resized - handling responsive adjustments');
}, 250);
}); |