我正在使用javascript制作一个下拉菜单 这是json文件
{
"bg1":"assets/bg/people_bg.jpg",
"bg2":"assets/bg/people_bg.jpg"
}
我只是想把名字作为bg1和bg2放到bg(n)下拉,这样用户可以选择任何背景。这是我的代码,用于dat.gui();
{
controller = new function() {
this.bg = '';
}
var gui = new dat.GUI();
f1 = gui.addFolder('Sphere');
f1.add( controller, 'bg', displayingNameOfBackground).onChange(function() {
//if change using their path to change background
);
答案 0 :(得分:1)
这取决于你想要的结果,但作为一个选项,你可以这样做:
var json = {
"background": {
"bg1": "assets/bg/people_bg1.jpg",
"bg2": "assets/bg/people_bg2.jpg"
}
};
var gui = new dat.GUI();
f1 = gui.addFolder('Sphere');
f1.add(json, "background", json.background).onChange(function(value) {
alert(value) // do the stuff to change background
});
f1.open();
body {
overflow: hidden;
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.5/dat.gui.min.js"></script>