我想在鼠标悬停时给每根头发一个反弹动画。动画部分是在第一根头发上完成的,但我不想给每一个头发图像都提供事件。所以这是我的问题,我在我的 javascript 代码中使用构造函数。是否有可能在我的原型中创建一个方法并创建它的实例?所以基本上我不想触发 10 个事件或 10 个 addEventListeners,我想要一个聪明的方法来解决这个问题。我如何完成我的任务,因为每根头发都应该只在鼠标悬停时反弹
我的代码:HTML:
<div class="main">
<div class="hair">
<img src="images/single.png" id="hair1" width="13" height="40" >
<img src="images/single.png" id="hair2" width="13" height="40">
<img src="images/single.png" id="hair3" width="13" height="40">
<img src="images/single.png" id="hair4" width="13" height="40">
<img src="images/single.png" id="hair5" width="13" height="40">
<img src="images/single.png" id="hair6" width="13" height="40">
<img src="images/single.png" id="hair7" width="13" height="40">
<img src="images/single.png" id="hair8" width="13" height="40">
<img src="images/single.png" id="hair9" width="13" height="40">
</div>
<div class="face">
<img src="images/ec_logo.png">
</div>
Javascript:
(function(){
hair=function (){
return this;
};
hair.prototype={
bounce:function(){
//code for some bounce animation
}
};
})();
document.getElementById('hair??').addEventListener("mouseover",???,false);????????//this is the part i am confused about,how to give same animation to every single hair by using object instances???