-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (164 loc) · 5.21 KB
/
Copy pathmain.js
File metadata and controls
195 lines (164 loc) · 5.21 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// ==========================================================================
// Main Application - Tyodev Music Journey
// Single Page Application with Smooth Scrolling Navigation
// ==========================================================================
// Import components
import Navbar from "./app/components/navbar.js";
import Footer from "./app/components/footer.js";
import homeSection from "./app/homeSection.js";
import reasonSection from "./app/reasonSection.js";
import urMusicSection from "./app/urMusicSection.js";
import playlistSection from "./app/playlistSection.js";
import favouriteSection from "./app/favouriteSection.js";
import galerySection from "./app/galerySection.js";
class App {
constructor() {
this.currentSection = "home";
this.init();
}
init() {
this.renderLayout();
this.loadAllSections();
this.setupSmoothScrolling();
this.setupScrollSpy();
// Initialize AOS
if (typeof AOS !== "undefined") {
AOS.init({
duration: 1000,
once: false,
mirror: true,
});
}
}
renderLayout() {
const body = document.body;
body.innerHTML = "";
// Add navbar
body.appendChild(Navbar());
// Add main container
const main = document.createElement("main");
main.id = "main-content";
main.style.minHeight = "100vh";
body.appendChild(main);
// Add footer
body.appendChild(Footer());
}
loadAllSections() {
const mainContent = document.getElementById("main-content");
if (!mainContent) {
console.error("Main content element not found");
return;
}
// Load all sections with proper IDs and spacing
const sections = [
{ id: "home", component: homeSection },
{ id: "reason", component: reasonSection },
{ id: "playlist", component: playlistSection },
{ id: "favourite", component: favouriteSection },
{ id: "gallery", component: galerySection },
{ id: "music", component: urMusicSection },
];
sections.forEach(({ id, component }, index) => {
try {
const section = component();
section.id = id;
// Add spacing between sections (except first one)
if (index > 0) {
section.style.paddingTop = "100px";
}
mainContent.appendChild(section);
console.log(`✅ Loaded section: ${id}`);
} catch (error) {
console.error(`❌ Error loading section ${id}:`, error);
}
});
}
setupSmoothScrolling() {
// Handle navigation clicks for smooth scrolling
document.addEventListener("click", (e) => {
if (
e.target.classList.contains("nav-link") ||
e.target.closest("a[data-section]") ||
e.target.hasAttribute("data-section")
) {
e.preventDefault();
const target = e.target.closest("[data-section]") || e.target;
const sectionId = target.dataset.section;
if (sectionId) {
this.scrollToSection(sectionId);
}
}
});
// Handle hash changes for direct URL access
window.addEventListener("hashchange", () => {
const hash = window.location.hash.substring(1);
if (hash) {
this.scrollToSection(hash);
}
});
// Handle initial hash on page load
const initialHash = window.location.hash.substring(1);
if (initialHash) {
setTimeout(() => this.scrollToSection(initialHash), 100);
}
}
scrollToSection(sectionId) {
const targetSection = document.getElementById(sectionId);
if (targetSection) {
const navbarHeight = 80; // Height of fixed navbar
const targetPosition = targetSection.offsetTop - navbarHeight;
window.scrollTo({
top: targetPosition,
behavior: "smooth",
});
// Update URL hash
history.replaceState(null, null, `#${sectionId}`);
// Update active nav item
this.updateActiveNav(sectionId);
}
}
setupScrollSpy() {
let isScrolling = false;
window.addEventListener("scroll", () => {
if (!isScrolling) {
requestAnimationFrame(() => {
this.handleScroll();
isScrolling = false;
});
isScrolling = true;
}
});
}
handleScroll() {
const sections = ["home", "reason", "playlist", "favourite", "gallery", "music"];
const scrollPos = window.scrollY + 150; // Offset for navbar
let currentSection = "home";
sections.forEach((sectionId) => {
const element = document.getElementById(sectionId);
if (element && element.offsetTop <= scrollPos) {
currentSection = sectionId;
}
});
this.updateActiveNav(currentSection);
// Update URL hash without triggering scroll
if (window.location.hash !== `#${currentSection}`) {
history.replaceState(null, null, `#${currentSection}`);
}
}
updateActiveNav(activeSection) {
document.querySelectorAll(".nav-link").forEach((link) => {
link.classList.remove("active");
const linkSection = link.dataset.section;
if (
linkSection === activeSection ||
(activeSection === "home" && linkSection === "home")
) {
link.classList.add("active");
}
});
}
}
// Initialize app when DOM is loaded
document.addEventListener("DOMContentLoaded", () => {
new App();
});