snap.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Snap 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>Snap Draggable</h2>
  14. <p>This sample shows how to snap a draggable object to a 20x20 grid.</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. d.left = repair(d.left);
  24. d.top = repair(d.top);
  25. function repair(v){
  26. var r = parseInt(v/20)*20;
  27. if (Math.abs(v % 20) > 10){
  28. r += v > 0 ? 20 : -20;
  29. }
  30. return r;
  31. }
  32. }
  33. </script>
  34. </body>
  35. </html>