Skip to content

Commit 1bd266a

Browse files
added to backtotop button in all pages
1 parent 7c87c40 commit 1bd266a

6 files changed

Lines changed: 71 additions & 19 deletions

File tree

bihar-culture-landing/culture.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ <h2>Want to Contribute to Bihar’s Cultural Archive?</h2>
103103
<p>Celebrating the spirit, art, and heritage of Bihar.</p>
104104
</div>
105105
</footer>
106+
</main>
106107

108+
<script src="script.js"></script>
107109
</body>
108110
</html>

bihar-culture-landing/festivals.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ <h3>Shravani Mela</h3>
7979
Bihar's rich heritage reflected in it's festivals</p>
8080
<br>
8181
</footer>
82+
<script src="script.js"></script>
8283
</body>
8384
</html>

bihar-culture-landing/history.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ <h2>Freedom Fighters</h2>
3939
<footer>
4040
<p>Help us add more historical events and personalities!</p>
4141
</footer>
42+
<script src="script.js"></script>
4243
</body>
4344
</html>

bihar-culture-landing/index.html

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,6 @@ <h2>Contribute to Bihar Culture</h2>
125125
</div>
126126
</footer>
127127

128-
<!-- Back to Top Button -->
129-
<button id="backToTopBtn" title="Back to Top">↑ Back to Top</button>
130-
131128
<script src="script.js"></script>
132-
<script>
133-
// Show/hide button on scroll
134-
const backToTopBtn = document.getElementById('backToTopBtn');
135-
window.onscroll = function() {
136-
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
137-
backToTopBtn.style.display = "block";
138-
} else {
139-
backToTopBtn.style.display = "none";
140-
}
141-
};
142-
143-
// Smooth scroll to top
144-
backToTopBtn.onclick = function() {
145-
window.scrollTo({ top: 0, behavior: 'smooth' });
146-
};
147-
</script>
148129
</body>
149130
</html>

bihar-culture-landing/modern.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ <h2>Bihar in Cinema</h2>
3939
<footer>
4040
<p>Modern Bihar section welcomes more contributions!</p>
4141
</footer>
42+
<script src="script.js"></script>
4243
</body>
4344
</html>

bihar-culture-landing/script.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ document.addEventListener('DOMContentLoaded', function() {
99
initAccessibility();
1010
initProgressiveEnhancement();
1111
initQuiz(); // attach quiz logic if present
12+
initBackToTop(); // initialize back-to-top button across pages
1213
});
1314

1415
// Navigation enhancements
@@ -447,4 +448,69 @@ document.head.appendChild(style);
447448
// Export for module usage (if needed)
448449
if (typeof module !== 'undefined' && module.exports) {
449450
module.exports = BiharCulture;
451+
}
452+
453+
// Back to Top button: create a single reusable button and behavior across pages
454+
function initBackToTop() {
455+
try {
456+
if (document.getElementById('backToTopBtn')) return; // already present
457+
458+
const btn = document.createElement('button');
459+
btn.id = 'backToTopBtn';
460+
btn.type = 'button';
461+
btn.title = 'Back to Top';
462+
btn.setAttribute('aria-label', 'Back to top');
463+
btn.innerHTML = '↑';
464+
btn.style.display = 'none';
465+
466+
// Make sure the button is added after the rest of content
467+
document.body.appendChild(btn);
468+
469+
const showThreshold = 200;
470+
471+
const updateVisibility = () => {
472+
const scrolled = window.pageYOffset || document.documentElement.scrollTop;
473+
if (scrolled > showThreshold) {
474+
btn.style.display = 'block';
475+
} else {
476+
btn.style.display = 'none';
477+
}
478+
};
479+
480+
// Throttle using requestAnimationFrame for better perf
481+
let ticking = false;
482+
window.addEventListener('scroll', () => {
483+
if (!ticking) {
484+
window.requestAnimationFrame(() => {
485+
updateVisibility();
486+
ticking = false;
487+
});
488+
ticking = true;
489+
}
490+
}, { passive: true });
491+
492+
// Smooth scroll while respecting user preference for reduced motion
493+
btn.addEventListener('click', () => {
494+
const prefersReduced = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
495+
if (prefersReduced) {
496+
window.scrollTo(0, 0);
497+
} else {
498+
window.scrollTo({ top: 0, behavior: 'smooth' });
499+
}
500+
btn.blur();
501+
});
502+
503+
// Keyboard accessibility (Enter/Space)
504+
btn.addEventListener('keydown', (e) => {
505+
if (e.key === 'Enter' || e.key === ' ') {
506+
e.preventDefault();
507+
btn.click();
508+
}
509+
});
510+
511+
// Initialize visibility on load
512+
updateVisibility();
513+
} catch (err) {
514+
console.error('BackToTop initialization failed:', err);
515+
}
450516
}

0 commit comments

Comments
 (0)