如何在Backbonejs视图方法中监听mousedown,mousemove和mouseup

时间:2014-01-30 09:40:24

标签: javascript events backbone.js

我希望能够在Backbone视图中拖动div元素。要做到这一点,我需要在Backbone View方法中听这三个事件mouseup,mousedown,mousemove。

    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元素,但我怎么能在slide方法中听这3个事件。

1 个答案:

答案 0 :(得分: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
        }
    },