//The amount the image will move
var HOP = 1;
var TIME = 5; //milliseconds it will take before the image moves again
//initiating standard mouse coords
var mouse = {
x: 0,
y: 0
};
//adding a listener on the document for mousemove to update mouse position
document.addEventListener('mousemove', function (e) {
mouse.x = e.clientX || e.pageX;
mouse.y = e.clientY || e.pageY
}, false);
//getting the image
var $img = document.getElementById('celine');
var si_id = setInterval(function () {
//every 50 miliseconds we do this
//getting img position (sylte.left/top will not return the position)
var tempL = $img.offsetLeft;
var tempT = $img.offsetTop;
//testing to see where the mouse is relative to the image
if (mouse.x > tempL) {
console.log('true: ' + mouse.x + ' : ' + tempL);
$img.style.left = tempL + HOP + 'px';
} else {
console.log('false: ' + mouse.x + ' : ' + tempL);
$img.style.left = tempL - HOP + 'px';
}
if (mouse.y > $img.offsetTop) {
console.log('in');
$img.style.top = tempT + HOP + 'px';
} else {
console.log('in');
$img.style.top = tempT - HOP + 'px';
}
}, TIME);
var button = document.getElementById('stop');
button.onclick = function(){
clearInterval(si_id);
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.