asimryu

drag & drop color box

May 20th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.52 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. $(document).ready(
  24.     function(){
  25.         $(".drag").each(
  26.             function(){
  27.                 var bg = $(this).attr("id");
  28.                 $(this).css("background-color",bg);
  29.             }
  30.         );
  31.     }
  32. );
  33. $(function(){
  34.     $(".drag").draggable({
  35.         start:function(){
  36.             $(".drag").css("z-index",10);
  37.             $(this).css("z-index", 100);
  38.             $(this).draggable( "option", "revert", true );
  39.         },
  40.         revert:true
  41.     });
  42. });
  43. $(function(){
  44.     $(".drop").droppable({
  45.         drop:function(event, ui){
  46.             var droptitle = $(this).attr("title");
  47.             var dragid = ui.draggable.attr("id");  
  48.             if(droptitle == dragid) {
  49.                 ui.draggable.draggable( "option", "revert", false );
  50.                 var droppableOffset = $(this).offset();
  51.                 var x = droppableOffset.left + 1;
  52.                 var y = droppableOffset.top + 1;
  53.                 ui.draggable.offset({ top: y, left: x });  
  54.             }
  55.         }
  56.     });
  57. });
  58. </script>
  59. </body>
  60. </html>
Advertisement
Add Comment
Please, Sign In to add comment