Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery UI - Draggable Axis</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script> <style> body{font-family:Arial, sans-serif; padding:20px;} .box{width:120px;height:60px;color:#fff;display:flex;align-items:center;justify-content:center;border-radius:6px;cursor:move;margin-bottom:20px;} #boxX{background:#04AA6D;} #boxY{background:#2196f3;} </style> </head> <body> <h3>axis: 'x' (horizontal only)</h3> <div id="boxX" class="box">X axis</div> <h3>axis: 'y' (vertical only)</h3> <div id="boxY" class="box">Y axis</div> <script> $(document).ready(function(){ $("#boxX").draggable({ axis: "x" }); $("#boxY").draggable({ axis: "y" }); }); </script> </body> </html>
Output