Trigger touchmove event drag and drop

1.9k Views Asked by At

Hi I need to trigger a touchmove event manually and pull an element out of the countainer on a mobile device (phonegap, jquery-mobile)

$(elem).bind('touchstart', function(event) {
        event.preventDefault();                  
        var target = event.target;
        target = $(this);

        //this for example just changes the css position obviously
        target.css("margin-top", "50px"); 
});


elem.trigger('touchstart');

Is there a way to setup a event manually and trigger the object with that event like that?

var event = $.Event( "touchstart", { pageX:200, pageY:200 } );

I am using this for drag and drop, the author already mentioned there is no way to do this by interact js and recommended:

- call your drag and drop listeners and give them event objects that you create or - trigger simulated pointer events so that interact sees them as a drag by the user.

any idea?

1

There are 1 best solutions below

1
On

Can't you declare the function outside the bind() and call it directly?

$(elem).bind('touchstart', handler);
function handler(event) {
    event.preventDefault();                  
    var target = $(event.target);

    //Do stuff with pageX and pageY
    target.css("margin-top", "50px"); 
}

handler({pageX:200, pageY:200, target:elem, preventDefault:function(){}});