-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added jQuery support namespace stratum.
- Loading branch information
Showing
6 changed files
with
219 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,112 @@ | ||
/* | ||
* Stratum.js | ||
* Grid layout library | ||
* MIT License | ||
* by Volodia Zakhovaiko | ||
*/ | ||
(function ($) { | ||
|
||
$(document).ready(function () { | ||
$.fn.stratum = function (options) { | ||
|
||
// Get all grids | ||
let grid = $('[data-grid]'); | ||
let settings = $.extend({ | ||
padding: 15, | ||
columns: 3 | ||
}, options); | ||
|
||
// Count of the columns | ||
let columns = grid.data('grid'); | ||
let grid = $(this); | ||
|
||
// Set default padding gap | ||
let padding = 15; | ||
// Count of the columns and default padding gap | ||
let columns = settings.columns; | ||
let padding = settings.padding; | ||
|
||
// Set default value if user does not set count of the columns | ||
if (columns === undefined || !columns) { | ||
columns = 3; | ||
} | ||
// Iterate and reformat each matched element | ||
return this.each(function () { | ||
|
||
grid.css({ | ||
position: 'relative' | ||
}); | ||
// Initialize stratum function | ||
function init() { | ||
grid.css({ | ||
position: 'relative' | ||
}); | ||
|
||
// Array with item's height and width | ||
let items = []; | ||
// Initial values: left and top position | ||
let top = 0, left = -100 / columns; | ||
// Array with items height | ||
let items = []; | ||
let top = 0; | ||
let left = -100 / columns; | ||
|
||
// | ||
//----- Main cycle: there we will check current coordinates -----// | ||
grid.children().each(function (i) { | ||
// | ||
// Main cycle: there we will check current coordinates | ||
grid.children().each(function (i, obj) { | ||
|
||
$(this).wrap('<div class="grid-item"></div>div>'); | ||
let gridItem = $(obj); | ||
|
||
// Set left position for every nth block | ||
i % columns === 0 ? left = 0 : left += 100 / columns; | ||
if (gridItem.attr('class') !== "grid_item") { | ||
gridItem.wrap('<div class="grid_item"></div>'); | ||
} | ||
|
||
// Set width and left position the first | ||
$(this).parent().css({ | ||
position : 'absolute', | ||
width : 100 / columns + '%', | ||
left: left + '%', | ||
padding: padding | ||
}); | ||
i % columns === 0 ? left = 0 : left += 100 / columns; | ||
|
||
// Set width and others CSS rules | ||
gridItem.parent().css({ | ||
position: 'absolute', | ||
width: 100 / columns + '%', | ||
left: left + '%', | ||
padding: padding | ||
}); | ||
|
||
// Save height value in main array to check top positions | ||
items.push(gridItem.parent().outerHeight(true)); | ||
|
||
// Set top position | ||
if (items[i - columns] === undefined) { | ||
top = 0; | ||
} else { | ||
let itemNumber = i; | ||
top = 0; | ||
while (itemNumber >= columns) { | ||
top += items[itemNumber - columns]; | ||
itemNumber -= columns; | ||
} | ||
} | ||
|
||
// Update and set new position values | ||
gridItem.parent().css({ | ||
top: top | ||
}); | ||
}); | ||
} | ||
|
||
function resize() { | ||
|
||
// Save height value in main array to check top positions | ||
items.push($(this).parent().outerHeight(true)); | ||
|
||
// Set top position | ||
if (items[i - columns] === undefined) { | ||
top = 0; | ||
} else { | ||
let itemNumber = i; | ||
top = 0; | ||
while (itemNumber >= columns) { | ||
top += items[itemNumber - columns]; | ||
itemNumber -= columns; | ||
// TODO: Create setTopPosition function; | ||
|
||
let items = [], top = 0; | ||
|
||
grid.children().each(function (i, obj) { | ||
|
||
let gridItem = $(obj); | ||
|
||
// Save height value in main array to check top positions | ||
items.push(gridItem.outerHeight(true)); | ||
|
||
// Set top position | ||
if (items[i - columns] === undefined) { | ||
top = 0; | ||
} else { | ||
let itemNumber = i; | ||
top = 0; | ||
while (itemNumber >= columns) { | ||
top += items[itemNumber - columns]; | ||
itemNumber -= columns; | ||
} | ||
} | ||
|
||
// Update and set new position values | ||
gridItem.css({ | ||
top: top | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
// Update and set new position values | ||
$(this).parent().css({ | ||
top: top | ||
$(window).on('load', function () { | ||
init(); | ||
}).on('resize', function () { | ||
resize(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
}(jQuery)); |