sandbox.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Draggabilly</title>
  7. <style>
  8. .box {
  9. width: 100px;
  10. height: 100px;
  11. background: red;
  12. }
  13. #ex1 {
  14. position: relative;
  15. left: 200px;
  16. top: 50px;
  17. }
  18. #ex2 {
  19. background: blue;
  20. }
  21. #has-handles {
  22. position: relative;
  23. left: 240px;
  24. top: 100px;
  25. background: orange;
  26. }
  27. .handle {
  28. background: #666;
  29. position: absolute;
  30. width: 40px;
  31. height: 40px;
  32. }
  33. #has-handles .handle2 {
  34. right: 10px;
  35. bottom: 10px;
  36. }
  37. .is-dragging {
  38. opacity: 0.5;
  39. }
  40. .container {
  41. border: 1px solid;
  42. position: relative;
  43. }
  44. .container:after {
  45. content: '';
  46. display: block;
  47. clear: both;
  48. }
  49. #container1 {
  50. width: 150px;
  51. }
  52. #container1 .box {
  53. float: left;
  54. margin: 5px;
  55. width: 38px;
  56. height: 38px;
  57. border: 1px solid;
  58. background: orange;
  59. }
  60. #kitties {
  61. background: #CCC;
  62. width: 140px;
  63. padding: 10px;
  64. }
  65. #grid-container {
  66. width: 340px;
  67. height: 210px;
  68. position: absolute;
  69. left: 400px;
  70. top: 300px;
  71. border: 1px solid;
  72. }
  73. #grid-container .draggie {
  74. position: relative;
  75. left: 30px;
  76. top: 30px;
  77. width: 80px;
  78. height: 80px;
  79. background: #CCC;
  80. }
  81. .axised {
  82. position: absolute;
  83. width: 80px;
  84. height: 60px;
  85. background: #F09;
  86. left: 400px;
  87. }
  88. #axis-x { top: 160px; }
  89. #axis-y { top: 230px; }
  90. </style>
  91. </head>
  92. <body>
  93. <h1>Draggabilly</h1>
  94. <p>Press [B] to enable/disable the blue draggabilly</p>
  95. <div id="ex1" class="box">
  96. <div class="handle"></div>
  97. </div>
  98. <div id="ex2" class="box"></div>
  99. <div id="has-handles" class="box">
  100. <div class="handle handle1"></div>
  101. <div class="handle handle2"></div>
  102. </div>
  103. <div id="container1" class="container">
  104. <div class="box"></div>
  105. <div class="box"></div>
  106. <div class="box"></div>
  107. <div class="box"></div>
  108. <div class="box"></div>
  109. <div class="box"></div>
  110. <div class="box"></div>
  111. <div class="box"></div>
  112. <div class="box"></div>
  113. </div>
  114. <img id="kitten" src="https://picsum.photos/100/100" />
  115. <div id="kitties">
  116. <img src="https://picsum.photos/40/60" />
  117. <img src="https://picsum.photos/60/40" />
  118. </div>
  119. <div id="grid-container">
  120. <div class="draggie"></div>
  121. </div>
  122. <div id="axis-x" class="axised">axis: x</div>
  123. <div id="axis-y" class="axised">axis: y</div>
  124. <div id="got-inputs" class="box">
  125. <input />
  126. <input type="checkbox" />
  127. </div>
  128. <script src="../node_modules/ev-emitter/ev-emitter.js"></script>
  129. <script src="../node_modules/get-size/get-size.js"></script>
  130. <script src="../node_modules/unipointer/unipointer.js"></script>
  131. <script src="../node_modules/unidragger/unidragger.js"></script>
  132. <script src="../draggabilly.js"></script>
  133. <script>
  134. window.onload = function() {
  135. var ex1 = document.getElementById('ex1');
  136. var drag1 = new Draggabilly( ex1 );
  137. var ex2 = document.getElementById('ex2');
  138. var drag2 = window.drag2 = new Draggabilly( ex2 );
  139. drag2.on( 'dragStart', function( event, pointer ) {
  140. console.log('dragStart', this.position.x, this.position.y, event.type, pointer.pageX, pointer.pageY );
  141. });
  142. drag2.on( 'staticClick', function( event, pointer ) {
  143. console.log( 'staticClick', event.type );
  144. });
  145. drag2.on( 'dragMove', function( event, pointer ) {
  146. console.log('dragMove', this.position.x, this.position.y, event.type, pointer.pageX, pointer.pageY );
  147. });
  148. drag2.on( 'dragEnd', function( event, pointer ) {
  149. console.log( 'dragEnd', this.position.x, this.position.y, event && event.type, pointer && pointer.pageX, pointer && pointer.pageY );
  150. });
  151. var handleDraggie = new Draggabilly( document.querySelector('#has-handles'), {
  152. handle: '.handle'
  153. });
  154. var container1Draggers = [];
  155. var dragger;
  156. var elems = document.querySelectorAll('#container1 .box');
  157. function handleDraggerEvent( event, pointer ) {
  158. console.log( event.type, this.position.x, this.position.y );
  159. }
  160. for ( var i=0, len = elems.length; i < len; i++ ) {
  161. dragger = new Draggabilly( elems[i], {
  162. containment: true
  163. });
  164. dragger.on( 'dragStart', handleDraggerEvent );
  165. dragger.on( 'dragEnd', handleDraggerEvent );
  166. }
  167. // toggle drag2 enable/disable on [B] keypress
  168. document.addEventListener( 'keyup', function( event ) {
  169. var code = event.keyCode || event.which;
  170. // console.log( code );
  171. switch ( code ) {
  172. case 66 : // B
  173. drag2[ drag2.isEnabled ? 'disable' : 'enable' ]();
  174. break;
  175. }
  176. });
  177. ( function() {
  178. var elem = document.querySelector('#kitten');
  179. new Draggabilly( elem );
  180. })();
  181. ( function() {
  182. var elem = document.querySelector('#kitties');
  183. new Draggabilly( elem );
  184. })();
  185. ( function() {
  186. new Draggabilly( '#grid-container .draggie', {
  187. containment: true,
  188. grid: [ 50, 50 ]
  189. // axis: 'y'
  190. });
  191. })();
  192. new Draggabilly( '#axis-x', { axis: 'x' });
  193. new Draggabilly( '#axis-y', { axis: 'y' });
  194. var inputDraggie = new Draggabilly('#got-inputs');
  195. // inputDraggie.disable();
  196. };
  197. </script>
  198. </body>
  199. </html>