0

How can i track if user has mouseover a element and rightclicks on the right x and y positions?

 <div style="position:absolute;left:200px;top:200px">Hello</div>

 <script>
   if ( MOUSE == divPosition && RIGHTCLICKS on it) {
       //Trigger a javascript function!
   }
 </script>

I really hope someone can help me.

It should first find the positions where the element is. After that a multi mouselistiner should track if user rightclicks on the specific position of the element. Without just making "oncontextmenu.." It should be done though jQuery or something, but i am stuck.

I have tried this and works fine, but it's only for "mouseover" element. It should be when user RIGHTCLICKS on the right position!

'wierdo' : function(id){
$("#item-" + id).each(function(){
boxX = $(this).offset().left;
boxY = $(this).offset().top;
boxW = $(this).innerWidth();
boxH = $(this).innerHeight();
if ((boxX <= mx) &&
    (boxX + 1000 >= mx) &&
    (boxY <= my) &&
    (boxY + boxH >= my))
{
    // mouse is over it so you can for example trigger a mouseenter event
    alert("Yes....");
    $("#item-" + id).trigger({type: "mousedown",which: 3});

    //$(this).trigger("mouseenter");
}

});

kim larsen
  • 5,111
  • 6
  • 19
  • 17
  • Well, there's no physical way to right click an element without hovering over it, so I don't think you need to check the mouse position. And the event for "right-click" is `contextmenu`. So give the element an `id`, get it with `document.getElementById()`, and look into `addEventListener` – Ian Jun 10 '13 at 05:47
  • It should first find the positions where the element is. After that a multi mouselistiner should track if user rightclicks on the specific position of the element. Without just making "oncontextmenu.." It should be done though jQuery or something, but i am stuck. – kim larsen Jun 10 '13 at 05:57

1 Answers1

0

may be you are looking for this:

Markup:

<div class='div'>Hello</div>

jQuery:

$('.div').on('contextmenu', function (e) { //<--contextmenu is right click event
  e.preventDefault();       //<------prevents the right click
  alert('rightClick customized.'); 
});

Checkout the fiddle here

Jai
  • 74,255
  • 12
  • 74
  • 103