var box1 = document.getElementsByClassName("box1")[0]; var box2 = document.getElementsByClassName("box2")[0]; var box2Img = box2.getElementsByTagName("img")[0]; var a = document.getElementsByClassName("a")[0];
box1.onmousemove = function(event) { var box1X = event.clientX; var box1Y = event.clientY; a.style.display = "block"; var maxX = box1.offsetWidth - a.offsetWidth / 2; var minX = a.offsetWidth / 2; var maxY = box1.offsetHeight - a.offsetHeight / 2; var minY = a.offsetHeight / 2; if (box1X >= maxX) { box1X = maxX; if (box1Y >= maxY) { box1Y = maxY; } } if (box1X <= minX) { box1X = minX; if (box1Y <= minY) { box1Y = minY; } } if (box1Y >= maxY) { box1Y = maxY; if (box1X >= maxX) { box1X = maxX; } } if (box1Y <= minY) { box1Y = minY; if (box1X <= minX) { box1X = minX; } } var box2X = -((box2Img.offsetWidth / box1.offsetWidth) * box1X); var box2Y = -((box2Img.offsetHeight / box1.offsetHeight) * box1Y); a.style.top = box1Y - a.offsetHeight / 2 + "px"; a.style.left = box1X - a.offsetWidth / 2 + "px"; box2Img.style.marginTop = box2Y + a.offsetHeight + "px"; box2Img.style.marginLeft = box2X + a.offsetWidth + "px"; };
box1.onmouseout = function() { a.style.display = "none"; };
|