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 - Droppable Tolerance</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;} #wrap{display:flex;gap:24px;align-items:flex-start;} #source{width:110px;height:70px;background:#04AA6D;color:#fff;border-radius:6px;display:flex;align-items:center;justify-content:center;cursor:move;text-align:center;} #target{width:180px;height:120px;border:2px dashed #999;border-radius:6px;background:#fafafa;display:flex;align-items:center;justify-content:center;text-align:center;color:#666;} select{margin-bottom:16px;padding:4px;} </style> </head> <body> <h3>tolerance modes</h3> <label>tolerance: <select id="toleranceSelect"> <option value="intersect">intersect (default)</option> <option value="fit">fit</option> <option value="pointer">pointer</option> <option value="touch">touch</option> </select> </label> <div id="wrap"> <div id="source">Drag me</div> <div id="target">Drop here</div> </div> <script> $(document).ready(function(){ $("#source").draggable(); function initDroppable(tolerance){ $("#target").droppable("destroy").droppable({ tolerance: tolerance, over: function(){ $(this).css("background", "#eaf6ff"); }, out: function(){ $(this).css("background", "#fafafa"); }, drop: function(event, ui){ $(this).css("background", "#e8f9ef").text("Dropped with tolerance: " + tolerance); } }); } initDroppable("intersect"); $("#toleranceSelect").on("change", function(){ initDroppable($(this).val()); $("#target").text("Drop here"); }); }); </script> </body> </html>
Output