-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (96 loc) · 3.39 KB
/
script.js
File metadata and controls
111 lines (96 loc) · 3.39 KB
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
/* ============================================
Henghui Du - Academic Homepage JavaScript
============================================ */
// --- Navigation scroll effect ---
const navbar = document.getElementById('navbar');
const navLinks = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('section[id]');
const navToggle = document.getElementById('nav-toggle');
const navMenu = document.getElementById('nav-menu');
const backToTop = document.getElementById('backToTop');
// Scroll handler
function onScroll() {
const scrollY = window.scrollY;
// Navbar shadow
if (scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
// Active nav link
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop - 100;
const sectionHeight = section.offsetHeight;
if (scrollY >= sectionTop && scrollY < sectionTop + sectionHeight) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
// Back to top
if (scrollY > 400) {
backToTop.classList.add('visible');
} else {
backToTop.classList.remove('visible');
}
}
window.addEventListener('scroll', onScroll, { passive: true });
// --- Mobile nav toggle ---
navToggle.addEventListener('click', () => {
navMenu.classList.toggle('active');
const spans = navToggle.querySelectorAll('span');
if (navMenu.classList.contains('active')) {
spans[0].style.transform = 'rotate(45deg) translate(5px, 5px)';
spans[1].style.opacity = '0';
spans[2].style.transform = 'rotate(-45deg) translate(5px, -5px)';
} else {
spans[0].style.transform = '';
spans[1].style.opacity = '';
spans[2].style.transform = '';
}
});
// Close mobile nav on link click
navLinks.forEach(link => {
link.addEventListener('click', () => {
navMenu.classList.remove('active');
const spans = navToggle.querySelectorAll('span');
spans[0].style.transform = '';
spans[1].style.opacity = '';
spans[2].style.transform = '';
});
});
// --- Back to top ---
backToTop.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
// --- Intersection Observer for animations ---
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe all section titles and pub items
document.querySelectorAll('.section-title, .pub-item, .timeline-item, .news-item').forEach(el => {
el.style.opacity = '0';
el.style.transform = 'translateY(20px)';
observer.observe(el);
});
// --- Smooth entrance ---
document.addEventListener('DOMContentLoaded', () => {
document.body.style.opacity = '1';
});
// --- Console greeting ---
console.log('%c🎓 Henghui Du\'s Homepage', 'color: #2563eb; font-size: 16px; font-weight: bold;');
console.log('%cFeel free to explore the source code!', 'color: #64748b; font-size: 12px;');