Spaces:
Running
Running
File size: 7,040 Bytes
7ebc062 1fca5b4 7ebc062 1fca5b4 7ebc062 | 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
// Main JavaScript File
document.addEventListener('DOMContentLoaded', function() {
// Initialize animations
const animateElements = document.querySelectorAll('.fade-in, .slide-up');
animateElements.forEach((el, index) => {
el.style.animationDelay = `${index * 0.1}s`;
});
// Language switcher functionality
const langSwitcher = document.getElementById('lang-switcher');
if (langSwitcher) {
langSwitcher.addEventListener('change', function() {
const selectedLang = this.value;
// In a real app, this would redirect to the selected language version
console.log(`Language changed to: ${selectedLang}`);
});
}
// Theme switcher functionality
const themeSwitcher = document.getElementById('theme-switcher');
if (themeSwitcher) {
themeSwitcher.addEventListener('change', function() {
const selectedTheme = this.value;
document.body.classList.toggle('dark', selectedTheme === 'dark');
localStorage.setItem('theme', selectedTheme);
});
// Check for saved theme preference
const savedTheme = localStorage.getItem('theme') || 'light';
themeSwitcher.value = savedTheme;
if (savedTheme === 'dark') {
document.body.classList.add('dark');
}
}
// Interactive code runner
const runCodeBtn = document.getElementById('run-code');
if (runCodeBtn) {
runCodeBtn.addEventListener('click', function() {
const output = document.getElementById('code-output');
if (output) {
output.innerHTML = `
<div class="text-green-600 font-bold">$ python hello.py</div>
<div class="mt-3 text-2xl font-bold text-primary">سلام دنیا!</div>
<div class="mt-3 text-gray-600">✅ کد با موفقیت اجرا شد!</div>
<div class="mt-4 text-sm text-gray-500">
زمان اجرا: ۰.۰۰۲ ثانیه<br>
حافظه مصرفی: ۲.۱ مگابایت
</div>
`;
// Add animation
output.classList.add('animate-pulse');
setTimeout(() => output.classList.remove('animate-pulse'), 500);
// Button feedback
this.innerHTML = '<span>✅ اجرا شد!</span>';
this.classList.add('bg-green-600', 'hover:bg-green-700');
setTimeout(() => {
this.innerHTML = '<span>اجرای کد</span>';
this.classList.remove('bg-green-600', 'hover:bg-green-700');
}, 2000);
}
});
}
// Add hover effects to cards
const cards = document.querySelectorAll('.bg-white\\/80');
cards.forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-10px) scale(1.02)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0) scale(1)';
});
});
// Add parallax effect to floating elements
window.addEventListener('mousemove', function(e) {
const x = e.clientX / window.innerWidth;
const y = e.clientY / window.innerHeight;
document.querySelectorAll('.floating-element').forEach(el => {
const speed = parseFloat(el.getAttribute('data-speed') || '0.5');
const xMove = x * speed * 100;
const yMove = y * speed * 100;
el.style.transform = `translate(${xMove}px, ${yMove}px)`;
});
});
// Initialize typing animation
const typeWriter = document.getElementById('typewriter');
if (typeWriter) {
const texts = [
'یادگیری پایتون',
'ساخت پروژههای واقعی',
'تبدیل شدن به حرفهای'
];
let textIndex = 0;
let charIndex = 0;
let isDeleting = false;
function type() {
const currentText = texts[textIndex];
if (isDeleting) {
typeWriter.textContent = currentText.substring(0, charIndex - 1);
charIndex--;
} else {
typeWriter.textContent = currentText.substring(0, charIndex + 1);
charIndex++;
}
if (!isDeleting && charIndex === currentText.length) {
isDeleting = true;
setTimeout(type, 2000);
} else if (isDeleting && charIndex === 0) {
isDeleting = false;
textIndex = (textIndex + 1) % texts.length;
setTimeout(type, 500);
} else {
setTimeout(type, isDeleting ? 50 : 100);
}
}
setTimeout(type, 1000);
}
});
// API functions
async function fetchCourses() {
try {
const response = await fetch('/api/courses');
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching courses:', error);
return [];
}
}
// Form validation
function validateForm(form) {
let isValid = true;
const inputs = form.querySelectorAll('input[required], textarea[required]');
inputs.forEach(input => {
if (!input.value.trim()) {
isValid = false;
input.classList.add('border-red-500');
} else {
input.classList.remove('border-red-500');
}
});
return isValid;
}
// Create confetti effect
function createConfetti() {
const confettiCount = 100;
const colors = ['#3B82F6', '#10B981', '#8B5CF6', '#EF4444', '#F59E0B'];
for (let i = 0; i < confettiCount; i++) {
const confetti = document.createElement('div');
confetti.style.position = 'fixed';
confetti.style.width = '10px';
confetti.style.height = '10px';
confetti.style.background = colors[Math.floor(Math.random() * colors.length)];
confetti.style.borderRadius = '50%';
confetti.style.left = `${Math.random() * 100}vw`;
confetti.style.top = '-20px';
confetti.style.zIndex = '9999';
confetti.style.pointerEvents = 'none';
document.body.appendChild(confetti);
// Animation
const animation = confetti.animate([
{ transform: 'translateY(0) rotate(0deg)', opacity: 1 },
{ transform: `translateY(${window.innerHeight + 100}px) rotate(${Math.random() * 360}deg)`, opacity: 0 }
], {
duration: Math.random() * 3000 + 2000,
easing: 'cubic-bezier(0.215, 0.610, 0.355, 1)'
});
animation.onfinish = () => confetti.remove();
}
}
// Export for global use
window.createConfetti = createConfetti;
|