A Pen by chrisarmstrong on CodePen.
Created
August 10, 2014 22:17
-
-
Save chrisarmstrong/d0d0e704015d39e6e081 to your computer and use it in GitHub Desktop.
A Pen by chrisarmstrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="container"> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// init | |
var i=34; | |
for (i; i > -1; i--) { | |
switch (Math.round(Math.random()*10)) { | |
case 1: | |
$('#container').append('<div class="item w2"></div>'); | |
i = i-1; | |
break; | |
case 2: | |
$('#container').append('<div class="item h2"></div>'); | |
i = i-1; | |
break; | |
case 3: | |
$('#container').append('<div class="item h2 w2"></div>'); | |
i = i-3; | |
break; | |
case 4: | |
$('#container').append('<div class="item h3"></div>'); | |
i = i-2; | |
break; | |
case 5: | |
$('#container').append('<div class="item w3"></div>'); | |
i = i-2; | |
break; | |
case 6: | |
$('#container').append('<div class="item w3 h3"></div>'); | |
i = i-8; | |
break; | |
case 7: | |
$('#container').append('<div class="item w2 h3"></div>'); | |
i = i-5; | |
break; | |
case 8: | |
$('#container').append('<div class="item h2 w3"></div>'); | |
i = i-5; | |
break; | |
default: | |
$('#container').append('<div class="item"></div>'); | |
} | |
if (i<=0) { | |
layout(); | |
} | |
} | |
function layout() { | |
var $container = $('#container'); | |
$container.packery({ | |
itemSelector: '.item', | |
gutter:20, | |
columnWidth: 48, | |
rowHeight: 48 | |
}); | |
$container.find('.item').each( function( i, itemElem ) { | |
// make element draggable with Draggabilly | |
var draggie = new Draggabilly( itemElem ); | |
// bind Draggabilly events to Packery | |
$container.packery( 'bindDraggabillyEvents', draggie ); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.item { | |
width:48px; | |
height:48px; | |
background:white; | |
display:block; | |
} | |
.item:nth-child(3){ | |
background:red; | |
} | |
.item:nth-child(7){ | |
background:blue; | |
} | |
.item:nth-child(9){ | |
background:yellow; | |
} | |
.w2 { | |
width:116px; | |
} | |
.h2 { | |
height:116px; | |
} | |
.h3 { | |
height:184px; | |
} | |
.w3 { | |
width:184px; | |
} | |
body { | |
width:100%; | |
height:100%; | |
background:black; | |
} | |
#container { | |
width:320px; | |
min-height:568px; | |
max-height:568px; | |
overflow:hidden; | |
outline:1px solid white; | |
margin:auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment