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.
Okay, let's add some JavaScript to your UI/UX portfolio website!
JavaScript will bring interactivity and a better user experience. I'll provide code for common features:
How to use this code:
script.js in the same directory as your index.html and styles.css.index.html just before the closing </body> tag:
<!-- At the very end of your body tag in index.html -->
<script src="script.js"></script>
</body>
</html>
script.js - The JavaScript Codedocument.addEventListener('DOMContentLoaded', () => {
// ---------------------------------------------------
// 1. Hamburger Menu Toggle (for mobile navigation)
// ---------------------------------------------------
const menuToggle = document.getElementById('menu-toggle');
const navMenu = document.getElementById('nav-menu');
const body = document.body; // To disable body scroll when menu is open
if (menuToggle && navMenu) {
menuToggle.addEventListener('click', () => {
navMenu.classList.toggle('active'); // Toggles visibility
menuToggle.classList.toggle('active'); // Toggles icon state
body.classList.toggle('no-scroll'); // Disables body scroll
});
// Close menu when a navigation link is clicked (for single-page navigation)
navMenu.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
navMenu.classList.remove('active');
menuToggle.classList.remove('active');
body.classList.remove('no-scroll');
});
});
}
// ---------------------------------------------------
// 2. Smooth Scrolling for Internal Links
// ---------------------------------------------------
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault(); // Prevent default jump
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth' // Smooth scroll effect
});
}
});
});
// ---------------------------------------------------
// 3. Dark Mode Toggle
// ---------------------------------------------------
const darkModeToggle = document.getElementById('dark-mode-toggle');
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
// Function to set the theme
const setTheme = (isDark) => {
body.classList.toggle('dark-mode', isDark);
localStorage.setItem('theme', isDark ? 'dark' : 'light');
};
// Check for saved theme preference or system preference on load
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
setTheme(savedTheme === 'dark');
} else {
setTheme(prefersDarkMode.matches); // Set based on system preference
}
// Event listener for the toggle button
if (darkModeToggle) {
darkModeToggle.checked = body.classList.contains('dark-mode'); // Sync toggle state
darkModeToggle.addEventListener('change', () => {
setTheme(darkModeToggle.checked);
});
}
// Listen for system theme changes
prefersDarkMode.addEventListener('change', (event) => {
if (!localStorage.getItem('theme')) { // Only update if no explicit preference is set
setTheme(event.matches);
if (darkModeToggle) {
darkModeToggle.checked = event.matches; // Sync toggle state
}
}
});
// ---------------------------------------------------
// 4. Basic Form Validation (for contact form)
// ---------------------------------------------------
const contactForm = document.getElementById('contact-form');
if (contactForm) {
contactForm.addEventListener('submit', function (event) {
event.preventDefault(); // Stop the form from submitting normally
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const messageInput = document.getElementById('message');
let isValid = true;
// Simple validation checks
if (nameInput.value.trim() === '') {
alert('Please enter your name.');
isValid = false;
} else if (emailInput.value.trim() === '' || !emailInput.value.includes('@')) {
alert('Please enter a valid email address.');
isValid = false;
} else if (messageInput.value.trim() === '') {
alert('Please enter your message.');
isValid = false;
}
if (isValid) {
// If validation passes, you'd typically send data to a backend
// For demonstration, we'll just log and clear the form.
console.log('Form submitted successfully!');
console.log('Name:', nameInput.value);
console.log('Email:', emailInput.value);
console.log('Message:', messageInput.value);
alert('Thank you for your message! I will get back to you soon.');
contactForm.reset(); // Clear the form fields
}
});
}
// ---------------------------------------------------
// 5. Scroll-based Animations (Fade-in on elements)
// ---------------------------------------------------
const animateOnScrollElements = document.querySelectorAll('.animate-on-scroll');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible'); // Add a class to trigger CSS animation
observer.unobserve(entry.target); // Stop observing once animated
}
});
}, {
threshold: 0.2 // Trigger when 20% of the element is visible
});
animateOnScrollElements.forEach(element => {
observer.observe(element);
});
});
You'll need to make sure your HTML has the correct IDs and classes for the JavaScript to work. Also, add the necessary CSS to define the visual changes.
<header>
<div class="container">
<a href="#" class="logo">Your Name</a>
<button id="menu-toggle" class="menu-toggle" aria-label="Toggle navigation">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</button>
<nav id="nav-menu">
<ul>
<li><a href="#about">About</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#contact">Contact</a></li>
<!-- Dark Mode Toggle within nav (optional placement) -->
<li>
<div class="dark-mode-switch">
<label for="dark-mode-toggle">Dark Mode</label>
<input type="checkbox" id="dark-mode-toggle">
<span class="slider round"></span>
</div>
</li>
</ul>
</nav>
</div>
</header>
/* Header Basic Styles */
header {
background-color: #f8f8f8;
padding: 1rem 0;
position: sticky;
top: 0;
z-index: 1000;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
header .container {
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 1.8rem;
font-weight: bold;
color: #333;
text-decoration: none;
}
nav ul {
list-style: none;
display: flex;
gap: 1.5rem;
}
nav a {
text-decoration: none;
color: #555;
font-weight: 500;
transition: color 0.3s ease;
}
nav a:hover {
color: #007bff;
}
/* Hamburger Menu Toggle Button */
.menu-toggle {
display: none; /* Hidden by default on desktop */
flex-direction: column;
justify-content: space-around;
width: 30px;
height: 25px;
background: transparent;
border: none;
cursor: pointer;
padding: 0;
z-index: 1001; /* Above nav menu */
}
.menu-toggle .bar {
width: 100%;
height: 3px;
background-color: #333;
border-radius: 5px;
transition: all 0.3s ease;
}
/* Hamburger menu active state */
.menu-toggle.active .bar:nth-child(1) {
transform: translateY(11px) rotate(45deg);
}
.menu-toggle.active .bar:nth-child(2) {
opacity: 0;
}
.menu-toggle.active .bar:nth-child(3) {
transform: translateY(-11px) rotate(-45deg);
}
/* Mobile Navigation */
@media (max-width: 768px) {
.menu-toggle {
display: flex; /* Show on mobile */
}
nav ul {
flex-direction: column;
position: fixed;
top: 0;
right: -100%; /* Off-screen by default */
width: 70%;
height: 100vh;
background-color: #fefefe; /* Light background for mobile menu */
padding: 5rem 2rem 2rem;
box-shadow: -5px 0 15px rgba(0,0,0,0.1);
transition: right 0.3s ease-in-out;
z-index: 999;
gap: 1rem;
align-items: flex-start; /* Align menu items to left */
}
nav ul.active {
right: 0; /* Slide in */
}
nav ul li {
width: 100%;
}
nav ul a {
display: block;
padding: 0.8rem 0;
font-size: 1.2rem;
border-bottom: 1px solid #eee;
}
nav ul a:last-child {
border-bottom: none;
}
body.no-scroll {
overflow: hidden; /* Prevent background scroll when menu is open */
}
}
(Already included in the nav HTML snippet above.)
<div class="dark-mode-switch">
<label for="dark-mode-toggle">Dark Mode</label>
<input type="checkbox" id="dark-mode-toggle">
<span class="slider round"></span>
</div>
/* Basic Dark Mode Toggle Styles (Optional, can be simpler) */
.dark-mode-switch {
display: flex;
align-items: center;
gap: 0.5rem;
position: relative;
padding-left: 3rem; /* Space for the custom switch */
cursor: pointer;
font-size: 0.9rem;
color: #555;
}
.dark-mode-switch input[type="checkbox"] {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
width: 2.5rem; /* Adjust width as needed */
height: 1.5rem; /* Adjust height as needed */
}
.slider:before {
position: absolute;
content: "";
height: 1.1rem; /* Adjust size */
width: 1.1rem; /* Adjust size */
left: 0.2rem;
bottom: 0.2rem;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #007bff; /* Accent color when dark mode is on */
}
input:checked + .slider:before {
-webkit-transform: translateX(1rem); /* Move slider to the right */
-ms-transform: translateX(1rem);
transform: translateX(1rem);
}
/* Dark Mode specific styles */
body.dark-mode {
background-color: #1a1a1a;
color: #e0e0e0;
}
body.dark-mode header {
background-color: #2a2a2a;
box-shadow: 0 2px 5px rgba(0,0,0,0.5);
}
body.dark-mode .logo,
body.dark-mode nav a {
color: #e0e0e0;
}
body.dark-mode nav a:hover {
color: #90caf9; /* Lighter accent for hover */
}
body.dark-mode .menu-toggle .bar {
background-color: #e0e0e0;
}
/* Apply dark mode to other sections */
body.dark-mode .hero {
background-color: #0056b3; /* Darker blue for hero */
}
body.dark-mode .project-card {
background-color: #333;
box-shadow: 0 2px 10px rgba(0,0,0,0.5);
}
body.dark-mode .project-card h3 {
color: #90caf9;
}
body.dark-mode .project-card p {
color: #ccc;
}
body.dark-mode .btn {
background-color: #007bff;
color: white;
}
body.dark-mode footer {
background-color: #2a2a2a;
color: #aaa;
}
/* Adjust mobile menu for dark mode */
@media (max-width: 768px) {
body.dark-mode nav ul {
background-color: #2a2a2a;
box-shadow: -5px 0 15px rgba(0,0,0,0.5);
}
body.dark-mode nav ul a {
color: #e0e0e0;
border-bottom-color: #444;
}
}
<section id="contact" class="contact">
<div class="container">
<h2>Contact Me</h2>
<form id="contact-form">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required />
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required />
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" required></textarea>
</div>
<button type="submit" class="btn">Send Message</button>
</form>
</div>
</section>
Add the class animate-on-scroll to any section or element you want to animate when it enters the viewport.
<!-- Example: Hero Section -->
<section class="hero animate-on-scroll">
<div class="container">
<h2>Hi, I'm a UI/UX Designer</h2>
<p>Specializing in creating intuitive digital experiences.</p>
<a href="#projects" class="btn">View My Work</a>
</div>
</section>
<!-- Example: About Section -->
<section id="about" class="about animate-on-scroll">
<div class="container">
<h2>About Me</h2>
<p>I'm a passionate designer with 3+ years of experience in...</p>
<div class="skills">
<p>Skills: Figma, User Research, Prototyping</p>
</div>
</div>
</section>
<!-- Example: Project Card (each one can animate) -->
<div class="project-card animate-on-scroll">
<h3>Project Title</h3>
<p>Short description of the project.</p>
<a href="#">View Case Study</a>
</div>
/* Base state for animated elements (hidden/offset) */
.animate-on-scroll {
opacity: 0;
transform: translateY(20px); /* Start slightly below its final position */
transition: opacity 0.8s ease-out, transform 0.8s ease-out;
}
/* Active state (visible and in place) */
.animate-on-scroll.visible {
opacity: 1;
transform: translateY(0);
}
/* Optional: Add a delay for elements in a sequence (e.g., project cards) */
/* You would need to add classes like .delay-1, .delay-2 to your HTML */
.animate-on-scroll.delay-1 { transition-delay: 0.1s; }
.animate-on-scroll.delay-2 { transition-delay: 0.2s; }
.animate-on-scroll.delay-3 { transition-delay: 0.3s; }
/* ...and so on for your project cards or lists */
This JavaScript code will significantly enhance the user experience of your UI/UX portfolio. Remember to test it thoroughly in different browsers and screen sizes! Good luck!