@@ -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)
448449if ( 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