constrain-movement.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI Draggable - Constrain movement</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: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
  15. #draggable, #draggable2 { margin-bottom:20px; }
  16. #draggable { cursor: n-resize; }
  17. #draggable2 { cursor: e-resize; }
  18. #containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; }
  19. h3 { clear: left; }
  20. </style>
  21. <script>
  22. $(function() {
  23. $( "#draggable" ).draggable({ axis: "y" });
  24. $( "#draggable2" ).draggable({ axis: "x" });
  25. $( "#draggable3" ).draggable({ containment: "#containment-wrapper", scroll: false });
  26. $( "#draggable5" ).draggable({ containment: "parent" });
  27. });
  28. </script>
  29. </head>
  30. <body>
  31. <h3>Constrain movement along an axis:</h3>
  32. <div id="draggable" class="draggable ui-widget-content">
  33. <p>I can be dragged only vertically</p>
  34. </div>
  35. <div id="draggable2" class="draggable ui-widget-content">
  36. <p>I can be dragged only horizontally</p>
  37. </div>
  38. <h3>Or to within another DOM element:</h3>
  39. <div id="containment-wrapper">
  40. <div id="draggable3" class="draggable ui-widget-content">
  41. <p>I'm contained within the box</p>
  42. </div>
  43. <div class="draggable ui-widget-content">
  44. <p id="draggable5" class="ui-widget-header">I'm contained within my parent</p>
  45. </div>
  46. </div>
  47. <div class="demo-description">
  48. <p>Constrain the movement of each draggable by defining the boundaries of the draggable area. Set the <code>axis</code> option to limit the draggable's path to the x- or y-axis, or use the <code>containment</code> option to specify a parent DOM element or a jQuery selector, like 'document.'</p>
  49. </div>
  50. </body>
  51. </html>