2011-05-30 48 views
0

這是我的代碼部分需要:雙擊激活綁定鼠標按下/上

$('.toggle_box').each(function(){ 
    var timeout, longtouch; 
    $(this).bind('mousedown', function() { 
     timeout = setTimeout(function() { 
      longtouch = true; 
     }, 1000); 

    $(this).bind('mouseup', function(){ 
     if (!longtouch) { 
      clearTimeout(timeout) 
      var state = $(this).find('.switch').attr('data-state'); 
      if(state == "off"){ 
       $(this).find('.switch').attr('data-state','on'); 
      } 
      else{ 
       $(this).find('.switch').attr('data-state','off'); 
      } 
      var choice = $(this).parent(); 
      ckbmovestate(choice) 
     } 
     else{ 
      alert("3") 
     }; 
    }); 
}) 
}); 

當我點擊一個元素(鼠標按下及以上)它工作正常,但2E時候我必須雙擊元素爲它發射。它看起來像第二次點擊重置綁定,以便3e可以再次使用它。很奇怪。

這裏是一個演示:不再有效...... 它是橙色的複選框:)。(請在Safari /鉻檢查)

感謝您的幫助:)

+0

我需要解除綁定並重新綁定的東西? – cmplieger 2011-05-30 02:03:55

+0

您不應該需要取消/重新綁定。你能用'longtouch'變量來解釋你想要達到什麼嗎?在你提供的代碼中,當你聲明它的時候你不會初始化它,一旦它是真的,你永遠不會將它重新設置爲false。在你的mouseup函數的else分支中,你試圖清除已經執行的超時。 – nnnnnn 2011-05-30 02:05:20

+0

這裏添加了一行:)。 longtouch需要區分長點擊和短點:) – cmplieger 2011-05-30 02:09:30

回答

1

結帳的代碼發佈這裏... http://jsfiddle.net/qv4Ve/2/

你綁定了mousedown事件處理程序中的mouseup事件。你應該不得不彼此獨立地約束他們。

$(this).bind('mousedown', function() { 
    timeout = setTimeout(function() {longtouch = true;}, 1000); 
    console.log("inside mouse down"); 
}); 

$(this).bind('mouseup', function(){ 
    console.log("inside mouse up"); 
    if (longtouch === false) { 
     clearTimeout(timeout) 
     var state = $(this).find('.switch').attr('data-state'); 
     if(state == "off"){ 
      $(this).find('.switch').attr('data-state','on'); 
     } 
     else { 
      $(this).find('.switch').attr('data-state','off') 
     ;} 

     var choice = $(this).parent(); 
     ckbmovestate(choice) 
    } 
    else { 
     alert("is a long touch") 
    }; 
}); 

在上面貼的小提琴代碼工作,你希望它的方式......