First you need to wrap your image into a relative
positioned DIV
. Then add mousedown
event listener for the image. In this handler store the click- coordinates and add mouseup
listener for the image. In this mouseUp()
store the mouseup-coordinates, remove all previous handlers and create a new DIV
to the document. Then add click
and mouseover
event listeners for the new DIV
, style the position absolute
, actual position and size you can calculate from stored coordinates. Finally append newly created DIV
to the image wrapper.
Instead of using a DIV
as a hotspot , you can include an empty MAP
element in your document, and then create new AREA
(s) to this MAP
.
EDIT
I'm really not sure what you need, but...
Creating a DIV
in mouseUpHandler()
:
var clickableDiv = imgWrapper.appendChild(document.createElement('DIV'));
clickableDiv.style.position = 'absolute';
clickableDiv.style.top = calculatedY;
clickableDiv.style.left = calculatedX;
clickableDiv.style.width = calculatedWidth;
clickableDiv.style.height = calculatedHeight;
clickableDiv.style.backGround = '#fff';
clickableDiv.style.opacity = '0.5';
clickableDiv.addEventListener('click', doSomethingOnClick, false);
You're free to style the clickableDiv
as you wish, but backGround
and opacity
(can be also 0) are needed to make the DIV
clickable. doSomethingOnClick
is a function name, which handles clicks on the clickableDiv
.