Javascript 尝试对鼠标指针的多个元素进行评级

Javascript 尝试对鼠标指针的多个元素进行评级,javascript,jquery,Javascript,Jquery,我正试图朝着鼠标光标旋转多个元素。它看起来像是在工作,但它们都是朝着同一个方向旋转,而不是朝着我的鼠标。我假设其中一个跟在我的鼠标后面,其余的跟那个角度一样 我怎样才能把它们分别朝着我的鼠标旋转呢?我做错什么了吗 有人能告诉我怎么让他们都指向我的鼠标吗 $(函数(){ $('.js followMouse').followMouse(); }); $.fn.followMouse=函数(){ 返回$(此).each(函数(索引,项){ 项目=$(项目); 如果(!item.data('Foll

我正试图朝着鼠标光标旋转多个元素。它看起来像是在工作,但它们都是朝着同一个方向旋转,而不是朝着我的鼠标。我假设其中一个跟在我的鼠标后面,其余的跟那个角度一样

我怎样才能把它们分别朝着我的鼠标旋转呢?我做错什么了吗

有人能告诉我怎么让他们都指向我的鼠标吗

$(函数(){
$('.js followMouse').followMouse();
});
$.fn.followMouse=函数(){
返回$(此).each(函数(索引,项){
项目=$(项目);
如果(!item.data('FollowMouse')){
item.data('FollowMouse',新的FollowMouse(item));
}
});
};
var FollowMouse=函数(元素){
this.element=元素;
鼠标座={
x:-1,
y:-1
};
$('body').on('mousemove',this.rotateObject.bind(this));
};
FollowMouse.prototype.rotateObject=function(){
mousePos.x=event.pageX;
mousePos.y=event.pageY;
变量curPos={
x:$('img').offset()左,
y:$('img').offset().top
};
var nextPos={
x:mousePos.x,
y:老鼠皮
};
$(this.element).find('img').each(function(){
offsetLeft=mousePos.x-$(this.offset().left;
offsetTop=mousePos.y-$(this.offset().top;
deg=(数学atan2(nextPos.y-curPos.y,nextPos.x-curPos.x)*180/数学PI);
$(this.css)({
“-webkit变换”:“旋转(“+deg+”deg)”,
“-moz变换”:“旋转(“+deg+”deg)”,
“-ms变换”:“旋转(“+deg+”deg)”,
“-o变换”:“旋转(“+deg+”deg)”,
“变换”:“旋转(“+deg+”deg)”
});
});
};
.pencil img{
高度:5px;
宽度:20px;
背景色:#000000;
}
.铅笔-2{
左边距:150像素
}
正文{
宽度:100vw;
高度:100vh;
}

好主意。您的
curPos
参数只需为
每个循环中的每个图像重新定义:

$(函数(){
$('.js followMouse').followMouse();
});
$.fn.followMouse=函数(){
返回$(此).each(函数(索引,项){
项目=$(项目);
如果(!item.data('FollowMouse')){
item.data('FollowMouse',新的FollowMouse(item));
}
});
};
var FollowMouse=函数(元素){
this.element=元素;
鼠标座={
x:-1,
y:-1
};
$('body').on('mousemove',this.rotateObject.bind(this));
};
FollowMouse.prototype.rotateObject=function(){
mousePos.x=event.pageX;
mousePos.y=event.pageY;
变量curPos={
x:$('img').offset()左,
y:$('img').offset().top
};
var nextPos={
x:mousePos.x,
y:老鼠皮
};
$(this.element).find('img').each(function(){
curPos.x=$(此).offset()左,
curPos.y=$(this.offset().top;
//curPos只需要为每个图像定义
offsetLeft=mousePos.x-$(this.offset().left;
offsetTop=mousePos.y-$(this.offset().top;
deg=(数学atan2(nextPos.y-curPos.y,nextPos.x-curPos.x)*180/数学PI);
$(this.css)({
“-webkit变换”:“旋转(“+deg+”deg)”,
“-moz变换”:“旋转(“+deg+”deg)”,
“-ms变换”:“旋转(“+deg+”deg)”,
“-o变换”:“旋转(“+deg+”deg)”,
“变换”:“旋转(“+deg+”deg)”
});
});
};
。铅笔img{
高度:5px;
宽度:20px;
背景色:#000000;
}
.铅笔-2{
左边距:150像素
}
身体{
宽度:100vw;
高度:100vh;
}

当然,这很有道理!谢谢你帮助我。