Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

HTML5 + get image location inside container

Is there a way to loop through all the images inside a container and get their location, the X and Y of each image?

I want to get the coordinates from a click of a button, the images are draggable, so once the button is clicked i want to see this info.
0
dkilby
Asked:
dkilby
1 Solution
 
Tom BeckCommented:
By way of example, you could add a <ol> and a button to your page:

<input type="button" id="printPositions" value="Print Image Positions" />
<ol id="positions"></ol>

Then use jquery to print out the positions of each image on the page. This would be X and Y coordinates relative to the document edges. You could also make it relative to a container's edges.
$(document).ready(function(){
	$("#printPositions").bind("click", function(){
		var listItem, liText;
		$("img").each(function(i){
			var imgPos = $(this).offset();
			listItem = document.createElement("li");
			liText = document.createTextNode("X=" + imgPos.left + " Y=" + imgPos.top);
			listItem.appendChild(liText);
			$("#positions").append(listItem);
		});
	});
});
</script>

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now