JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

HTML

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

CSS

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

JavaScript

$(function() {
    $("#draggable").draggable({
        // Can't use revert, as we animate the original object
        //revert: true,
        
        axis: "y",
        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);
            $this.data('starttop',$this.position().top);
        },
        stop: function(){
            // When dragging stops, revert the draggable to its
            // original starting position.
            var $this = $(this);
            $this.stop().animate({
                top: $this.data('starttop')
            },600,'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
            },600,'easeOutCirc');
        }
    });
});