I am looking to create a unique div that moves in the opposite direction of the mouse cursor within another div called .box
. As the mouse moves, I want the red box to create a subtle parallax effect by slightly moving in the opposite direction. Instead of moving at the same speed as the mouse, I want the box to move just a little bit to achieve the desired effect. Additionally, I would like the box to be centered around the mouse cursor.
I have already implemented a script that allows the red box to follow the mouse cursor, but I am unsure of how to achieve the desired effect described above.
$(document).ready(function(){
$('div.container').on('mousemove',function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
$('div.box').css({'left': x, 'top': y});
});
});