2011-05-30 53 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") 
    }; 
}); 

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