|
|
| Line 4: |
Line 4: |
| /** | | /** |
| * Prevent Leaflet maps from auto-scrolling page on load | | * Prevent Leaflet maps from auto-scrolling page on load |
| * This intercepts scroll attempts during map initialization | | * by removing focusability from the map container |
| */ | | */ |
| (function() { | | (function() { |
| 'use strict'; | | 'use strict'; |
| | | |
| // Only apply fix if we're near the top of the page | | function disableMapFocus() { |
| if (window.scrollY > 100) return;
| | var maps = document.querySelectorAll('.leaflet-container'); |
| | maps.forEach(function(map) { |
| | map.setAttribute('tabindex', '-1'); |
| | }); |
| | } |
| | | |
| var savedPosition = window.scrollY; | | // Run immediately and again after a delay (maps initialize asynchronously) |
| var scrollLocked = true;
| | disableMapFocus(); |
|
| | setTimeout(disableMapFocus, 500); |
| // Temporarily override scrollIntoView to prevent map from scrolling page
| |
| var originalScrollIntoView = Element.prototype.scrollIntoView;
| |
| Element.prototype.scrollIntoView = function(options) {
| |
| // Allow scrollIntoView for non-map elements after unlock
| |
| if (!scrollLocked || !this.closest('.leaflet-container, .smw-format-leaflet')) {
| |
| originalScrollIntoView.call(this, options);
| |
| }
| |
| }; | |
|
| |
| // Also intercept any direct scroll attempts
| |
| var originalScrollTo = window.scrollTo.bind(window);
| |
| window.scrollTo = function(x, y) {
| |
| if (scrollLocked && savedPosition < 100) {
| |
| return; // Block scroll during initialization
| |
| }
| |
| originalScrollTo(x, y);
| |
| };
| |
|
| |
| // Restore normal behavior after maps have initialized
| |
| setTimeout(function() { | |
| scrollLocked = false;
| |
| Element.prototype.scrollIntoView = originalScrollIntoView;
| |
| window.scrollTo = originalScrollTo;
| |
| }, 1000);
| |
| })(); | | })(); |