Skip to main content

Add a fade effect when showing and hiding a layer

This example uses fill-opacity-transition to create a fade effect when changing a layer's opacity.

When the "Fade Out" button is clicked, setPaintProperty is used to update the fill layer's fill-opacity-transition and opacity properties. fill-opacity-transition is then set to the value specified by the slider, and opacity is toggled (from 1 to 0 for fade out or from 0 to 1 for fade in)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add a fade effect when showing and hiding a layer</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.2.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
#fade {
display: block;
position: relative;
margin: 0px auto;
width: 100%;
height: 40px;
padding: 10px;
border: none;
border-radius: 3px;
font-size: 12px;
text-align: center;
color: #fff;
background: #ee8a65;
}

.map-overlay {
font:
12px/20px 'Helvetica Neue',
Arial,
Helvetica,
sans-serif;
position: absolute;
width: 25%;
top: 0;
left: 0;
padding: 10px;
}

.map-overlay .map-overlay-inner {
background-color: #fff;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
border-radius: 3px;
padding: 10px;
margin-bottom: 10px;
}

.map-overlay h2 {
line-height: 24px;
display: block;
margin: 0 0 10px;
}

.map-overlay input {
background-color: transparent;
display: inline-block;
width: 100%;
position: relative;
margin: 0;
cursor: ew-resize;
text-align: center;
font-weight: normal;
}
</style>
<div id="map"></div>
<br>

<div class="map-overlay top">
<div class="map-overlay-inner">
<h2>Duration (ms)</h2>
<label id="ms">500</label>
<input id="slider" type="range" min="0" max="1000" step="50" value="500">
</div>
<button id="fade">Fade out</button>
</div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map', // container ID
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/light-v11', // style URL
center: [-68.137343, 45.137451], // starting position
zoom: 5 // starting zoom
});

map.on('load', () => {
// Add a data source containing GeoJSON data.
map.addSource('maine', {
'type': 'geojson',
'data': {
'type': 'Feature',
'geometry': {
'type': 'Polygon',
// These coordinates outline Maine.
'coordinates': [
[
[-67.13734, 45.13745],
[-66.96466, 44.8097],
[-68.03252, 44.3252],
[-69.06, 43.98],
[-70.11617, 43.68405],
[-70.64573, 43.09008],
[-70.75102, 43.08003],
[-70.79761, 43.21973],
[-70.98176, 43.36789],
[-70.94416, 43.46633],
[-71.08482, 45.30524],
[-70.66002, 45.46022],
[-70.30495, 45.91479],
[-70.00014, 46.69317],
[-69.23708, 47.44777],
[-68.90478, 47.18479],
[-68.2343, 47.35462],
[-67.79035, 47.06624],
[-67.79141, 45.70258],
[-67.13734, 45.13745]
]
]
}
}
});

// Add a new layer to visualize the polygon.
map.addLayer({
'id': 'maine',
'type': 'fill',
'source': 'maine', // reference the data source
'layout': {},
'paint': {
'fill-color': '#0080ff', // blue color fill
'fill-opacity': 1, // 100% opaque
'fill-opacity-transition': { duration: 500 } // 500 milliseconds = 1/2 seconds
}
});
// Add a black outline around the polygon.
map.addLayer({
'id': 'outline',
'type': 'line',
'source': 'maine',
'layout': {},
'paint': {
'line-color': '#000',
'line-width': 3
}
});
});

// Consume fill-opacity-transition, switch between fade out and fade in on click, and fade.
document.getElementById('fade').addEventListener('click', () => {
map.setPaintProperty('maine', 'fill-opacity-transition', {
duration: parseInt(document.getElementById('slider').value)
});
const fader = document.getElementById('fade');
const fadeText = fader.textContent;
if (fadeText === 'Fade out') {
document.querySelector('#fade').textContent = 'Fade in';
map.setPaintProperty('maine', 'fill-opacity', 0);
} else {
document.querySelector('#fade').textContent = 'Fade out';
map.setPaintProperty('maine', 'fill-opacity', 1);
}
});

// Update the text content that is used directly as the fill-opacity-transition value.
document.getElementById('slider').addEventListener('input', (e) => {
const milliseconds = e.target.value;
document.getElementById('ms').textContent = milliseconds.toString();
});
</script>

</body>
</html>
Was this example helpful?