Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var foundDrags = false;
var pArray = new Array();
var imgArray = new Array();
function findDrags() {
pArray = document.getElementsByTagName("p");
imgArray = document.getElementsByTagName("img");
foundDrags = true;
}
var gap = 20;
var pBorder = 5;
var pWidth = 400; // Padding + Width
function mouseMove(event) {
if(!event) {
event = window.event // Required for IE
}
mousex = event.clientX;
mousey = event.clientY;
if(window.innerWidth) {
windowx = window.innerWidth;
}
else {
windowx = document.body.offsetWidth; // Fallback for IE
}
if(foundDrags) {
for(i=0; i<pArray.length; i=i+1) {
if(pArray[i].className == "drag") {
if((mousex + gap + pBorder + pWidth + pBorder + gap) < windowx) {
pArray[i].style.left = (mousex + gap) + "px";
}
else {
pArray[i].style.left = (mousex - pBorder - pWidth - pBorder - gap) + "px";
}
pArray[i].style.top = mousey + "px";
}
}
for(i=0; i<imgArray.length; i=i+1) {
if(imgArray[i].className == "drag") {
if((mousex + gap + gap + imgArray[i].width) < windowx) {
imgArray[i].style.left = (mousex + gap) + "px";
}
else {
imgArray[i].style.left = (mousex - imgArray[i].width - gap) + "px";
}
imgArray[i].style.top = mousey + "px";
}
}
}
else {
findDrags();
}
}
document.onmousemove = mouseMove;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.