如果鼠标右键未按下(按下),请执行某些操作

问题描述:

当鼠标悬停在图像上并且鼠标右键保持按下状态时,我需要缩放图像。如:如果鼠标右键未按下(按下),请执行某些操作

$('img').hover(
    function(){ 
     if (the-right-mouse-button-is-pressed){ 
     $(this).animate({ 
     width: $(this).width() *2, 
     height: $(this).height()*2, 
     }, 500); 
     } 
    }, 
}); 

我需要帮助。谢谢。

+0

搜索的律”有点.... http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery, http://stackoverflow.com/questions/706655/bind-event-to-right-mouse-click,http://stackoverflow.com/questions/4007482/jquery-detect-if-middle-or-right-mouse-按钮被点击 - 如果这样做 - 这 – elclanrs 2012-04-10 19:57:37

+0

我看到了。这与我的问题不同。谢谢你的方式。 – 2012-04-12 02:43:50

编辑:为了您下面的评论,

感谢。但是,它需要右键单击图片。它不 工作,如果你在屏幕 其他地方保持正确的按钮下来,然后通过图像

您需要添加鼠标松开事件和缩放条件。请参见下面的代码,DEMO

var hasExpanded = false; 
$('img').on('mousedown mouseup', function(e) { 
    if (e.which == 3) { 
      if (!hasExpanded) { 
      $(this).animate({ 
       width: $(this).width() * 2, 
       height: $(this).height() * 2, 
      }, 500); 
     } 
     hasExpanded = true; 
    } 
}).mouseleave(function(e) { 
    if (hasExpanded == true) { 
    $(this).animate({ 
     width: $(this).width()/2, 
     height: $(this).height()/2, 
    }, 500); 
    hasExpanded = false; 
} 
}); 

你需要什么无法通过悬停实现。将在mouseeneter上触发悬停,该操作只会被调用一次,并且无法记录稍后发生的mousedown事件。

您需要实施mousedown处理程序。见下,

DEMO - 演示既有mousedownmouseleave实施。

$('img').mousedown(function(e) { 
    if (e.which == 3) { 
     $(this).animate({ 
      width: $(this).width() * 2, 
      height: $(this).height() * 2, 
     }, 500); 
    } 
}); 
+0

谢谢。但是,它需要右键单击图片。如果您在屏幕上的其他位置保持右键并传递图像,它将不起作用。 – 2012-04-11 01:35:35

+0

@BaharS处理图像上的mouseup案例的更新代码。 [** DEMO **](http://jsfiddle.net/PhM8P/3/)。 – 2012-04-12 21:16:29