snap.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. <div class="demo-info">
  15. <div class="demo-tip icon-tip"></div>
  16. <div>This sample shows how to snap a draggable object to a 20x20 grid.</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. d.left = repair(d.left);
  27. d.top = repair(d.top);
  28. function repair(v){
  29. var r = parseInt(v/20)*20;
  30. if (Math.abs(v % 20) > 10){
  31. r += v > 0 ? 20 : -20;
  32. }
  33. return r;
  34. }
  35. }
  36. </script>
  37. </body>
  38. </html>