現在、マウスの位置に応じて更新される画像があります。[http://www.fore6.com/?p=533][1]をチェックしてください
[1]:http ://www.fore6.com/?p= 533で、私が何を意味するのかを理解できます。
問題は、これを適用する必要のある複数の画像があり、各画像が互いに独立してアニメーション化する必要があることです。現時点では、それらはすべて同時にアニメートします。マウスの位置は画像ごとに異なるため、個別にアニメーション化する必要があります。
画像ごとに関数を繰り返し、変数を変更することで機能させることができますが、それは非常に多くのコードです。1つの関数内でこれを行うにはどうすればよいですか?
各画像を配列に入れるか、$(this)を使用する必要があるかもしれないと推測していますが、それを実装する方法を理解できないようです。
どんな助けでも大歓迎です。私が使用しているコードは以下のとおりです。
var imageX = null;
var imageY = null;
imageX = $('.anim-photo').offset().left;
imageY = $('.anim-photo').offset().top;
$(document).mousemove(function(event) {
var mousePos = new Array(event.pageX, event.pageY);
interact(mousePos, ["0px", "-288px", "-576px"]);
})
function interact(mouseCord, aniCord) {
if (mouseCord[0] < imageX && mouseCord[1] < imageY){ // Box-1
$(".anim-photo").css("background-position", aniCord[0]+" 0px");
} else if (mouseCord[0] > imageX && mouseCord[0] < imageX+285 && mouseCord[1] < imageY){ // Box-2
$(".anim-photo").css("background-position", aniCord[1]+" 0px");
} else if (mouseCord[0] > imageX+285 && mouseCord[1] < imageY){ // Box-3
$(".anim-photo").css("background-position", aniCord[2]+" 0px");
} else if (mouseCord[0] < imageX && mouseCord[1] > imageY && mouseCord[1] < imageY+357){ // Box-4
$(".anim-photo").css("background-position", aniCord[0]+" -360px");
} else if (mouseCord[0] > imageX && mouseCord[0] < imageX+285 && mouseCord[1] > imageY && mouseCord[1] < imageY+357){ // Box-5
$(".anim-photo").css("background-position", aniCord[1]+" -360px");
}else if (mouseCord[0] > imageX+285 && mouseCord[1] > imageY && mouseCord[1] < imageY+357){ // Box-6
$(".anim-photo").css("background-position", aniCord[2]+" -360px");
} else if (mouseCord[0] < imageX && mouseCord[1] > imageY+357){ // Box-7
$(".anim-photo").css("background-position", aniCord[0]+" -720px");
} else if (mouseCord[0] > imageX && mouseCord[0] < imageX+285 && mouseCord[1] > imageY+357){ // Box-8
$(".anim-photo").css("background-position", aniCord[1]+" -720px");
} else if (mouseCord[0] > imageX+285 && mouseCord[1] > imageY+357){ // Box-9
$(".anim-photo").css("background-position", aniCord[2]+" -720px");
}
};