我想知道如何使用 JavaScript 和 A-frame ( https://aframe.io ) 创建这样的东西。
我想创建一个名为 load()
的 onload 函数,它将检查变量 x 的值,如果 x 是一个,则 gltf
的 id 1 将显示,而 id 为 2 和 3 的 gltf 将不可见。 gltf 的 2 和 3 也是如此。我怎样才能完成这个?我的代码:
<script>
var x = 1;
</script>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<body onload="load()">
<a-gltf-model id="1" src="https://cdn.glitch.com/2556b706-79db-4661-ab72-d86cfd5b5649%2Fscene.glb?v=1622850633562" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-gltf-model id="2" src="https://cdn.glitch.com/6632f840-a210-4bdc-a62f-bc38e65ae48a%2Fscene%20-%202021-06-12T230318.001.glb?v=1623564342354" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-gltf-model id="3"src="https://cdn.glitch.com/6632f840-a210-4bdc-a62f-bc38e65ae48a%2Fwinter.glb?v=1623564477495" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-scene>
</a-scene>
</body>
最佳答案
将有效的 id
存储在数组中并循环遍历每个,然后相应地删除。
const ids = [1, 2, 3];
function load() {
var x = 1;
ids.forEach((e) => {
if (e != x) {
document.getElementById(e).remove();
}
})
}
a-gltf-model {
display: none;
}
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<body onload="load()">
<a-scene>
<a-gltf-model id="1" src="https://cdn.glitch.com/2556b706-79db-4661-ab72-d86cfd5b5649%2Fscene.glb?v=1622850633562" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-gltf-model id="2" src="https://cdn.glitch.com/6632f840-a210-4bdc-a62f-bc38e65ae48a%2Fscene%20-%202021-06-12T230318.001.glb?v=1623564342354" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-gltf-model id="3" src="https://cdn.glitch.com/6632f840-a210-4bdc-a62f-bc38e65ae48a%2Fwinter.glb?v=1623564477495" position="-0 6 -1" scale="0.01 0.01 0.01"></a-gltf-model>
<a-plane width="100" height="100" position=" 0.00 0.00 0.00" rotation="-90 0 0" color="royalblue"></a-plane>
</a-scene>
</body>
关于javascript - 具有功能的 A 帧检查变量并根据值显示不同的 gltf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67954505/