Get TOP and LEFT position of image element - Javascript
How can I get the position of image element from the top and left side by Javascript function ? Would you please provide an example that work ?
Hi,
To get the top and left position of image or other HTML element, you can use the solution below. It is based on offsetTop and offsetLeft functions. The position is checked by using ID of element, so with the function you can get the position of multiple elements.
<script type="text/javascript">
function getPosition( elementID ) {
var e = document.getElementById(elementID);
if ( e.offsetParent ) {
var top = e.offsetTop;
var left = e.offsetLeft;
alert('Element position from top is: '+top+'px. Element position from left is:'+left+'px.');
}
}
</script>
<img src="abc.png" alt="image abc.png" id="photo" style="position: absolute; top: 50%; left: 50%;" onMouseOver="javascript: getPosition('photo');" />