constain.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. <div class="demo-info">
  15. <div class="demo-tip icon-tip"></div>
  16. <div>The draggable object can only be moved within its parent container.</div>
  17. </div>
  18. <div style="margin:10px 0;"></div>
  19. <div style="position:relative;overflow:hidden;border:1px solid #ccc;width:500px;height:300px">
  20. <div class="easyui-draggable" data-options="onDrag:onDrag" style="width:100px;height:100px;background:#fafafa;border:1px solid #ccc;">
  21. </div>
  22. </div>
  23. <script>
  24. function onDrag(e){
  25. var d = e.data;
  26. if (d.left < 0){d.left = 0}
  27. if (d.top < 0){d.top = 0}
  28. if (d.left + $(d.target).outerWidth() > $(d.parent).width()){
  29. d.left = $(d.parent).width() - $(d.target).outerWidth();
  30. }
  31. if (d.top + $(d.target).outerHeight() > $(d.parent).height()){
  32. d.top = $(d.parent).height() - $(d.target).outerHeight();
  33. }
  34. }
  35. </script>
  36. </body>
  37. </html>