give me javacript code
- User
give me javacript code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
give me javacript code
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll provide a comprehensive JavaScript solution for your portfolio website, focusing on interactivity, project filtering, form validation, and smooth animations.
// 1. Project Filtering System
class ProjectFilter {
constructor(projects) {
this.projects = projects;
this.filterContainer = document.querySelector('.project-filter');
this.projectGrid = document.querySelector('.project-grid');
this.initializeFilter();
}
initializeFilter() {
const categories = [...new Set(this.projects.map(p => p.category))];
categories.forEach(category => {
const button = document.createElement('button');
button.textContent = category;
button.addEventListener('click', () => this.filterProjects(category));
this.filterContainer.appendChild(button);
});
}
filterProjects(category) {
const filteredProjects = this.projects.filter(p => p.category === category);
this.renderProjects(filteredProjects);
}
renderProjects(projects) {
this.projectGrid.innerHTML = '';
projects.forEach(project => {
const projectCard = document.createElement('div');
projectCard.classList.add('project-card');
projectCard.innerHTML = `
<img src="${project.image}" alt="${project.title}">
<h3>${project.title}</h3>
<p>${project.description}</p>
<a href="${project.link}">View Project</a>
`;
this.projectGrid.appendChild(projectCard);
});
}
}
// 2. Form Validation & Submission
class ContactForm {
constructor(formElement) {
this.form = formElement;
this.nameInput = this.form.querySelector('#name');
this.emailInput = this.form.querySelector('#email');
this.messageInput = this.form.querySelector('#message');
this.initEventListeners();
}
initEventListeners() {
this.form.addEventListener('submit', this.handleSubmit.bind(this));
this.nameInput.addEventListener('input', this.validateName.bind(this));
this.emailInput.addEventListener('input', this.validateEmail.bind(this));
}
validateName() {
const nameRegex = /^[A-Za-z\s]{2,30}$/;
return nameRegex.test(this.nameInput.value);
}
validateEmail() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(this.emailInput.value);
}
handleSubmit(event) {
event.preventDefault();
if (this.validateName() && this.validateEmail()) {
const formData = {
name: this.nameInput.value,
email: this.emailInput.value,
message: this.messageInput.value
};
this.submitToServer(formData);
} else {
alert('Please check your input fields');
}
}
async submitToServer(formData) {
try {
const response = await fetch('/submit-form', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
});
if (response.ok) {
alert('Message sent successfully!');
this.form.reset();
}
} catch (error) {
console.error('Submission error:', error);
}
}
}
// 3. Scroll-Triggered Animations
class ScrollAnimations {
constructor() {
this.observeElements();
}
observeElements() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate');
}
});
}, { threshold: 0.1 });
document.querySelectorAll('.animate-on-scroll').forEach(element => {
observer.observe(element);
});
}
}
// 4. Dark Mode Toggle
class ThemeManager {
constructor() {
this.themeToggle = document.querySelector('#theme-toggle');
this.initThemeToggle();
}
initThemeToggle() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
document.body.classList.add(savedTheme);
}
this.themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
const currentTheme = document.body.classList.contains('dark-mode')
? 'dark-mode'
: '';
localStorage.setItem('theme', currentTheme);
});
}
}
// 5. Project Data (Sample)
const projectData = [
{
title: 'Mobile Banking App',
category: 'UX Design',
description: 'Redesigned mobile banking experience',
image: 'banking-app.jpg',
link: '#project1'
},
{
title: 'E-commerce Platform',
category: 'UI Design',
description: 'Improved user flow for online shopping',
image: 'ecommerce.jpg',
link: '#project2'
}
];
// 6. Initialization Function
function initPortfolio() {
// Initialize components
new ProjectFilter(projectData);
new ContactForm(document.querySelector('#contact-form'));
new ScrollAnimations();
new ThemeManager();
}
// Run initialization when DOM is fully loaded
document.addEventListener('DOMContentLoaded', initPortfolio);
// 7. Performance Tracking
window.addEventListener('load', () => {
const loadTime = performance.now();
console.log(`Page loaded in ${loadTime} milliseconds`);
});
/* Scroll Animation Styles */
.animate-on-scroll {
opacity: 0;
transform: translateY(20px);
transition: all 0.6s ease;
}
.animate-on-scroll.animate {
opacity: 1;
transform: translateY(0);
}
/* Dark Mode Styles */
body.dark-mode {
background-color: #121212;
color: #ffffff;
}
/* Responsive Design */
@media (max-width: 768px) {
.project-grid {
grid-template-columns: 1fr;
}
}
<!-- Add these attributes to elements you want to animate -->
<div class="project-card animate-on-scroll">...</div>
<!-- Theme Toggle Button -->
<button id="theme-toggle">🌓 Toggle Theme</button>
<!-- Contact Form -->
<form id="contact-form">
<input id="name" type="text" required>
<input id="email" type="email" required>
<textarea id="message" required></textarea>
<button type="submit">Send Message</button>
</form>
Would you like me to elaborate on any specific part of the code or discuss implementation strategies? 🚀