Html Overlays
Runnable Cesium example: Html Overlays. 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
<!--
示例:HTML 浮层标注
说明:一个网页 div 跟随世界坐标移动,相机转动时始终贴着建筑
核心逻辑:cartesianToCanvasCoordinates → 每帧写 left / top → display 控制可见
依赖:CesiumJS 1.121 · jsDelivr CDN
-->
<!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>HTML 浮层标注</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
html,body{height:100%;margin:0}
body{background:#0e1116;color:#e6e8ec;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
#c{height:100%}
.tip{position:absolute;left:12px;top:12px;z-index:5;
background:rgba(23,28,35,.9);border:1px solid #2a313b;border-radius:6px;
padding:8px 12px;font-size:12px;color:#a2a9b6;max-width:320px;line-height:1.6}
.overlay{position:absolute;transform:translate(-50%,-100%);pointer-events:none;
background:rgba(23,28,35,.92);border:1px solid #2a313b;border-radius:6px;
padding:6px 10px;font-size:12px;color:#cfe2f5;white-space:nowrap;
box-shadow:0 6px 18px rgba(0,0,0,.35)}
.overlay span{display:block;color:#8fa3b8;font-size:11px;margin-top:2px}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">这个标注是真正的 DOM 元素,可以放按钮、图标或任意 HTML。</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
'use strict';
var viewer = new Cesium.Viewer('c', {
baseLayer: false,
baseLayerPicker: false, geocoder: false, homeButton: false,
sceneModePicker: false, navigationHelpButton: false,
animation: false, timeline: false, fullscreenButton: false,
infoBox: false, selectionIndicator: false
});
var scene = viewer.scene;
scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
var target = Cesium.Cartesian3.fromDegrees(116.3895, 39.9035, 60);
viewer.entities.add({
position: target,
box: {
dimensions: new Cesium.Cartesian3(40, 40, 60),
material: Cesium.Color.fromCssColorString('#4a6a8c')
}
});
var overlay = document.createElement('div');
overlay.className = 'overlay';
overlay.innerHTML = '<b>A 座</b><span>18 层 · 已满租</span>';
document.body.appendChild(overlay);
var pos2 = new Cesium.Cartesian2();
scene.preRender.addEventListener(function () {
var win = scene.cartesianToCanvasCoordinates(target, pos2);
if (!Cesium.defined(win)) { overlay.style.display = 'none'; return; }
overlay.style.display = 'block';
overlay.style.left = win.x + 'px';
overlay.style.top = win.y + 'px';
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.3860, 39.8998, 260),
orientation: { heading: Cesium.Math.toRadians(28), pitch: Cesium.Math.toRadians(-14), roll: 0 },
duration: 0
});
})();
</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.