add-remove.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width" />
  6. <title>add/remove cells</title>
  7. <link rel="stylesheet" href="../css/flickity.css" />
  8. <link rel="stylesheet" href="sandbox.css" />
  9. <style>
  10. .cell {
  11. position: relative;
  12. }
  13. .remove-button {
  14. font-size: 20px;
  15. position: absolute;
  16. right: 10px;
  17. top: 10px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h1>add/remove cells</h1>
  23. <div id="demo1" class="demo">
  24. <div class="container variable-width js-flickity">
  25. <div class="cell n1 w3"><b>1</b><button class="remove-button">&times;</button></div>
  26. <div class="cell n2"><b>2</b><button class="remove-button">&times;</button></div>
  27. <div class="cell n3 w3"><b>3</b><button class="remove-button">&times;</button></div>
  28. <div class="cell n4"><b>4</b><button class="remove-button">&times;</button></div>
  29. <div class="cell n5 w2"><b>5</b><button class="remove-button">&times;</button></div>
  30. <div class="cell n6 w2"><b>6</b><button class="remove-button">&times;</button></div>
  31. </div>
  32. <p>
  33. <button class="prepend-button">Prepend</button>
  34. <button class="insert-button">Insert</button>
  35. <button class="append-button">Append</button>
  36. </p>
  37. </div>
  38. <h2>freeScroll</h2>
  39. <div id="demo2" class="demo">
  40. <div class="container variable-width js-flickity"
  41. data-flickity-options='{ "freeScroll": true }'>
  42. <div class="cell n6"><b>1</b><button class="remove-button">&times;</button></div>
  43. <div class="cell n5 w2"><b>2</b><button class="remove-button">&times;</button></div>
  44. <div class="cell n4"><b>3</b><button class="remove-button">&times;</button></div>
  45. <div class="cell n3 w3"><b>4</b><button class="remove-button">&times;</button></div>
  46. <div class="cell n2"><b>5</b><button class="remove-button">&times;</button></div>
  47. </div>
  48. <p>
  49. <button class="prepend-button">Prepend</button>
  50. <button class="insert-button">Insert</button>
  51. <button class="append-button">Append</button>
  52. </p>
  53. </div>
  54. <h2>wrapAround</h2>
  55. <div id="demo3" class="demo">
  56. <div class="container variable-width js-flickity"
  57. data-flickity-options='{ "wrapAround": true }'>
  58. <div class="cell n6"><b>1</b><button class="remove-button">&times;</button></div>
  59. <div class="cell n5 w2"><b>2</b><button class="remove-button">&times;</button></div>
  60. <div class="cell n4"><b>3</b><button class="remove-button">&times;</button></div>
  61. <div class="cell n3 w3"><b>4</b><button class="remove-button">&times;</button></div>
  62. <div class="cell n2"><b>5</b><button class="remove-button">&times;</button></div>
  63. <div class="cell n6 w2"><b>6</b><button class="remove-button">&times;</button></div>
  64. </div>
  65. <p>
  66. <button class="prepend-button">Prepend</button>
  67. <button class="insert-button">Insert</button>
  68. <button class="append-button">Append</button>
  69. </p>
  70. </div>
  71. <h2>wrapAround, freeScroll</h2>
  72. <div id="demo4" class="demo">
  73. <div class="container variable-width js-flickity"
  74. data-flickity-options='{ "wrapAround": true, "freeScroll": true }'>
  75. <div class="cell n6"><b>1</b><button class="remove-button">&times;</button></div>
  76. <div class="cell n5 w2"><b>2</b><button class="remove-button">&times;</button></div>
  77. <div class="cell n4"><b>3</b><button class="remove-button">&times;</button></div>
  78. <div class="cell n3 w3"><b>4</b><button class="remove-button">&times;</button></div>
  79. <div class="cell n2"><b>5</b><button class="remove-button">&times;</button></div>
  80. <div class="cell n6 w2"><b>6</b><button class="remove-button">&times;</button></div>
  81. </div>
  82. <p>
  83. <button class="prepend-button">Prepend</button>
  84. <button class="insert-button">Insert</button>
  85. <button class="append-button">Append</button>
  86. </p>
  87. </div>
  88. <h2>contain</h2>
  89. <div id="demo5" class="demo">
  90. <div class="container variable-width js-flickity"
  91. data-flickity-options='{ "contain": true }'>
  92. <div class="cell n1 w3"><b>1</b><button class="remove-button">&times;</button></div>
  93. <div class="cell n2"><b>2</b><button class="remove-button">&times;</button></div>
  94. <div class="cell n3 w3"><b>3</b><button class="remove-button">&times;</button></div>
  95. <div class="cell n4"><b>4</b><button class="remove-button">&times;</button></div>
  96. <div class="cell n5 w2"><b>5</b><button class="remove-button">&times;</button></div>
  97. <div class="cell n6 w2"><b>6</b><button class="remove-button">&times;</button></div>
  98. </div>
  99. <p>
  100. <button class="prepend-button">Prepend</button>
  101. <button class="insert-button">Insert</button>
  102. <button class="append-button">Append</button>
  103. </p>
  104. </div>
  105. <h2>reposition</h2>
  106. <div id="reposition">
  107. <div class="container variable-width">
  108. <div class="cell w2"><b>1</b></div>
  109. <div class="cell"><b>2</b></div>
  110. <div class="cell w3"><b>3</b></div>
  111. <div class="cell"><b>4</b></div>
  112. <div class="cell"><b>5</b></div>
  113. <div class="cell w2"><b>6</b></div>
  114. <div class="cell"><b>7</b></div>
  115. <div class="cell w2"><b>8</b></div>
  116. <div class="cell"><b>9</b></div>
  117. </div>
  118. </div>
  119. <h2>prepend with single #492</h2>
  120. <div id="prepend-single">
  121. <div class="container variable-width">
  122. <div class="cell n1"><b>1</b><button class="remove-button">&times;</button></div>
  123. </div>
  124. <p>
  125. <button class="prepend-button">Prepend</button>
  126. </p>
  127. </div>
  128. <script src="../bower_components/get-size/get-size.js"></script>
  129. <script src="../bower_components/desandro-matches-selector/matches-selector.js"></script>
  130. <script src="../bower_components/ev-emitter/ev-emitter.js"></script>
  131. <script src="../bower_components/unipointer/unipointer.js"></script>
  132. <script src="../bower_components/unidragger/unidragger.js"></script>
  133. <script src="../bower_components/fizzy-ui-utils/utils.js"></script>
  134. <script src="../js/cell.js"></script>
  135. <script src="../js/slide.js"></script>
  136. <script src="../js/animate.js"></script>
  137. <script src="../js/flickity.js"></script>
  138. <script src="../js/drag.js"></script>
  139. <script src="../js/prev-next-button.js"></script>
  140. <script src="../js/page-dots.js"></script>
  141. <script src="../js/player.js"></script>
  142. <script src="../js/add-remove-cell.js"></script>
  143. <script>
  144. var cellCount = 6;
  145. function randInt( num ) {
  146. return Math.floor( Math.random() * num );
  147. }
  148. function getRandom( ary ) {
  149. var index = Math.floor( Math.random() * ary.length );
  150. return ary[ index ];
  151. }
  152. var widthClasses = [ '', 'w2', 'w3' ];
  153. var nClasses = 'n1 n2 n3 n4 n5 n6'.split(' ');
  154. function makeCell() {
  155. var cell = document.createElement('div');
  156. cell.className = 'cell ' + getRandom( widthClasses ) + ' ' + getRandom( nClasses );
  157. var b = document.createElement('b');
  158. b.textContent = ++cellCount;
  159. cell.appendChild( b );
  160. var removeButton = document.createElement('button');
  161. removeButton.className = 'remove-button';
  162. removeButton.textContent = '×';
  163. cell.appendChild( removeButton );
  164. return cell;
  165. }
  166. function getParent( elem, selector ) {
  167. while ( elem != document.body ) {
  168. elem = elem.parentNode;
  169. if ( matchesSelector( elem, selector ) ) {
  170. return elem;
  171. }
  172. }
  173. }
  174. function onContainerClick( event ) {
  175. if ( event.target.className != 'remove-button' ) {
  176. return;
  177. }
  178. //
  179. var cellElement = getParent( event.target, '.cell' );
  180. var container = getParent( event.target, '.container' );
  181. var flkty = Flickity.data( container );
  182. flkty.remove( cellElement );
  183. }
  184. function getParentFlickity( elem ) {
  185. var demo = getParent( elem, '.demo' );
  186. return Flickity.data( demo.querySelector('.container') );
  187. }
  188. function onPrependClick( event ) {
  189. var flkty = getParentFlickity( event.target );
  190. flkty.prepend([ makeCell(), makeCell(), makeCell() ]);
  191. }
  192. function onInsertClick( event ) {
  193. var flkty = getParentFlickity( event.target );
  194. flkty.insert([ makeCell(), makeCell(), makeCell() ], 3 );
  195. }
  196. function onAppendClick( event ) {
  197. var flkty = getParentFlickity( event.target );
  198. flkty.append([ makeCell(), makeCell(), makeCell() ]);
  199. }
  200. // init
  201. var demos = document.querySelectorAll('.demo');
  202. for ( var i=0; i < demos.length; i++ ) {
  203. var demo = demos[i];
  204. demo.querySelector('.container').addEventListener( 'click', onContainerClick );
  205. demo.querySelector('.prepend-button').addEventListener( 'click', onPrependClick );
  206. demo.querySelector('.insert-button').addEventListener( 'click', onInsertClick );
  207. demo.querySelector('.append-button').addEventListener( 'click', onAppendClick );
  208. }
  209. // ----- reposition ----- //
  210. (function() {
  211. var flkty = new Flickity('#reposition .container');
  212. flkty.on( 'staticClick', function( event, pointer, cellElem ) {
  213. cellElem.classList.toggle('w3');
  214. flkty.reposition();
  215. });
  216. window.mapTargets = function() {
  217. return flkty.slides.map( function( slide ) {
  218. return ~~slide.target;
  219. });
  220. }
  221. })();
  222. // ----- prepend single, #492 ----- //
  223. ( function() {
  224. var demo = document.querySelector('#prepend-single');
  225. var flkty = new Flickity( demo.querySelector('.container') );
  226. demo.querySelector('.prepend-button').onclick = function() {
  227. flkty.prepend( makeCell() );
  228. };
  229. })();
  230. </script>
  231. </body>
  232. </html>