You can achieve your goal regardless of where the click occurred in relation to your element:
Example in JSFiddle
$("#meuElemento").click(function(e){
var meuOffset = $(this).offset(),
relX = e.pageX - meuOffset.left,
relY = e.pageY - meuOffset.top,
largura = $(this).width(),
limite = 15;
// Onde foi o clique?
if (relX>limite && relX<(largura-limite))
$('p span').html("Dentro");
else
$('p span').html("Fora");
});
Explain:
The variable relX
will contain the distance in pixels from the left of your element to the place where the user clicked.
The variable limite
defines the click limit, that is, it indicates that up to this value both counting from the right and counting from the left the click is considered "out".
In the example it can be seen that clicking on the white area that corresponds to a margin of 15 pixels equal to your image, gives "out". Clicking on the green color gives "in".
ThejQuery offset()
method was used to translate the coordinates event.pageX
and event.pageY
originated with the click from the mouse to a pixel position relative to the element.