j-ulrich / jquery-simulate-ext

jQuery simulate extended
https://j-ulrich.github.io/jquery-simulate-ext/
Other
146 stars 48 forks source link

how can I simulate drag with a speed function? #40

Closed imdoge closed 6 years ago

imdoge commented 6 years ago

I don't want to drag with the same speed, can I config it? thx

j-ulrich commented 6 years ago

I'm not sure what you mean with "drag with the same speed".

Using either the duration or the stepDelay property of the interpolation option allows to define how fast a drag is simulated (see the documentation of drag) For example:

$(dragElement).simulate('drag-n-drop', {
    dropTarget: dropElement,
    interpolation: {
        stepWidth: 10,
        stepDelay: 50
    }
});

Increasing stepDelay makes the drag slower.

If you want to drag an element with different speeds (for example, slower at the beginning and then faster), then you need to simulate multiple successive drags:

$(dragElement).simulate('drag', {
    dragTarget: firstPointOfDrag,
    interpolation: {
        stepWidth: 10,
        stepDelay: 50 // slower
    },
    callback: function() {
        $(dragElement).simulate('drag', {
            dragTarget: secondPointOfDrag,
            interpolation: {
                stepWidth: 10,
                stepDelay: 25 // faster
            },
            callback: function() {
                $(dragElement).simulate('drag-n-drop', {
                    dropTarget: endPointOfDrag,
                    interpolation: {
                        stepWidth: 10,
                        stepDelay: 75 // slower again
                    }
                });
            }
        });
    }
});

Side note: Of course you can use promises (for example, jQuery.Deferred()) to avoid nesting the callbacks.

Does that answer your question?

imdoge commented 6 years ago

thanks a lot