Skip to content

Instantly share code, notes, and snippets.

@jakobmattsson
Created September 8, 2010 15:29
Show Gist options
  • Save jakobmattsson/570295 to your computer and use it in GitHub Desktop.
Save jakobmattsson/570295 to your computer and use it in GitHub Desktop.
Run IE, Run!
$(function() {
var minDist = 100;
var target = document.getElementById('volatile');
if (target == null)
return;
target.style.position = "relative";
target.style.left = target.style.left || "0px";
target.style.top = target.style.top || "0px";
var getPosition = function(obj) {
var xlocation = parseInt(obj.offsetLeft);
var ylocation = parseInt(obj.offsetTop);
while (obj.parent) {
obj = obj.parent;
xlocation = xlocation + parseInt(obj.offsetLeft);
ylocation = ylocation + parseInt(obj.offsetTop);
}
return { x: xlocation, y: ylocation, width: obj.offsetWidth, height: obj.offsetHeight };
};
document.onmousemove = function(e) {
var p = getPosition(target);
var px = p.x + p.width / 2;
var py = p.y + p.height / 2;
var mx = e.pageX;
var my = e.pageY;
var dist = Math.sqrt((px - mx) * (px - mx) + (py - my) * (py - my));
if (dist < minDist) {
var factor = (minDist - dist) / minDist;
var dx = parseInt((px - mx) * factor);
var dy = parseInt((py - my) * factor);
target.style.left = (parseInt(target.style.left) + dx) + "px";
target.style.top = (parseInt(target.style.top) + dy) + "px";
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment