-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvanilla.equalize.js
More file actions
120 lines (109 loc) · 2.67 KB
/
Copy pathvanilla.equalize.js
File metadata and controls
120 lines (109 loc) · 2.67 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @author denim2x <https://github.com/denim2x>
*
* This file is a fork of jQuery.equalize for Vanilla JS
*/
/*global equalize*/
(function (exports, augment, $) {
'use strict'
augment(Array, ['forEach'])
/**
* This function is (almost) exactly the same
* as in jQuery.equalize (modulo a few tweaks)
*/
exports.equalize = function (group, equalize) {
group = group || '.group'
equalize = equalize || '.equalize'
$(group).each(function () {
var highestBox = 0
$(equalize, this).
css('height', 'auto').
each(function () {
if ($(this).innerHeight() > highestBox) {
highestBox = $(this).innerHeight()
}
}).
innerHeight(highestBox)
})
}
// When the page has loaded
$(window).on('DOMContentLoaded', function () {
var resizeTimeout
equalize() // Run the function
// And every time the window is resized
$(window).on('resize', function () {
if (resizeTimeout) return
// Will execute at a rate of 15fps
resizeTimeout = setTimeout(function() {
resizeTimeout = null
equalize() // Run again
}, 66)
})
})
})(window,
/**
* Augment Class with some of its prototype
*/
function augment (Class, keys) {
'use strict'
var slice = Array.prototype.slice
keys.forEach(function (key) {
var method = Class.prototype[key]
Class[key] = function () {
var args = slice.call(arguments, 1)
method.apply(arguments[0], args)
}
})
}, window.jQuery ||
/**
* Here comes a teeny-tiny jQuery shim (just because
* it seemed more convenient to have one handy instead
* of having to fiddle with the raw thing).
* PS: It could also be moved in a separate script and
* included before this one.
*/
function jQuery (sel, ctx) {
'use strict'
switch (typeof sel) {
case 'string':
ctx = ctx || document
sel = ctx.querySelectorAll(sel)
return {
css: function (key, val) {
Array.forEach(sel, function each (item) {
item.style[key] = val
})
return this
},
each: function (callback) {
Array.forEach(sel, function each (item, index) {
callback.call(item, index, item)
})
return this
},
innerHeight: function (val) {
Array.forEach(sel, function each (item) {
item.style.height = val + 'px'
})
return this
},
}
case 'object':
if (sel == null) {
throw 'Bad usage of $ - the first argument is null'
}
return {
innerHeight: function () {
return sel.clientHeight
},
on: function (event, handler) {
sel.addEventListener(event, handler)
return this
},
}
case 'undefined':
throw 'Bad usage of $ - nothing given as first argument'
default:
throw "$ doesn't currently work on a " + (typeof sel)
}
})