|
| 1 | +// site.js - Custom JavaScript for Culhane Lab Website |
| 2 | + |
| 3 | +document.addEventListener('DOMContentLoaded', function() { |
| 4 | + // Enable smooth scrolling for the entire page |
| 5 | + document.documentElement.style.scrollBehavior = 'smooth'; |
| 6 | + |
| 7 | + // Enhanced smooth scrolling for anchor links |
| 8 | + document.querySelectorAll('a[href^="#"]').forEach(anchor => { |
| 9 | + anchor.addEventListener('click', function(e) { |
| 10 | + e.preventDefault(); |
| 11 | + |
| 12 | + const targetId = this.getAttribute('href'); |
| 13 | + if (targetId === '#') return; |
| 14 | + |
| 15 | + const targetElement = document.querySelector(targetId); |
| 16 | + if (!targetElement) return; |
| 17 | + |
| 18 | + // Smooth scroll with animation |
| 19 | + const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset; |
| 20 | + const startPosition = window.pageYOffset; |
| 21 | + const distance = targetPosition - startPosition; |
| 22 | + const duration = 1000; // ms |
| 23 | + let start = null; |
| 24 | + |
| 25 | + function animation(currentTime) { |
| 26 | + if (start === null) start = currentTime; |
| 27 | + const timeElapsed = currentTime - start; |
| 28 | + const progress = Math.min(timeElapsed / duration, 1); |
| 29 | + const ease = easeInOutCubic(progress); |
| 30 | + |
| 31 | + window.scrollTo(0, startPosition + distance * ease); |
| 32 | + |
| 33 | + if (timeElapsed < duration) { |
| 34 | + requestAnimationFrame(animation); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Easing function for smooth acceleration/deceleration |
| 39 | + function easeInOutCubic(t) { |
| 40 | + return t < 0.5 |
| 41 | + ? 4 * t * t * t |
| 42 | + : 1 - Math.pow(-2 * t + 2, 3) / 2; |
| 43 | + } |
| 44 | + |
| 45 | + requestAnimationFrame(animation); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + // Add fade-in animation to elements |
| 50 | + const fadeElements = document.querySelectorAll('.fade-in'); |
| 51 | + |
| 52 | + const observer = new IntersectionObserver(entries => { |
| 53 | + entries.forEach(entry => { |
| 54 | + if (entry.isIntersecting) { |
| 55 | + entry.target.classList.add('visible'); |
| 56 | + observer.unobserve(entry.target); |
| 57 | + } |
| 58 | + }); |
| 59 | + }, { |
| 60 | + threshold: 0.1, |
| 61 | + rootMargin: '0px 0px -100px 0px' // Triggers slightly before element comes into view |
| 62 | + }); |
| 63 | + |
| 64 | + fadeElements.forEach(element => { |
| 65 | + observer.observe(element); |
| 66 | + }); |
| 67 | + |
| 68 | + // Also observe slide-in animations |
| 69 | + const slideLeftElements = document.querySelectorAll('.slide-in-left'); |
| 70 | + const slideRightElements = document.querySelectorAll('.slide-in-right'); |
| 71 | + |
| 72 | + slideLeftElements.forEach(element => { |
| 73 | + observer.observe(element); |
| 74 | + }); |
| 75 | + |
| 76 | + slideRightElements.forEach(element => { |
| 77 | + observer.observe(element); |
| 78 | + }); |
| 79 | + |
| 80 | + // Parallax scrolling effect for hero section |
| 81 | + const heroSection = document.querySelector('.hero-section'); |
| 82 | + if (heroSection) { |
| 83 | + window.addEventListener('scroll', () => { |
| 84 | + const scrollPosition = window.pageYOffset; |
| 85 | + heroSection.style.backgroundPositionY = `${scrollPosition * 0.5}px`; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + // Add sticky navigation behavior |
| 90 | + window.addEventListener('scroll', function() { |
| 91 | + const navbar = document.querySelector('.navbar'); |
| 92 | + if (navbar) { |
| 93 | + if (window.scrollY > 50) { |
| 94 | + navbar.classList.add('scrolled'); |
| 95 | + } else { |
| 96 | + navbar.classList.remove('scrolled'); |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | +}); |
0 commit comments