You can achieve your objective detector 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");
});
TO EXPLAIN:
The variable relX
will contain the pixel distance from the left of your element to the place where the user clicked.
The variable limite
sets the limit of the click, that is, indicates that up to this value both from the right and from the left the click is considered "outside".
In the example it can be seen that the click on the white zone that corresponds to a margin of 15 pixels equal to your image, goes "out". Click on the green color gives "inside".
The jQuery method was used offset()
to translate the coordinates event.pageX
and event.pageY
originated with the mouse click to a pixel position relative to the element.
The
margin
is already ignored for events in the elements, in your case the problem would be in thepadding
, nay?– Kazzkiq
Then again the div and an image with the margin:0 auto only that the hit of the event continues to pick up in the area en white(no content) TT
– Douglas dos Santos
And if you use the image itself to trigger the event while passing the mouse?
– luigibertaco
Or, use the mouseover of all the children of your main div, and not the div itself, in this way becomes even more dynamic.
– luigibertaco
Then luigibertaco the problem is that for example: I have the div that appears in the mouseover the moment the div appears and the mouse is on top the function will be called a second made unnecessarily... but quiet some ifs must solve valel
– Douglas dos Santos
Put the code.
– Manuel Gerardo Pereira
Without examining the code it is difficult to suggest a solution. Take a look at this: http://javascript.info/tutorial/bubbling-and-capturing
– Maujor
Thank you Maujor, very enlightening link, but I solved my problem by eliminating the white space of the div was not the solution I wanted but now the problem is gone, thanks
– Douglas dos Santos
@Douglasdossantos then puts this solution as an answer so that other users can see more clearly and this question leaves the list of unanswered questions.
– Raul Sena Ferreira
@Douglasdossantos you could create two Ivs, one that would contain the white space you need, and the other where would be the content with the hit.
– Hiago Souza