add-items.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>add items</title>
  6. <style>
  7. * {
  8. -webkit-box-sizing: border-box;
  9. box-sizing: border-box;
  10. }
  11. .grid {
  12. background: #DDD;
  13. }
  14. /* clearfix */
  15. .grid:after {
  16. display: block;
  17. content: '';
  18. clear: both;
  19. }
  20. .grid-sizer,
  21. .grid-item {
  22. width: 25%;
  23. }
  24. .grid-item {
  25. border: 1px solid;
  26. background: #09F;
  27. height: 100px;
  28. }
  29. .grid-item--width2 { width: 50%; }
  30. .grid-item--height2 { height: 160px; }
  31. .grid-item--height3 { height: 220px; }
  32. </style>
  33. </head>
  34. <body>
  35. <h1>add items</h1>
  36. <p>
  37. <button class="prepend-button">Prepend button</button>
  38. <button class="append-button">Append button</button>
  39. </p>
  40. <div class="grid">
  41. <div class="grid-sizer"></div>
  42. <div class="grid-item grid-item--width2"></div>
  43. <div class="grid-item grid-item--height2"></div>
  44. </div>
  45. <script src="../bower_components/eventEmitter/EventEmitter.js"></script>
  46. <script src="../bower_components/eventie/eventie.js"></script>
  47. <script src="../bower_components/doc-ready/doc-ready.js"></script>
  48. <script src="../bower_components/get-style-property/get-style-property.js"></script>
  49. <script src="../bower_components/get-size/get-size.js"></script>
  50. <script src="../bower_components/jquery-bridget/jquery.bridget.js"></script>
  51. <script src="../bower_components/matches-selector/matches-selector.js"></script>
  52. <script src="../bower_components/fizzy-ui-utils/utils.js"></script>
  53. <script src="../bower_components/outlayer/item.js"></script>
  54. <script src="../bower_components/outlayer/outlayer.js"></script>
  55. <script src="../masonry.js"></script>
  56. <script>
  57. function getItemElement() {
  58. var elem = document.createElement('div');
  59. var wRand = Math.random();
  60. var hRand = Math.random();
  61. var widthClass = wRand > 0.8 ? 'grid-item--width3' :
  62. wRand > 0.6 ? 'grid-item--width2' : '';
  63. var heightClass = hRand > 0.8 ? 'grid-item--height3' :
  64. hRand > 0.5 ? 'grid-item--height2' : '';
  65. elem.className = 'grid-item ' + widthClass + ' ' + heightClass;
  66. return elem;
  67. };
  68. docReady( function() {
  69. var msnry = window.msnry = new Masonry( '.grid', {
  70. columnWidth: '.grid-sizer',
  71. percentPosition: true
  72. });
  73. document.querySelector('.prepend-button').addEventListener( 'click', function() {
  74. var itemElem = getItemElement();
  75. msnry.element.insertBefore( itemElem, msnry.element.firstChild );
  76. msnry.prepended( itemElem );
  77. });
  78. document.querySelector('.append-button').addEventListener( 'click', function() {
  79. var itemElem = getItemElement();
  80. msnry.element.appendChild( itemElem );
  81. msnry.appended( itemElem );
  82. });
  83. });
  84. </script>
  85. </body>
  86. </html>