I want to capture the cursor coordinates over an image in a modal form when the image is clicked but I’m suck on the javascript. I also want the modal form to open ONLY when the image is clicked. The modal form currently pops up when the user clicks anywhere in the window. Bonus marks for anyone who can help me get the modal form centered in the window as well!
Any insights will be greatly appreciated.
Here is what I have so far:
HTML
<a onmouseover="document.body.style.cursor='crossHair'"
onmouseout="document.body.style.cursor='default'"
onmousemove="getXY(event)">
<img id="Image" src="../Images/TestImage.jpg" alt=""/></a><br/><br/>
X = <input type="text" id="xVal" size="1" readonly="true"/>
Y = <input type="text" id="yVal" size="1" readonly="true"/><br/><br/>
<div id="mask"></div>
<div id="container">
<div id="submitDialog" class="window"><br />
X-coordinate:<asp:Label ID="lblX" runat="server"></asp:Label><br/>
Y-coordinate:<asp:Label ID="lblY" runat="server"></asp:Label><br/>
Javascript
function getXY(e) {
var txtX = document.getElementById("xVal");
var txtY = document.getElementById("yVal");
MouseX = (e).offsetX;
MouseY = (e).offsetY;
txtX.value = MouseX;
txtY.value = MouseY;
}
$(document.getElementById('#Image')).click(function() {
launchWindow('#submitDialog');
$(window).resize(function() {
var box = $('#container .window');
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(document).width();
//Set height and width to mask to fill up the whole screen
$('#mask').css({ 'width': maskWidth, 'height': maskHeight });
//Get the window height and width
var winH = $(document).height();
var winW = $(document).width();
//Set the popup window to center
box.css('top', winH / 2 - winH);
box.css('left', winW / 2 - winW / 2);
});
});
function launchWindow(id) {
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(document).width();
//Get the window height and width
var winH = $(document).height();
var winW = $(document).width();
//Set heigth and width to mask to fill up the whole screen
//and mask position
$('#mask').css({ 'width': maskWidth, 'height': maskHeight });
$('#mask').css({ 'top': 0, 'left': 0});
//transition effect
$('#mask').fadeIn(500);
$('#mask').fadeTo("fast", 0.8);
//Set the popup window to center
$(id).css('top', winH / 2 - winH);
$(id).css('left', winW / 2 - winW / 2);
//transition effect
$(id).fadeIn(500);
//These are not setting the label text :(
$('#lblX').text($('#xVal').val());
$('#lblY').text($('#yVal').val());
}
CSS
#mask
{
position:absolute;
z-index:9000;
background-color:#000;
display:none;
}
#container .window
{
position:relative;
width:300px;
height:490px;
display:none;
z-index:9999;
padding:20px;
padding-bottom: 40px;
background-color: white;
color:black;
}
#container
{
position: relative;
width: 0px;
height: 0px;
}
The solution below addresses all of the issues I was having previously. My fix to get the modal form to be “centered” was a bit of a hack but it suited my purpose well enough. Hopefully this can be useful to someone else out there!
HTML
Javascript