2014-01-30 37 views
0

我希望能夠在Backbone視圖中拖動div元素。爲此,我需要在Backbone View方法中偵聽這三個事件mouseup,mousedown,mousemove。如何在Backbonejs視圖方法中聆聽mousedown,mousemove和mouseup

events: { 
     "mousedown .status .progress .seek-bar .seek-bar-grip": "slide", 
     "mouseup .status .progress .seek-bar .seek-bar-grip": "slide", 
     "mousemove .status .progress .seek-bar .seek-bar-grip": "slide", 
    }, 

    slide: function(event) { 

     // Code about the drag here 

    }, 

這不起作用,因爲每次其他事件被觸發時,它都會再次調用該方法。我的問題不是如何在JavaScript中拖動div元素,但我怎麼能聽到幻燈片方法中的這3個事件。

回答

0

您可以在滑動功能中使用event.type來區分事件。

var FLAG = 1; 
var currentTarget; 
slide: function(event) {   
     // Code about the drag here 
     if(event.type = "mousedown"){ 
      FLAG = 2; // now handle mouse move 
      currentTarget = event.currentTarget; 
     }else if(event.type = "mousemove"){ 
      if(FLAG == 2){ 
       // use the currentTarget to doSomething() 
      } 
     }else if(event.type = "mouseup"){ 
      FLAG = 1; 
       // drop the target 
     } 
    }, 
+0

感謝您的回答。但是,我不明白如何將event與event.type結合在一起。 用戶按下鼠標左鍵(mousedown) - >我們聽mousemove並根據鼠標位置移動div(mousemove) - >用戶離開左鍵(mouseup) – JeremyW

+0

更新了我的答案,添加了提示邏輯。 –

相關問題