在cesium中加入弹窗功能,实现点击模型出现弹窗

         实现效果如图,如果有更好的实现效果可以发评论区交流一下。

 一、添加弹窗和关闭按钮

<div id="popup" class="info">
    <p id="popupContent" style="font-size: 14px;">信息</p>
    <button id="popupClose" style="margin-top: 10px;">关闭</button>
</div>

 二、添加弹窗功能

 

viewer.screenSpaceEventHandler.setInputAction((click) => {
            const pickedObject = viewer.scene.pick(click.position);
            if (Cesium.defined(pickedObject) && pickedObject.id && pickedObject.id._id === '模型') {
                const pop = document.getElementById("popup");
                if (pop) {
                    const canvasRect = viewer.scene.canvas.getBoundingClientRect();
                    const top = click.position.y + canvasRect.top;
                    const left = click.position.x + canvasRect.left;
                    pop.style.top = top + "px";
                    pop.style.left = left + "px";
                    pop.style.display = "block"; // 显示弹出层

                    // 更新弹出内容
                    document.getElementById("popupContent").innerHTML = `
<span style="color: white;">
    设计模型详细信息:<br><br>
    高度分别是:101.50米、66.21米<br><br>
    斜度分别是:0.000881、0.003441<br><br>
    1、红色代表无人机飞行测试轨迹<br><br>
    2、蓝色表示两个塔之间连线的中点<br><br>
    以及相邻两条线之间的距离
</span>`;
                    this.overlayChartObj.chartData = document.getElementById("popupContent").innerText;
                    this.popVisible = true;
                }
            } else {
                // 点击对象不是目标时隐藏弹出层
                document.getElementById("popup").style.display = "none";
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

         注意在使用这段代码之前,加载模型时加上一个id,这样可以精准找到这个模型。

三、添加关闭按钮功能 

 

document.getElementById("popupClose").addEventListener('click', () => {
    document.getElementById("popup").style.display = "none";
});

 四、样式

 

.info {
    display: none;
    position: absolute;
    z-index: 99;
    border: 1px solid #00ffd0;
    background-color: rgba(24, 130, 14, 0.54);
    padding: 10px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    border-radius: 20px;
}

#popupClose {
    border-radius: 5px;
}

#popupClose:hover {
    background-color: #00ff59; /* 悬停时的背景色 */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值