Hover Effect
Runnable MapLibre GL JS example: Hover Effect. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:悬停高亮
核心逻辑:mouseenter 给要素写 feature-state,circle 的绘制表达式读 hover 决定半径与颜色
数据:页面内生成的 40 个站点,promoteId 把属性 id 提为要素 id
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>悬停高亮</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;display:flex;gap:14px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<span>站点 <b>40</b></span>
<span>悬停 <b id="cur">—</b></span>
<span>feature-state 只在 GPU 侧变化,不重建源</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
'use strict';
if (typeof maplibregl === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var cities = [
[116.39, 39.90], [121.47, 31.23], [113.26, 23.12], [104.06, 30.57],
[108.94, 34.34], [114.30, 30.59], [120.15, 30.28], [106.55, 29.56]
];
var names = ['北站', '东站', '南站', '西站', '中站'];
var features = [];
for (var i = 0; i < 40; i++) {
var c = cities[i % cities.length];
features.push({
type: 'Feature',
properties: {
id: i,
name: names[i % 5] + ' ' + (i + 1),
load: 20 + Math.round(Math.random() * 80)
},
geometry: {
type: 'Point',
coordinates: [c[0] + (Math.random() - 0.5) * 3.2, c[1] + (Math.random() - 0.5) * 2.4]
}
});
}
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [110, 33],
zoom: 3.6,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
var curEl = document.getElementById('cur');
map.on('load', function () {
map.addSource('stations', {
type: 'geojson',
data: { type: 'FeatureCollection', features: features },
/* 让 properties.id 直接充当 feature-state 的 id */
promoteId: 'id'
});
map.addLayer({
id: 'stations-circle',
type: 'circle',
source: 'stations',
paint: {
'circle-radius': [
'case',
['boolean', ['feature-state', 'hover'], false],
11,
6
],
'circle-color': [
'case',
['boolean', ['feature-state', 'hover'], false],
'#ff922b',
'#2e86de'
],
'circle-stroke-width': 1.5,
'circle-stroke-color': '#ffffff'
}
});
});
var hoveredId = null;
function setHover(id, on) {
if (id === null || id === undefined) return;
map.setFeatureState({ source: 'stations', id: id }, { hover: on });
}
map.on('mousemove', 'stations-circle', function (e) {
var f = e.features[0];
if (hoveredId === f.id) return;
setHover(hoveredId, false);
hoveredId = f.id;
setHover(hoveredId, true);
map.getCanvas().style.cursor = 'pointer';
curEl.textContent = f.properties.name + ' · 负载 ' + f.properties.load + '%';
});
map.on('mouseleave', 'stations-circle', function () {
setHover(hoveredId, false);
hoveredId = null;
map.getCanvas().style.cursor = '';
curEl.textContent = '—';
});
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.