JSFiddle - React, Tailwind, and code Playground

by denniswaltermartinez

HTML

<div id="draggable" class="ui-widget-content">
    <p>Revert the original</p>
</div>

CSS

#draggable {
    position: absolute;
    width: 100px;
    height: 100px;
    padding: 0.5em;
    float: left;
    margin: 0 10px 10px 0;
    background-color: red;
    border: 2px solid gray;
}

JavaScript

$(function() {
    $("#draggable").draggable({
        // Can't use revert, as we animate the original object
        //revert: true,
        
        helper: function(){
            // Create an invisible div as the helper. It will move and
            // follow the cursor as usual.
            return $('<div></div>').css('opacity',0);
        },
        create: function(){
            // When the draggable is created, save its starting
            // position into a data attribute, so we know where we
            // need to revert to.
            var $this = $(this),
                pos = $this.position();
            $this.data({
                startTop: pos.top,
                startLeft: pos.left
            });
        },
        stop: function(){
            // When dragging stops, revert the draggable to its
            // original starting position.
            var $this = $(this),
                data = $this.data();
            $this.stop().animate({
                top: data.startTop,
                left: data.startLeft
            },1000,'easeOutCirc');
        },
        drag: function(event, ui){
            // During dragging, animate the original object to
            // follow the invisible helper with custom easing.
            $(this).stop().animate({
                top: ui.helper.position().top,
                left: ui.helper.position().left
            },1000,'easeOutCirc');
        }
    });
});