【问题标题】:Do something in hover if the right button of the mouse is down (pressed)如果鼠标右键按下(按下),则在悬停时执行某些操作
【发布时间】:2012-04-10 19:53:38
【问题描述】:

当鼠标悬停在图像上并且按住鼠标右键时,我需要缩放图像。类似:

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

我需要帮助。谢谢。

【问题讨论】:

标签: jquery hover mouse


【解决方案1】:

编辑:对于您的以下评论,

谢谢。但是,它需要右键单击图片。它不是 如果您在屏幕的其他位置按住右键,则可以工作 然后传递图像

您需要添加 mouseup 事件并有条件地缩放。请参阅下面的代码,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 - Demo 同时实现了mousedownmouseleave

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

【讨论】:

  • 谢谢。但是,它需要右键单击图片。如果您在屏幕的其他位置按住右键然后传递图像,则它不起作用。
  • @BaharS 更新了处理图像上 mouseup 情况的代码。 DEMO.
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多