snap-to.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI Draggable - Snap to element or grid</title>
  6. <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
  7. <script src="../../jquery-1.10.2.js"></script>
  8. <script src="../../ui/jquery.ui.core.js"></script>
  9. <script src="../../ui/jquery.ui.widget.js"></script>
  10. <script src="../../ui/jquery.ui.mouse.js"></script>
  11. <script src="../../ui/jquery.ui.draggable.js"></script>
  12. <link rel="stylesheet" href="../demos.css">
  13. <style>
  14. .draggable { width: 90px; height: 80px; padding: 5px; float: left; margin: 0 10px 10px 0; font-size: .9em; }
  15. .ui-widget-header p, .ui-widget-content p { margin: 0; }
  16. #snaptarget { height: 140px; }
  17. </style>
  18. <script>
  19. $(function() {
  20. $( "#draggable" ).draggable({ snap: true });
  21. $( "#draggable2" ).draggable({ snap: ".ui-widget-header" });
  22. $( "#draggable3" ).draggable({ snap: ".ui-widget-header", snapMode: "outer" });
  23. $( "#draggable4" ).draggable({ grid: [ 20, 20 ] });
  24. $( "#draggable5" ).draggable({ grid: [ 80, 80 ] });
  25. });
  26. </script>
  27. </head>
  28. <body>
  29. <div id="snaptarget" class="ui-widget-header">
  30. <p>I'm a snap target</p>
  31. </div>
  32. <br style="clear:both" />
  33. <div id="draggable" class="draggable ui-widget-content">
  34. <p>Default (snap: true), snaps to all other draggable elements</p>
  35. </div>
  36. <div id="draggable2" class="draggable ui-widget-content">
  37. <p>I only snap to the big box</p>
  38. </div>
  39. <div id="draggable3" class="draggable ui-widget-content">
  40. <p>I only snap to the outer edges of the big box</p>
  41. </div>
  42. <div id="draggable4" class="draggable ui-widget-content">
  43. <p>I snap to a 20 x 20 grid</p>
  44. </div>
  45. <div id="draggable5" class="draggable ui-widget-content">
  46. <p>I snap to a 80 x 80 grid</p>
  47. </div>
  48. <div class="demo-description">
  49. <p>Snap the draggable to the inner or outer boundaries of a DOM element. Use the <code>snap</code>, <code>snapMode</code> (inner, outer, both), and <code>snapTolerance</code> (distance in pixels the draggable must be from the element when snapping is invoked) options. </p>
  50. <p>Or snap the draggable to a grid. Set the dimensions of grid cells (height and width in pixels) with the <code>grid</code> option.</p>
  51. </div>
  52. </body>
  53. </html>