0
我正在創建一個示例應用程序,我需要在用戶將光標移離光標後儘快更改div的顏色。jQuery動畫鼠標事件
它工作但準確,不知道我哪裏出錯了。
請參閱下面的示例應用程序。
http://jsfiddle.net/manishparab/3q1trzwk/
var cursorX = 0;
var cursorY = 0;
$(document).ready(function() {
$(".a").mouseover(function() {
animateDiv();
setInterval(function() {
isCursorOnSquare('.a')
}, 1);
});
});
$(document).on('mousemove', function(e) {
cursorX = e.pageX;
cursorY = e.pageY;
});
function isCursorOnSquare(elem) {
var pos, width, height;
pos = $(elem).position();
if ((Math.abs(pos.left - cursorX) <= 100 && Math.abs(pos.top - cursorY) <= 100)) {
$(elem).css("background-color", "red");
} else {
$(elem).css("background-color", "yellow");
}
}
function makeNewPosition() {
var h = $(window).height() - 50;
var w = $(window).width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
function animateDiv() {
var newq = makeNewPosition();
var speed = 2000;
$('.a').animate({ top: newq[0], left: newq[1] }, speed, function() {
animateDiv();
});
}
什麼不準確? –
請檢查JS小提琴...重疊是不正確的......即使鼠標在塊外顏色是紅色的...這不應該是案件 –
所以你想當鼠標不在div中顏色必須是黃色的,如果它的內部div顏色必須是紅色的。那是你的意思嗎? –