【发布时间】:2021-08-19 14:09:31
【问题描述】:
我为 Aframe 编写了一个组件,用于在光标悬停时更改图像的 src 和 scale。然后,当它退出时,我想恢复默认值。
所以,我存储了默认 src 和默认比例值,然后用 setAttribute 更新它 当光标离开时,它会返回到默认的src,而不是默认的比例。
如何正确地做到这一点?
这是我的组件:
AFRAME.registerComponent('change-hotspots-on-hover', {
schema: {
src: {default: '#hotspots'},
scale: {type: 'vec3', default: {x: 1, y: 1, z: 1}},
},
init: function () {
var data = this.data;
var el = this.el;
var defaultSrc = el.getAttribute('src');
var defaultScale = el.getAttribute('scale');
console.log("Init");
el.addEventListener('mouseenter', function (){
console.log("Enter");
el.setAttribute('src', data.src);
setTimeout(() => {
el.setAttribute('scale', data.scale);
}, 1);
});
el.addEventListener('mouseleave', function (){
console.log("Exit");
el.setAttribute('src', defaultSrc);
el.setAttribute('scale', defaultScale);
});
}
});
还有我的图片:
<a-image position="-10 4 -10" scale="1 1 1" material="" src="#hotspot"
change-hotspots-on-hover="src: #hotspot_hover; scale: 7 3 2;"></a-image>
【问题讨论】:
-
哪个鼠标事件不能正常工作? “mouseleave”还是“mouseenter”?
-
两者都能正常工作:src 和 scale 随 mouseenter 变化,Src 随 mouseleave 而变化但不缩放。问题似乎来自在 mouseenter 之后被覆盖的 var defaultScale。我试图 consol.log 它,之后就不一样了。
-
请对此进行测试:使用“const”或“let”代替带有“var”的变量声明
-
不工作。没有任何变化:/
标签: javascript aframe