在图片上移动时用JavaScript显示坐标
代码如下,保存成.htm即可
目前不支持FF,但支持其它浏览器
<img src="http://a.lakequincy.com/img/633722034484875829.gif" alt="" onclick="imageOnClick(this);"
onmousemove="imageOnMouseMove(this);" />
<br />
<span id="msg"></span>
<script type="text/javascript">
function imageOnClick(elementRef) {
var posObject = getEventLocation(event);
alert('Src: ' + elementRef.src + ' Mouse click at x: ' + posObject.x + ' y: ' + posObject.y);
}
function imageOnMouseMove(elementRef) {
var posObject = getEventLocation(event);
document.getElementById("msg").innerText = 'Src: ' + elementRef.src + ' Mouse click at x: ' + posObject.x + ' y: ' + posObject.y;
}
function getEventLocation(eventRef) {
var positionX = 0;
var positionY = 0;
if (eventRef.pageX) {
positionX = eventRef.pageX;
positionY = eventRef.pageY;
}
else if (window.event) {
positionX = eventRef.clientX + document.body.scrollLeft;
positionY = eventRef.clientY + document.body.scrollTop;
}
else if (eventRef.clientX) {
positionX = eventRef.clientX;
positionY = eventRef.clientY;
}
return { x: positionX, y: positionY };
}
</script>
onmousemove="imageOnMouseMove(this);" />
<br />
<span id="msg"></span>
<script type="text/javascript">
function imageOnClick(elementRef) {
var posObject = getEventLocation(event);
alert('Src: ' + elementRef.src + ' Mouse click at x: ' + posObject.x + ' y: ' + posObject.y);
}
function imageOnMouseMove(elementRef) {
var posObject = getEventLocation(event);
document.getElementById("msg").innerText = 'Src: ' + elementRef.src + ' Mouse click at x: ' + posObject.x + ' y: ' + posObject.y;
}
function getEventLocation(eventRef) {
var positionX = 0;
var positionY = 0;
if (eventRef.pageX) {
positionX = eventRef.pageX;
positionY = eventRef.pageY;
}
else if (window.event) {
positionX = eventRef.clientX + document.body.scrollLeft;
positionY = eventRef.clientY + document.body.scrollTop;
}
else if (eventRef.clientX) {
positionX = eventRef.clientX;
positionY = eventRef.clientY;
}
return { x: positionX, y: positionY };
}
</script>