【发布时间】:2021-03-06 12:01:39
【问题描述】:
我的舞台上有 8 个学生,它们与鼠标光标交互移动。我不知道如何将css独立应用于每个。有人可以帮我解决这个问题吗?
这是代码
jQuery(function($){
/*I have 8 pupils in total*/
$("body").mousemove(function(event) {
var eye = $(".pupil");
var x = (eye.offset().left) + (eye.width() / 2);
var y = (eye.offset().top) + (eye.height() / 2);
var rad = Math.atan2(event.pageX - x, event.pageY - y);
var rot = (rad * (180 / Math.PI) * -1) + 180;
/*I can't figure out how to fix the -for each-*/
$(eye).each(function() {
$(this).css({ 'transform': 'rotate(' + rot + 'deg)'});
});
});
});
这里是 jsFiddle:https://jsfiddle.net/NickBil/qaspfk1L/2/
【问题讨论】:
-
$(".pupil")是一个数组,你应该遍历它。这会有所帮助:api.jquery.com/jquery.each -
谢谢,会好好看看这个。
标签: jquery css rotation each mousemove