【发布时间】:2019-02-25 02:51:08
【问题描述】:
https://jsfiddle.net/ykma6r0d/1/
演示链接 ^
HMTL:
<a-scene>
<!-- CAMERA CURSOR -->
<a-entity id="camera" camera mouse-cursor look-controls wasd-controls>
<a-cursor fuse="true" color="red"></a-cursor>
</a-entity>
<a-entity id="myTarget" scale="1 .001 1" look-at="#camera" text="color: black;align: center; value: YAYA!; width: 6; wrap-count: 10" animation__1="startEvents: in;property: scale; dur: 200; from: 1 .001 7; to: 1 1 1" animation__2="startEvents: out;property: scale; dur: 200; from: 1 1 1; to: 1 .001 1"></a-entity>
<a-circle name-on-hover="target: #myTarget" position="-2 0 -10" material="color: blue" material="opacity:.75" look-at="#camera" animation="property: position;easing: easeInSine;loop:true;dir: alternate;dur: 2000;to:-2 .2 -10">
</a-circle>
</a-scene>
JS:
AFRAME.registerComponent("name-on-hover", {
schema: {
target: {
type: "selector",
default: ""
}
},
init: function() {
var target = this.data.target;
var el = this.el;
this.setupNamePos();
el.addEventListener("mouseenter", function() {
target.emit("in"); // animate in
});
el.addEventListener("mouseleave", function() {
target.emit("out"); // animate out
});
},
setupNamePos: function() {
var name = this.data.target; // get name element
var elPos = this.el.getAttribute("position"); // get the pos of hovered element
name.setAttribute("position", {
//set name position relevant to hovered element
x: elPos.x,
y: elPos.y + 2,
z: elPos.z
});
}
});
我有两个元素,一个圆圈和一个文本元素
圆形元素:其上方的文本需要在悬停时进行动画处理。在我的 JS 中,我对其进行了设置,以便它抓取圆的位置,然后将该位置设置为我的文本元素,但在此之前它将 Y 位置增加 2 个单位。
由于某种原因,当我将鼠标悬停在圆圈上时,我的动画会疯狂循环。起初我认为这可能是因为文本元素与圆圈重叠,失去了悬停事件。但是我尝试通过修改 X 和 Z 将文本元素移动到不同的位置.. 但没有运气。
【问题讨论】: