-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ks.js
74 lines (70 loc) · 2.25 KB
/
jquery.ks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
(function ($) {
$.fn.ks = function (options) {
options = options || {};
options.onRealign = options.onRealign || function(){};
options.target = $(options.target || '.ks-target');
options.spacing = parseInt(options.spacing, 10) || 0;
var context = $(this),
isIE8 = /MSIE\s8/.test(navigator.userAgent);
$(options.target).addClass('__ks_target_selector');
var fill = function () {
var width = parseInt(options.targetWidth) || $(options.target).width();
$(options.target).children().remove();
var children = $(context).children();
for (var i = 0; i < children.length; ) {
var row = $('<div class="ks-row"></div>');
var w = -options.spacing;
for (var j = 0; (w <= width) && (i < children.length); j++, i++) {
var item = $(children[i]).clone();
item.css({
width: $(item).data('width') + 'px',
height: $(item).data('height') + 'px'
});
if (isIE8) {
var selfWidth = item.width(), selfHeight = item.height();
item.find('img').css({
marginTop: "-" + (selfHeight / 2) + "px",
marginLeft: "-" + (selfWidth / 2) + "px"
});
}
row.append(item);
w += parseInt(item.data('width'), 10);
w += options.spacing;
}
if (w > width) {
var numItems = $(row).children().length;
var overflow = w - width;
var pOverflow = overflow / width * 100;
var cut = pOverflow / numItems;
if (cut > 0) {
$(row).children().each(function () {
var iWidth = parseInt($(this).data('width'), 10);
var initialWidth = iWidth / width * 100;
var holdSpace = iWidth / w * 100;
var percentageWidth = (initialWidth - (pOverflow / 100 * holdSpace));
$(this).css({
width: percentageWidth + '%'
});
if (isIE8) {
var selfWidth = width / 100 * percentageWidth;
var selfHeight = $(this).height();
$(this).find('img').css({
marginTop: "-" + (selfHeight / 2) + "px",
marginLeft: "-" + (selfWidth / 2) + "px"
});
}
});
}
}
options.target.append(row);
}
options.onRealign();
};
if (!options.targetWidth) {
$(window).resize(fill).trigger('resize');
} else {
fill();
}
};
})(jQuery);