Create a hover effect
Use events and feature states to create a per feature hover effect. See the example: https://docs.mapbox.com//mapbox-gl-js/example/hover-styles/
by jscastro76
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css">
<div class="container-fluid">
<div class="row">
<div class="col-lg-8">
<div id="map" style="height: 500px;"></div>
</div>
<div class="col-lg-4 bg-secondary">
</div>
</div>
</div>
CSS
.marker {
display: block;
border: none;
border-radius: 50%;
cursor: pointer;
padding: 0;
background-repeat: no-repeat;
background-size: cover;
}
.mapboxgl-popup {
max-width: 100px;
}
.as-console-wrapper { max-height: 15% !important; bottom: 0; }
JavaScript
mapboxgl.accessToken = 'pk.eyJ1IjoianNjYXN0cm8iLCJhIjoiY2s2YzB6Z25kMDVhejNrbXNpcmtjNGtpbiJ9.28ynPf1Y5Q8EyB_moOHylw';
var geojson = {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'properties': {
'message': 'Foo',
'class': 'tree',
'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
'iconSize': [60, 60]
},
'geometry': {
'type': 'Point',
'coordinates': [-66.324462890625, -16.024695711685304]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Bar',
'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
'class': 'xmas',
'iconSize': [50, 50]
},
'geometry': {
'type': 'Point',
'coordinates': [-61.2158203125, -15.97189158092897]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Baz',
'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
'class': 'meadow',
'iconSize': [40, 40]
},
'geometry': {
'type': 'Point',
'coordinates': [-63.29223632812499, -18.28151823530889]
}
}
]
};
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-65.017, -16.457],
zoom: 5
});
// Create a popup, but don't add it to the map yet.
var popup;
var el;
// add markers to map
geojson.features.forEach(function (marker) {
// Get each feature class
var cls = marker.properties['class'];
if (cls == 'tree') {
// create a DOM element for the tree marker
el = document.createElement('div');
el.className =...