javascript - jQuery RightClick Handler Plugin -
i looking way distinguish different click types in mouse events jquery. ended small plugin seems work. feedback on it.
the main difficulty emulate dom event "click" or "mousedown" le right mouse button.
if(jquery) (function(){ $.extend($.fn, { rightclick: function(handler) { $(this).each( function() { $(this).on("rightclick", function(e,event) { handler(event); }); }); return $(this); }, rightmousedown: function(handler) { $(this).each( function() { $(this).on("rightmousedown",function(e,event) { handler(event); }); }); return $(this); }, rightmouseup: function(handler) { $(this).each( function() { $(this).on("rightmouseup",function(e,event) { handler(event); }); }); return $(this); }, leftclick: function(handler) { $(this).each( function() { $(this).on("leftclick", function(e,event) { handler(event); }); }); return $(this); }, leftmousedown: function(handler) { $(this).each( function() { $(this).on("leftmousedown",function(e,event) { handler(event); }); }); return $(this); }, leftmouseup: function(handler) { $(this).each( function() { $(this).on("leftmouseup",function(e,event) { handler(event); }); }); return $(this); }, nocontext: function() { $(this).each( function() { $(this)[0].oncontextmenu = function() { return false; } }); return $(this); } }); $(document).on({ click:function(e){ $(e.target).trigger("leftclick",e); e.stoppropagation(); }, mousedown:function(e){ if(e.button == 0){ $(e.target).trigger("leftmousedown",e); } if(e.button == 2){ $(e.target).trigger("rightmousedown",e); } e.stoppropagation(); }, mouseup:function(e){ if(e.button == 0){ $(e.target).trigger("leftmouseup",e); } if(e.button == 2){ $(e.target).trigger("rightmouseup",e); } e.stoppropagation(); }, contextmenu:function(e){ $(e.target).trigger("rightclick",e); e.stoppropagation(); return false; } },"*"); })(jquery);
try this:
$(document).ready(function(){ document.oncontextmenu = function() {return false;}; $(document).mousedown(function(e){ if( e.button == 2 ) { //2 right click, 3 mouse wheel click alert('right mouse button!'); return false; } return true; }); });
Comments
Post a Comment