asimryu

drag and drop 20140526-1

May 25th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.33 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>drag & drop</title>
  6. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  7. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  8. <style>
  9. div { width:200px; height:200px; margin:5px; border:1px solid #000; float:left;}
  10. .drop { font-size:2em; text-align:center; line-height:200px;}
  11. .clear { clear:both; }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="red" class="drag"></div>
  16. <div id="green" class="drag"></div>
  17. <div id="blue" class="drag"></div>
  18. <p class="clear"></p>
  19. <div title="red" class="drop">red color</div>
  20. <div title="green" class="drop">green color</div>
  21. <div title="blue" class="drop">blue color</div>
  22. <script>
  23. $(".drag").each(
  24.     function(){
  25.       $(this).css("background-color",$(this).attr("id"));
  26.     }
  27. );
  28. $(".drag").draggable({
  29.  revert:true,
  30.  start:function(){
  31.   $(".drag").css("z-index",10);
  32.   $(this).css("z-index",100);
  33.   $(this).draggable( "option", "revert", true );
  34.  }
  35. });
  36. $(".drop").droppable({
  37.  drop:function(event, ui){
  38.   var mytitle = $(this).attr("title");
  39.   var dragid = ui.draggable.attr("id");
  40.   if( mytitle ==dragid ) {
  41.     ui.draggable.draggable( "option", "revert", false );
  42.     var xy = $(this).offset();
  43.     var x = xy.left + 1;
  44.     var y = xy.top + 1;
  45.     ui.draggable.offset({ top: y, left: x });
  46.   }
  47.  }
  48. });
  49. </script>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment