javascript - Determine the left offset of a click using jQuery -


Say you have 200px wide image. Is there a way to determine how far you are from the left side of the image you clicked on? For example, if you clicked in the dead center you would get 100. I tried to use ui.position.left like something but did not get it to work. Any ideas?

First, get the X position of the image. After this, use event information to get the X position of the click event.

Once you have it twice, it is a simple math to get results:

  $ ('#yourImg'). Click (function (e) {var imageLeft = $ (this) .offset (). Left clickLeft = e.pageX; var howFarFromLeft = clickLeft - imageLeft;});  

Comments