Open In App

jQuery UI Draggable drag Event

Improve
Improve
Like Article
Like
Save
Share
Report

jQuery UI is a web-based technology and consists of various GUI widgets, visual effects, and themes. These features can be implemented using the jQuery JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add widgets easily.

In this article, we will be using the jQuery UI Draggable drag Event to trigger while the mouse is moved during the dragging, immediately before the current move happens.

Syntax:

We need to initialize the Draggable widget with the drag a callback function:

$( ".selector" ).draggable({
 drag: function( event, ui ) {}
});
  • Bind an event listener to the drag event:

    $( ".selector" ).on( "drag", function( event, ui ) {} );
  • Setting the Constrain movement via ui.position:

    $( ".selector" ).draggable({
     drag: function( event, ui ) {
       // Keep the left edge of the element
       // at least 100 pixels from the container
       ui.position.left = Math.min( 100, ui.position.left );
     }
    });

Parameters: These are the following parameters that are accepted.  

  • event: This event is triggered when the draggable item is dragged.
  • ui: This parameter is of type object with below-given options.
    • helper: This parameter is the jQuery object representing the sorted helper.
    • offset: This parameter is the current absolute position of the helper object which is represented as { top, left }.
    • position: This parameter is the current position of the helper object which is represented as { top, left }.

CDN Link: Below are some jQuery Mobile scripts that will be needed for your project so add these to your project.

<link rel=”stylesheet” href=”https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<script src= “https://code.jquery.com/jquery-1.12.4.js”></script>
<script src= “https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>

Example: This example describes the uses of the jQuery UI Draggable Drag Event.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
  
    <style>
        .dragg {
            width: 90px;
            height: 40px;
            border: 1px solid black;
            background-color: blue;
        }
  
        .dropp2 {
            width: 200px;
            height: 40px;
            border: 1px solid black;
            float: center;
            background-color: green;
        }
    </style>
  
    <script>
        $(function () {
            $(".dropp2").droppable();
            $(".dragg").draggable({
                drag: function (event, ui) {
                    $(".res").html("<b>Draggable Drag.</b><br>");
                }
            });
        });
    </script>
</head>
  
<body>
    <center>
        <h1 style="color:green;">GeeksforGeeks</h1>
  
        <h3>jQuery UI Draggable Drag Event</h3>
  
        <div class="dragg">
            <p>Drag</p>
  
        </div>
        <br>
        <div class="dropp2">
            <p>Drop here</p>
  
        </div>
        <br>
        <div class="res"></div>
    </center>
</body>
  
</html>


Output:

jQuery UI Draggable drag Event

jQuery UI Draggable Drag Event

Reference: https://api.jqueryui.com/draggable/#event-drag



Last Updated : 25 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads