简体   繁体   中英

How to listen to mousedown, mousemove and mouseup in a Backbonejs view method

I'd like to be able to drag a div element inside a Backbone view. To do that, I would need to listen to these three events mouseup, mousedown, mousemove inside a Backbone View method.

    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

    },

This won't work because each time an other event is fired, it will call the method again. My question is not how to drag a div element in javascript but how could I listen to these 3 events in the slide method.

You can distinguish between the events using the event.type in you slide function.

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
        }
    },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM