Open In App

jQuery UI Draggable start Event

Last Updated : 23 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

 jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. 

In this article, we are going to understand the jQuery UI Draggable start event.  We can drag any HTML element throughout the page. 

Syntax:

$( ".selector" ).draggable({
    start: function( event, ui ) {}
});

 

CDN Link: First, add jQuery UI scripts needed for your project.

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

Example 1: In this example, a box is set to be draggable. We are using a callback start event, we will define a function that changes the color of the text and background.

HTML




<!doctype html>
<html>
    
<head>
    <title>jQuery UI Draggable start event</title>
    <link rel="stylesheet" href=
"//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
        #draggable_box {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: roboto;
            width: 100px;
            height: 100px;
            background: #ccc;
            border-radius: 10px;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h3>jQuery UI Draggable start event</h3>
  
    <div id="draggable_box">Drag this box.</div>
  
    <script>
        $("#draggable_box").draggable({
            start: function (event, ui) {
                $(this).css("background-color", "green");
                $(this).css("color", "white")
            }
        });
    </script>
</body>
  
</html>


Output:

Example 2: We can set various actions on the element. In this example, we will animate the box, using the start event.

HTML




<!doctype html>
<html>
    
<head>
    <title>jQuery UI Draggable start event</title>
    <link rel="stylesheet" href=
"//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
        #draggable_box {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: roboto;
            width: 100px;
            height: 100px;
            background: #ccc;
            border-radius: 10px;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h3>jQuery UI Draggable start event</h3>
  
    <div id="draggable_box">Drag this box.</div>
  
    <script>
        $("#draggable_box").draggable({
            start: function (event, ui) {
                $(this).animate({
                    width: "200px",
                    height: "200px",
                    backgroundColor: "green",
                }, "slow");
            }
        });
    </script>
</body>
  
</html>


Output:

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads