asimryu

jquery ui drag&drop

May 20th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.02 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.     #images > div, #boards > div {float:left;width:200px;height:200px;border:1px solid #000;margin:5px;}
  10.     #images div img {width:200px;height:200px;}
  11.     #boards {clear:both;}
  12.     #boards > div {font-size:2em;line-height:200px;text-align:center;}
  13. </style>
  14. </head>
  15. <body>
  16. <div id="images">
  17.     <div><img src="http://lorempixel.com/200/200/food" id="food"></div>
  18.     <div><img src="http://lorempixel.com/200/200/city" id="city"></div>
  19.     <div><img src="http://lorempixel.com/200/200/sports" id="sports"></div>
  20.     <div><img src="http://lorempixel.com/200/200/animals" id="animals"></div>
  21. </div>
  22. <div id="boards">
  23.     <div title="sports">sports</div>
  24.     <div title="food">food</div>
  25.     <div title="animals">animals</div>
  26.     <div title="city">city</div>
  27. </div>
  28. <script>
  29.     $(function(){
  30.         $("#images div img").draggable({
  31.             start: function(event,ui) {
  32.                 $(this).draggable( "option", "revert", true );
  33.                 $("#images div img").css("zIndex",10);
  34.                 $(this).css("zIndex",100);
  35.             }
  36.         });
  37.         $("#boards div").droppable({
  38.             drop: function(event,ui) {
  39.                 var droptitle = $(this).attr("title");
  40.                 var drophtml = $(this).html();
  41.                 var dragid = ui.draggable.attr("id");
  42.                 if( dragid == droptitle ) {
  43.                     ui.draggable.draggable( "option", "revert", false );
  44.                     var droppableOffset = $(this).offset();
  45.                     var x = droppableOffset.left + 1;
  46.                     var y = droppableOffset.top + 1;
  47.                     ui.draggable.offset({ top: y, left: x });
  48.                 }
  49.             }
  50.         });
  51.     });
  52.     $(document).ready(function(){
  53.         $("#images div").sort(function(){
  54.             return Math.random()*10 > 5 ? 1 : -1;
  55.         }).each(function(){
  56.             $(this).appendTo( $(this).parent() );    
  57.         });
  58.         $("#boards div").sort(function(){
  59.             return Math.random()*10 > 5 ? 1 : -1;
  60.         }).each(function(){
  61.             $(this).appendTo( $(this).parent() );    
  62.         });
  63.     });
  64. </script>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment