Maison > Article > interface Web > Pourquoi ma carte Leaflet ne s'affiche-t-elle pas lorsqu'elle est placée dans un onglet de basculement de données ?
Problems with Leaflet Map in Data-Toggle Tab
When using tabs to display content, it is possible to encounter issues where a Leaflet map fails to download when placed within a data-toggle tab.
Cause
The issue arises due to the way Leaflet initializes the map container size. When the map is initialized, Leaflet reads the container size, which can later become invalid if the container is hidden or its dimensions are changed.
Solution
To resolve this issue, you need to manually update the container size after the tab is displayed. This can be achieved using the map.invalidateSize() method.
Implementation
In your JavaScript code, add a listener to the tab button's click event to trigger the map update:
$(document).ready(function () { var map = new L.Map('carteBenef'); // ... (rest of your code) // Add listener to tab button click $('#carteTabButton').on('click', function () { map.invalidateSize(); }); });
Alternatively, you can use framework-specific methods to trigger the map update based on the tab's visibility. For instance, in Bootstrap you can use the shown.bs.tab event:
$('#carteTab').on('shown.bs.tab', function () { map.invalidateSize(); });
By updating the map container size after the tab is displayed, you can ensure that the Leaflet map downloads and renders correctly within the tab.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!