VOOZH about

URL: https://www.geeksforgeeks.org/jquery/jquery-ui-draggable-containment-option/

⇱ jQuery UI Draggable containment Option - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

jQuery UI Draggable containment Option

Last Updated : 25 Jan, 2022

jQuery UI consists of GUI widgets, visual effects, and themes implemented using the jQuery JavaScript Library. jQuery UI is great for building UI interfaces for the webpages. It can be used to build highly interactive web applications or can be used to add widgets easily. 

In this article, we are going to learn the jQuery UI Draggable containment Option. The containment option is used to set that the draggable items will be contained in the specified container.

Syntax: The containment option takes an element as string, jquery, element and is initialized as follows:

$(".drag").draggable({
 containment: "#gfg_container",
});
  • Get the containment option:

    var containmentOpt = $(".drag")
     .draggable( "option", "containment");
  • Set the containment option:

    $(".drag").draggable( "option", 
     "containment", "#gfg_container");

CDN Links: Use the following CDNs for the jQuery UI project.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

Example: In the following example, the items are contained inside the container with the id "gfg_container".

Output:

👁 jQuery UI Draggable containment Option
jQuery UI Draggable containment Option

Reference: https://api.jqueryui.com/draggable/#option-containment

Comment

Explore