Skip to content

Commit

Permalink
Added getTopPosition function and removed repeated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed Jun 23, 2018
1 parent 2acb0d8 commit dfd83c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 79 deletions.
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<script src="../dist/stratum.min.js"></script>
<script>
$('[data-grid]').stratum({
// padding: 20,
// columns: 3
padding: 5,
columns: 2
});
</script>

Expand Down
64 changes: 27 additions & 37 deletions dist/stratum.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@
}, options);

var grid = $(this);
var heights = [];

// Count of the columns and default padding gap
var columns = settings.columns;
var padding = settings.padding;

// Default positions
var top = 0;
var left = -100 / columns;

// Iterate and reformat each matched element
return this.each(function () {

Expand All @@ -115,11 +120,6 @@
position: 'relative'
});

// Array with items height
var items = [];
var top = 0;
var left = -100 / columns;

//
// Main cycle: there we will check current coordinates
grid.children().each(function (i, obj) {
Expand All @@ -141,57 +141,47 @@
});

// 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 {
var itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}
heights.push(gridItem.parent().outerHeight(true));

// Update and set new position values
gridItem.parent().css({
top: top
top: getTopPosition(gridItem, i)
});
});
}

function resize() {
var items = [],
top = 0;
heights = [];
top = 0;

grid.children().each(function (i, obj) {

var 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 {
var itemNumber = i;
top = 0;
while (itemNumber >= columns) {
top += items[itemNumber - columns];
itemNumber -= columns;
}
}
heights.push(gridItem.outerHeight(true));

// Update and set new position values
gridItem.css({
top: top
top: getTopPosition(gridItem, i)
});
});
}

//
// Get current top position value
function getTopPosition(obj, index) {
if (heights[index - columns] === undefined) {
top = 0;
} else {
var itemNumber = index;
top = 0;
while (itemNumber >= columns) {
top += heights[itemNumber - columns];
itemNumber -= columns;
}
}
return top;
}

$(window).on('load', function () {
init();
}).on('resize', function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/stratum.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 27 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
}, options);

let grid = $(this);
let heights = [];

// Count of the columns and default padding gap
let columns = settings.columns;
let padding = settings.padding;

// Default positions
let top = 0;
let left = -100 / columns;

// Iterate and reformat each matched element
return this.each(function () {

Expand All @@ -22,11 +27,6 @@
position: 'relative'
});

// 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, obj) {
Expand All @@ -48,59 +48,47 @@
});

// 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;
}
}
heights.push(gridItem.parent().outerHeight(true));

// Update and set new position values
gridItem.parent().css({
top: top
top: getTopPosition(gridItem, i)
});
});
}

function resize() {

// TODO: Create setTopPosition function;

let items = [], top = 0;
heights = [];
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;
}
}
heights.push(gridItem.outerHeight(true));

// Update and set new position values
gridItem.css({
top: top
top: getTopPosition(gridItem, i)
});
});
}

//
// Get current top position value
function getTopPosition(obj, index) {
if (heights[index - columns] === undefined) {
top = 0;
} else {
let itemNumber = index;
top = 0;
while (itemNumber >= columns) {
top += heights[itemNumber - columns];
itemNumber -= columns;
}
}
return top;
}

$(window).on('load', function () {
init();
}).on('resize', function () {
Expand Down

0 comments on commit dfd83c4

Please sign in to comment.