Javascript A帧颜色设置为变量
我想知道在A-frame(aframe.io)中如何将对象的颜色设置为变量。例如,如果名为x的变量的值为#8F3A84,则形状的颜色将设置为该颜色。如果变量的值为#738F3A,则形状也将设置为该颜色。我怎样才能做到这一点?Javascript A帧颜色设置为变量,javascript,html,css,aframe,virtual-reality,Javascript,Html,Css,Aframe,Virtual Reality,我想知道在A-frame(aframe.io)中如何将对象的颜色设置为变量。例如,如果名为x的变量的值为#8F3A84,则形状的颜色将设置为该颜色。如果变量的值为#738F3A,则形状也将设置为该颜色。我怎样才能做到这一点? 以下是我的形状的代码:您可以使用函数而不是变量。例如: const changeColor = (eleId, colorValue) => { const box = document.getElementById(eleId) box.setAttribu
以下是我的形状的代码:
您可以使用函数而不是变量。例如:
const changeColor = (eleId, colorValue) => {
const box = document.getElementById(eleId)
box.setAttribute('color', colorValue)
}
编辑
全面实施
index.html
<HTML>
<body>
<a-scene stats="" inspector="" keyboard-shortcuts="" screenshot="" vr-mode-ui="" device-orientation-permission-ui="">
<a-box id='test' position="0 5 0" static-body="" width="5" depth="5" height="5" material="" geometry=""></a-box>
</a-scene>
</body>
<script>
window.changeColor = (eleId, colorValue) => {
const box = document.getElementById(eleId)
box.setAttribute('color', colorValue)
}
changeColor('test', 'red')
</script>
</html>
window.changeColor=(eleId,colorValue)=>{
常量框=document.getElementById(eleId)
box.setAttribute('color',colorValue)
}
changeColor('测试','红色')
从任何地方调用changeColor函数您好,很抱歉回复得太晚,我正在尝试编写代码,但无法正常工作。有这样的例子吗?我尝试了所有的代码并创建了变量,但是我的多维数据集显示为白色,这是默认颜色。我更新了我的答案以获得完整的实现。我对您的
a-box
标签所做的唯一更改是更改位置、高度、宽度和深度值。