jqueryHow do I use jQuery UI draggable?
To use jQuery UI draggable, you first need to include the jQuery UI library in your HTML page. You can do this by adding the following code to the
section of your HTML page:<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
Next, you need to add the draggable()
function to the element you want to make draggable. For example, if you want to make a <div>
element draggable, you can use the following code:
$("#myDiv").draggable();
You can also customize the draggable behavior by passing in an options object. For example, the following code will make the element snap to a grid with 10px increments:
$("#myDiv").draggable({
grid: [ 10, 10 ]
});
You can also specify functions to be called when the draggable element is dragged, started, stopped, or reverted. For example, the following code will display an alert when the draggable element is started:
$("#myDiv").draggable({
start: function(event, ui) {
alert("Drag started!");
}
});
You can find more information about the draggable()
function and its options in the jQuery UI documentation.
Code explanation
**
- Include jQuery UI library in HTML page:
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
- Add
draggable()
function to element you want to make draggable:$("#myDiv").draggable();
- Customize draggable behavior by passing in an options object:
$("#myDiv").draggable({ grid: [ 10, 10 ] });
- Specify functions to be called when draggable element is dragged, started, stopped, or reverted:
$("#myDiv").draggable({ start: function(event, ui) { alert("Drag started!"); } });
## Helpful links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How can I get the y position of an element using jQuery?
- How do I use jQuery with Yarn?
- How do I prevent XSS attacks when using jQuery?
- How do I use jQuery to detect window resize events?
- How do I use jQuery UI Dialog?
- How do I uncheck a checkbox using jQuery?
- How do I remove an attribute using jQuery?
- How can I convert a jQuery JSON object to an array?
- How do I use jQuery to zip files?
See more codes...