【发布时间】:2019-11-24 21:05:34
【问题描述】:
我正在尝试通过document.getElementsByClassName() 为多个帮助图标设置动画。我知道它返回一个类似对象的数组,它实际上是具有相同类的所有元素的节点列表,我也知道我可以通过附加我要访问的节点的index[4] 来访问各个项目。但是我不确定如何访问所有节点。我试过[*].animate(),但没有奏效。我还尝试了下面列出的forEach => 函数,但它仍然无法正常工作,我尝试了[0-8] 没有任何反应。
谁能发现我做错了什么?
我知道我可以将它包装在 for、while 或 forEach 循环甚至 switch 语句中,但这是我目前拥有的代码:
const infIco = document.getElementsByClassName('helpIco')[*].forEach(item =>{
item.addEventListener('mouseover',(e) => {
this.animate([
{ transform: 'scale(1)', opacity:1},
{ transform: 'scale(1.42) rotate(22deg)', opacity: .8},
{ transform: 'scale(1)', opacity: 1},
], {
duration: 1560, // ms
easing: 'ease-in-out', // 'linear', bezier curve, etc..cubic-bezier(.43,.95,.85,.14)
delay: 4, // ms
iteration: Infinity, // Or number
direction: 'normal', // 'normal', 'reverse' etc...
fill: 'forwards' // 'backwards', 'both', 'none', 'auto.'
})
// Todo...
})
});
我知道.animate() 部分正在工作,因为我也将它附加到另一个项目并且它工作正常。有时可能会有点小故障,但这很可能是由于使用了旧显卡和杂乱无章的计算机。
【问题讨论】:
标签: javascript animation dom-events mouseover