Layer Order
Runnable Cesium example: Layer Order. 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
<!--
示例:影像图层的顺序控制
要点:三层程序生成的栅格叠在一起,raise/lower 调整索引,索引决定最终合成顺序
依赖:CesiumJS 1.121 · jsDelivr CDN,GridImageryProvider 不请求任何瓦片
-->
<!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/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%}
.hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;max-width:268px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:6px}
.layer{display:flex;align-items:center;gap:6px;margin-top:5px}
.layer .name{flex:1 1 auto;color:#cfd6e2;font-size:12px}
.hud button{font:inherit;font-size:11px;padding:2px 7px;border-radius:4px;cursor:pointer;
border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button:hover{background:#262d37}
input[type=range]{width:100%;margin-top:6px}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
<div><b>图层顺序</b><span style="color:#7f8899"> 上层优先</span></div>
<div id="list"></div>
<div class="row">整体透明度</div>
<div class="row"><input id="alpha" type="range" min="0" max="1" step="0.05" value="1"></div>
</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('#0f1d29');
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 BLUE = Cesium.Color.fromCssColorString('#4d9bff');
var ORANGE = Cesium.Color.fromCssColorString('#ffb13d');
var GREEN = Cesium.Color.fromCssColorString('#3fe0b0');
function gridLayer(color, cells, alpha, name) {
var layer = new Cesium.ImageryLayer(new Cesium.GridImageryProvider({
color: color.withAlpha(0.65),
glowColor: Cesium.Color.TRANSPARENT,
backgroundColor: color.withAlpha(0.10),
cells: cells,
tileWidth: 256,
tileHeight: 256
}), { alpha: alpha });
viewer.imageryLayers.add(layer);
layer.__baseAlpha = alpha;
layer.__name = name;
return layer;
}
gridLayer(BLUE, 4, 0.55, '蓝色 4×4 网格');
gridLayer(ORANGE, 8, 0.45, '橙色 8×8 网格');
var numbering = new Cesium.ImageryLayer(new Cesium.TileCoordinatesImageryProvider({
color: GREEN,
backgroundColor: Cesium.Color.TRANSPARENT,
tileWidth: 256,
tileHeight: 256
}), { alpha: 0.9 });
viewer.imageryLayers.add(numbering);
numbering.__baseAlpha = 0.9;
numbering.__name = '瓦片行列号';
function renderList() {
var layers = viewer.imageryLayers;
var box = document.getElementById('list');
box.innerHTML = '';
for (var i = layers.length - 1; i >= 0; i--) {
(function (index) {
var layer = layers.get(index);
var row = document.createElement('div');
row.className = 'layer';
var name = document.createElement('span');
name.className = 'name';
name.textContent = (index + 1) + '. ' + layer.__name;
row.appendChild(name);
var up = document.createElement('button');
up.textContent = '↑';
up.title = '上移一层';
up.addEventListener('click', function () {
if (index < viewer.imageryLayers.length - 1) {
viewer.imageryLayers.raise(layer);
renderList();
}
});
row.appendChild(up);
var down = document.createElement('button');
down.textContent = '↓';
down.title = '下移一层';
down.addEventListener('click', function () {
if (index > 0) {
viewer.imageryLayers.lower(layer);
renderList();
}
});
row.appendChild(down);
var show = document.createElement('button');
show.textContent = layer.show ? '隐藏' : '显示';
show.addEventListener('click', function () {
layer.show = !layer.show;
renderList();
});
row.appendChild(show);
box.appendChild(row);
})(i);
}
}
document.getElementById('alpha').addEventListener('input', function () {
var k = parseFloat(this.value);
var layers = viewer.imageryLayers;
for (var i = 0; i < layers.length; i++) {
layers.get(i).alpha = layers.get(i).__baseAlpha * k;
}
});
renderList();
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(105, 20, 20000000),
orientation: { heading: 0, pitch: Cesium.Math.toRadians(-90), 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.