Here's the continuation of Jcubed's answer:
Essentially, it calculates the position of the mouse and checks it for the position of objects, and then sees if the distance between them is less than 25 pixels every 100 ms.
If the object is less than 25px (half the width of the object), it is inside it and will add a hover class. If it is larger, it will remove the hover class.
Codepen
(function() {
$("#restart").on("click", function() {
tl.restart();
})
var mX, mY, distance, mousePosition,
$distance = $('#distance span'),
$element = $('#redBox');
var tl = new TimelineLite()
tl.to($element, 15, {x:550});
setInterval(function() {
function calculateDistance(elem, mouseX, mouseY) {
return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.offset().left+(elem.width()/2)), 2) + Math.pow(mouseY - (elem.offset().top+(elem.height()/2)), 2)));
}
$(document).mousemove(function(e) {
mX = e.pageX;
mY = e.pageY;
mousePosition = (mX, mY);
$distance.text(distance);
});
distance = calculateDistance($element, mX, mY);
if(distance < 25){
console.log("Mouse Has Entered");
$($element).addClass('hovered');
}
else{
$($element).removeClass('hovered');
}
}, 100);
})();