I've done some research on how to get a div/graphic to follow the cursor - Resource Here - I am trying to create this effect for multiple divs where each div has it's own random speed where some elements are lagging behind more than others. I have created a JS Fiddle to show the current progress, you can see it kind of works to some extend. But I am hoping to achieve a more dramatic effect than what I currently have.
Code HTML
<div class="container">
<div class="following blue"></div>
<div class="following red"></div>
<div class="following yellow"></div>
<div class="following orange"></div>
<div class="following green"></div>
<div class="following purple"></div>
<div class="following pink"></div>
</div>
Code JS
var mouseX = 0,
mouseY = 0,
limitX = 400 - 15,
limitY = 550 - 15;
$(window).mousemove(function(e) {
// with the math subtractnig the boundary
mouseX = Math.min(e.pageX, limitX);
mouseY = Math.min(e.pageY, limitY);
});
// cache the selector
var followers = $(".following");
var x_pixels = 0,
y_pixels = 0;
var loop = setInterval(function() {
// Loop through each follower to move and have random speeds
followers.each(function() {
// Set a max Number to allow for the randomIntFromInterval
// function to work
var max = followers.length * 15;
var min = followers.length;
x_pixels += (mouseX - x_pixels) / randomIntFromInterval(min, max);
y_pixels += (mouseY - y_pixels) / randomIntFromInterval(min, max);
$(this).css({
left: x_pixels,
top: y_pixels
});
});
}, 40);
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
Any advice on how to do this is greatly appreciated. Thanks!