2014-03-04 2 views
0

두 개의 마우스 이벤트를 함수 (mousedown 및 moucemove)에 바인딩하고 싶습니다. 하지만 두 이벤트가 발생하는 경우에만 함수를 실행하고 싶습니다.여러 이벤트를 바인딩하고 모든 이벤트가 시작되면 함수를 실행하십시오! JS, Jquery

이 함수에 각 이벤트를 바인딩 :

$("#someid").bind("mousedown mousemove", function (event) { someFunction(); }); 

나는이 작업을 수행 할 수 있습니다 (내가 원하는 것은 아니에요) 및 작동 :

$("#someid").bind("mousedown", function (event) { 
    someFunction(); 
    $("#someid").bind("mousemove", function (event) { 
     someFunction(); 
    }); 
}); 

$("#someid").bind("mouseup", function (event) { 
    $("#someid").unbind("mousemove"); 
}); 

이 더 나은, 더 빨리 이걸 할 방법 ???

답변

2

바인딩. 이동하는 동안 왼쪽 마우스 버튼을 누르면 event.which는 1이됩니다.

$(document).on('mousemove', function(e) { 
    if (e.which == 1) { 
     //do some stuff 
    } 
}); 
+0

그래,이게 훨씬 낫네! 감사 – Razor

0

mousemove 마우스 버튼을 눌렀을 때만 처리기를 사용 하시겠습니까? 그렇다면 어떤 종류의 부울 값 플래그을 사용하는 것이 좋습니다. 당신은 mousedownmouseup 이벤트 상태를 전환 : 단지 MouseMove 이벤트 이벤트에

var flag = false; 
$('#someid') 
    .on('mousedown', function(e) { 
     flag = true; 
    }) 
    .on('mouseup', function(e) { 
     flag = false; 
    }) 
    .on('mousemove', function(e) { 
     if (!flag) { 
      return false; 
     } 

     // else... do your stuff 
    }); 
관련 문제