简体   繁体   English

如何改变 <a-sky> 动画触发事件的颜色

[英]How To Change <a-sky> Color With Event Set Off By An Animation

My project basically is an assortment of blocks and the goal is to have the blocks rise on mouse hover as well as the "a-sky" component to change color to whatever color I set it too. 我的项目基本上是各种各样的块,目标是使块在鼠标悬停时抬起,以及“ a-sky”组件将颜色更改为我设置的任何颜色。 Each cube will change the color of the sky to its color. 每个立方体都会将天空的颜色更改为其颜色。 Im having difficulty getting it to work. 我很难让它工作。 The first thing I tried was to set an animation to the a-sky component and have it begin with an event. 我尝试的第一件事是将动画设置为a-sky组件,并使其从事件开始。 Then I used Javascript in the block animation to set off the event when the block begins to hover. 然后,当块开始悬停时,我在块动画中使用了Javascript来触发事件。 Here is what I have in the Glitch link below and the code is also below 这是我在下面的故障链接中所包含的内容,代码也在下面

https://glitch.com/edit/#!/join/b3c644bf-cbf9-4d26-80f4-cb6e5a00a556 https://glitch.com/edit/#!/join/b3c644bf-cbf9-4d26-80f4-cb6e5a00a556

 <!-- Sky -->
 <a-sky id="sky" color="beige" geometry="">
<a-animation attribute="color" begin="Normal" to="beige" dur="1000"></a-animation>
<a-animation attribute="color" begin="Orange" from="beige" to="#ff7400" dur="1000"></a-animation>
<a-animation attribute="color" begin="Dark-Yellow" from="beige" to="#ffe50a" dur="1000"></a-animation>

<!-- Orange Block -->
<a-box id="Orange-Box" color="#ff7400" navigate-on-click="url:http://google.com" position="0 1.5 -5" material="" geometry="">
    <a-animation attribute="position" from="0 1.5 -5" to="0 2.5 -5" begin="mouseenter">
    <script> document.querySelector('#sky').emit('Orange'); </script>
  </a-animation>
  <a-animation attribute="position" from="0 2.5 -5" to="0 1.5 -5" begin="mouseleave">
    <script> document.querySelector('#sky').emit('Normal'); </script>
  </a-animation>
</a-box>

<!-- Dark Yellow Block-->
<a-box color="#ffe50a" position="2.5 1.5 -4" rotation="0 -33.11696055856158 0" material="" geometry="">
    <a-animation attribute="position" from="2.5 1.5 -4" to="2.5 2.5 -4" begin="mouseenter">
    <script> document.querySelector('#sky').emit('Dark-Yellow'); </script>
  </a-animation>
  <a-animation attribute="position" from="2.5 2.5 -4" to="2.5 1.5 -4" begin="mouseleave">
    <script> document.querySelector('#sky').emit('Normal'); </script>
  </a-animation>
</a-box>

You have to add an event listener to the <a-animation> element to get the moment when the animation has ended 您必须向<a-animation>元素添加事件侦听器,以获取动画结束的时刻

document.querySelector("#animation_component").addEventListener("animationend", (e) => {
  document.querySelector('#sky').emit('Normal');
})

Like i did here . 就像我在这里一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM