Shift + mouseover with jQuery

前端 未结 4 1259
再見小時候
再見小時候 2021-02-20 13:34

I\'m trying to detect whether the shift key is being pressed while the cursor is moved over a particular element. The function fires, but only after I click on another

4条回答
  •  -上瘾入骨i
    2021-02-20 14:01

    check this on the keypress event:

    $(document).keypress(function (e) {
    
      if(e.shiftKey) {
        pressed = true; // pressed is a global varialbe. Be carefull of the scope
      }
    
    }
    

    then on the keyup:

    $(document).keyup(function(event){
       pressed = false;
    });
    

    then do:

    $("#selector").mouseover(function(e){
        if(pressed) {
            console.log("the shift key is pressed");
        }
    });
    

    or the other way around :

    $("#selector").mouseover(function(e){
        isover = true;
    });
    

    and

       $(document).keypress(function (e) {
    
          if(e.shiftKey) {
            alert("do something")
          }
    
       }
    

提交回复
热议问题