-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.verticalAjust.js
89 lines (72 loc) · 2.01 KB
/
jquery.verticalAjust.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(function( $ ){
var options = {
elms: []
};
var methods = {
attach: function (str) {
//add an list case
options.elms[options.elms.length] = str;
},
ajust: function () {
if (options.elms.length) {
for (var i = 0; i < options.elms.length; i++) {
methods._ajust(options.elms[i]);
}
}
},
_ajust: function (str) {
//detect col count
var last_left = 0;
var cols = 0;
var htsCol = [];
$(str).each(function(index) {
var left = $(this).offset().left;
if ($(this).attr('data-om') === undefined) {
$(this).attr('data-om', parseInt($(this).css('marginTop')));
}
if (!cols) {
if (last_left > left)
cols = index;
else
htsCol[index] = 0;
}
last_left = left;
});
console.log(cols);
var hts = [];
var htsRow = 0;
var innerWidth = $(window).innerWidth();
$(str).each(function(index) {
var height = $(this).height();
hts[index] = height;
var delta = 0;
if (index >= cols) {
if (index % cols == 0) {
var mx = 0;
for(var i = index-cols; i < index; i++) mx = Math.max(mx, hts[i]);
htsRow += mx;
}
var delta = htsRow - htsCol[index % cols] - parseInt($(this).attr('data-om')) * Math.floor(index / cols);
}
hts[index] = height - delta;
htsCol[index % cols] += height;
$(this).css({marginTop: -delta});
});
}
};
$.fn.verticalAjust = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.attach.apply( this, arguments );
} else {
$.error( 'Метод с именем ' + method + ' не существует для jQuery.tooltip' );
}
};
$( window ).bind('resize', function() {
$.fn.verticalAjust('ajust');
});
$(function() {
$.fn.verticalAjust('ajust');
});
})( jQuery );