constrain.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Constrain Draggable - jQuery EasyUI Demo</title>
  6. <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
  7. <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
  8. <link rel="stylesheet" type="text/css" href="../demo.css">
  9. <script type="text/javascript" src="../../jquery.min.js"></script>
  10. <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
  11. </head>
  12. <body>
  13. <h2>Constrain Draggable</h2>
  14. <p>The draggable object can only be moved within its parent container.</p>
  15. <div style="margin:20px 0;"></div>
  16. <div class="easyui-panel" style="position:relative;overflow:hidden;width:500px;height:300px">
  17. <div class="easyui-draggable" data-options="onDrag:onDrag" style="width:100px;height:100px;background:#fafafa;border:1px solid #ccc;">
  18. </div>
  19. </div>
  20. <script>
  21. function onDrag(e){
  22. var d = e.data;
  23. if (d.left < 0){d.left = 0}
  24. if (d.top < 0){d.top = 0}
  25. if (d.left + $(d.target).outerWidth() > $(d.parent).width()){
  26. d.left = $(d.parent).width() - $(d.target).outerWidth();
  27. }
  28. if (d.top + $(d.target).outerHeight() > $(d.parent).height()){
  29. d.top = $(d.parent).height() - $(d.target).outerHeight();
  30. }
  31. }
  32. </script>
  33. </body>
  34. </html>