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 do I use jQuery to detect window resize events?
- How do I use jQuery to zip files?
- How do I use jQuery to zoom in or out on an element?
- How can I get the y position of an element using jQuery?
- How can I use jQuery to select elements with an XPath expression?
- How do I use the jQuery window load function?
- How can I use jQuery in WordPress?
- How can I use jQuery to access data attributes?
- How do I use the jQuery when function?
See more codes...