Skip to content

Instantly share code, notes, and snippets.

@bcho
Forked from anonymous/index.html
Created January 25, 2013 11:21
Show Gist options
  • Save bcho/4633635 to your computer and use it in GitHub Desktop.
Save bcho/4633635 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>metro</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container grid-home grid-wrap">
<div class="grid-block-wrap grid-block-span1" data-grid-row="1" data-grid-col="1"></div>
<div class="grid-block-wrap grid-block-span1" data-grid-row="1" data-grid-col="1"></div>
<div class="grid-block-wrap grid-block-span1" data-grid-row="1" data-grid-col="1"></div>
<div class="grid-block-wrap grid-block-span2" data-grid-row="2" data-grid-col="2"></div>
<div class="grid-block-wrap grid-block-span1" data-grid-row="1" data-grid-col="1"></div>
</div>
</body>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="metro.js" type="text/javascript"></script>
</html>
(function() {
'use strict';
var c = function(home, cols) {
var children = $('.grid-block-wrap', home),
placed = [],
i, j, k, flag,
width, height, grid_width;
for (i = 0;i < cols;i++) {
placed[i] = [];
for (j = 0;j < children.length;j++) {
placed[i][j] = 0;
}
}
var validate = function(a, b, w, h) {
var i, j;
if ((w + a > cols) || (h + b > children.length)) {
return false;
}
for (i = 0;i < w;i++) {
for (j = 0;j < h;j++) {
if (placed[a + i][b + j] !== 0) {
return false;
}
}
}
return true;
};
var map = function(a, b, w, h) {
var i, j;
for (i = 0;i < w;i++) {
for (j = 0;j < h;j++) {
placed[a + i][b + j] = 1;
}
}
};
grid_width = home.width($(window).width() * 0.8).width() / cols;
home.css('position', 'relative');
children.css('position', 'absolute');
for (i = 0;i < children.length;i++) {
width = parseInt($(children[i]).attr('data-grid-row'), 10);
height = parseInt($(children[i]).attr('data-grid-col'), 10);
for (j = 0, flag = true;j < children.length && flag;j++) {
for (k = 0;k < cols && flag;k++) {
if (validate(k, j, width, height)) {
$(children[i]).css('top', j * grid_width)
.css('left', k * grid_width)
.height(height * grid_width)
.width(width * grid_width);
map(k, j, width, height);
flag = false;
break;
}
}
}
}
};
var home = $('.grid-home');
var cols = 4;
$(window).bind('resize', function() {
c(home, cols);
});
c(home, cols);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment