Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (133 loc) · 5.07 KB
/
Copy pathscript.js
File metadata and controls
165 lines (133 loc) · 5.07 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
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
/**
* BURKHA MAHAL — Dark Luxury Theme
* script.js
* Author : Abhishek Suvarna
*
* Features
* ────────
* 1. Navbar solidifies on scroll
* 2. Mobile burger menu (slide-in panel + overlay)
* 3. Scroll-reveal (.reveal → .in-view)
* 4. Active nav-link highlight on scroll
* 5. Back-to-top button
* 6. Smooth offset-scroll for anchor links (accounts for fixed navbar)
*/
(function () {
'use strict';
/* ── Refs ───────────────────────────────────────────────── */
const navbar = document.getElementById('navbar');
const burger = document.getElementById('burger');
const navLinks = document.getElementById('navLinks');
const allLinks = document.querySelectorAll('.nav-link');
const btt = document.getElementById('btt');
const sections = document.querySelectorAll('section[id]');
/* Elements eligible for scroll-reveal */
const revealTargets = document.querySelectorAll(
'.s-head, .about-prose, .about-stats, .svc-card, ' +
'.contact-info, .contact-map, .reveal'
);
/* ── 1. Navbar — solid background on scroll ─────────────── */
function syncNavbar() {
navbar.classList.toggle('solid', window.scrollY > 50);
}
/* ── 2. Mobile menu ──────────────────────────────────────── */
let menuOpen = false;
function openMenu() {
menuOpen = true;
navLinks.classList.add('open');
burger.classList.add('open');
burger.setAttribute('aria-expanded', 'true');
document.body.style.overflow = 'hidden';
}
function closeMenu() {
menuOpen = false;
navLinks.classList.remove('open');
burger.classList.remove('open');
burger.setAttribute('aria-expanded', 'false');
document.body.style.overflow = '';
}
if (burger) {
burger.addEventListener('click', function () {
menuOpen ? closeMenu() : openMenu();
});
}
/* Close on nav link click */
allLinks.forEach(function (link) {
link.addEventListener('click', closeMenu);
});
/* Close when clicking outside the menu panel */
document.addEventListener('click', function (e) {
if (menuOpen &&
!navLinks.contains(e.target) &&
!burger.contains(e.target)) {
closeMenu();
}
});
/* Close on resize to desktop width */
window.addEventListener('resize', function () {
if (window.innerWidth > 768 && menuOpen) closeMenu();
}, { passive: true });
/* ── 3. Scroll-reveal ────────────────────────────────────── */
function revealElements() {
const threshold = window.innerHeight * 0.88;
revealTargets.forEach(function (el) {
if (el.getBoundingClientRect().top < threshold) {
el.classList.add('in-view');
}
});
}
/* ── 4. Active nav link ──────────────────────────────────── */
function updateActiveLink() {
const offset = (navbar ? navbar.offsetHeight : 70) + 24;
let current = '';
sections.forEach(function (sec) {
if (window.scrollY >= sec.offsetTop - offset) {
current = sec.getAttribute('id');
}
});
allLinks.forEach(function (link) {
const href = link.getAttribute('href');
link.classList.toggle('active', href === '#' + current);
});
}
/* ── 5. Back to Top ──────────────────────────────────────── */
function syncBtt() {
if (btt) btt.classList.toggle('show', window.scrollY > 480);
}
if (btt) {
btt.addEventListener('click', function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
/* ── 6. Smooth offset scroll for anchor links ───────────── */
document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
anchor.addEventListener('click', function (e) {
const target = document.querySelector(this.getAttribute('href'));
if (!target) return;
e.preventDefault();
const navH = navbar ? navbar.offsetHeight : 70;
const top = target.getBoundingClientRect().top + window.scrollY - navH;
window.scrollTo({ top: top, behavior: 'smooth' });
});
});
/* ── Combined scroll handler (passive for performance) ──── */
function onScroll() {
syncNavbar();
syncBtt();
revealElements();
updateActiveLink();
}
window.addEventListener('scroll', onScroll, { passive: true });
/* ── Init ───────────────────────────────────────────────── */
function init() {
syncNavbar();
syncBtt();
revealElements(); /* catch elements already in viewport on load */
updateActiveLink();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
}());