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 ?
0
give a positive ratinggive a negative rating
15 Jul 2020 at 04:43 PM
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');" />

0
give a positive ratinggive a negative rating
31 Aug 2020 at 04:37 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us