我写了一个组件。一开始,两个物体(抽屉和注入餐具)的运动是同步的。点击后应按下灌装餐具,抽屉的运动应独立。注射器不再动了。现在代码的结果是:在单击Infusion刀之后,条件为"if(data.moveWithInfusion===true)“的代码仍然运行。"if(data.moveWithInfusion===false)“下的代码也会运行。我找不到原因。有人知道原因吗?谢谢!https://glitch.com/edit/#!/skill-lab
AFRAME.registerComponent('drawerwithinfusioncutlery',{
schema: {
open : {default: '1.8 0 0.1'},
close : {default: '0 0 0.1'},
dur : {default: 300},
moveWithInfusion :{default: true},
},
update: function (){
var data= this.data;
var el=this.el;
var closed=true;
var infusion=document.querySelector("#infusioninpack");
el.addEventListener("click",function(){
if(data.moveWithInfusion===true){
alert("true");
if(closed === true){
open(el, data.close, data.open, data.dur);
open(infusion, '1.24 4.439 1.555', '3.04 4.439 1.555', data.dur);
closed=false;
}else if(closed===false){
open(el, data.open, data.close, data.dur);
open(infusion, '3.04 4.439 1.555', '1.24 4.439 1.555', data.dur);
closed=true;
}
}else if(data.moveWithInfusion===false){
alert("false");
if(closed === true){
open(el, data.close, data.open, data.dur);
closed=false;
}else if(closed===false){
open(el, data.open, data.close, data.dur);
closed=true;
}
}
});
infusion.addEventListener("click",function(){
el.setAttribute('drawerwithinfusioncutlery', {moveWithInfusion: 'false'})
});
}
});
发布于 2017-08-03 11:23:40
首先,事件侦听器应该在init
函数中,以防止重复它们。
因此,我抛出了init
函数的“单击”侦听器。我在init
中添加了“关闭”变量,就像在侦听器的labda函数中使用的那样,现在它似乎正常工作了:
https://glitch.com/edit/#!/gigantic-toucan?path=public/js/drawerwithinfusioncutlery.js:26:25
https://stackoverflow.com/questions/45479605
复制相似问题