This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
baseline.js
166 lines (134 loc) · 4.32 KB
/
baseline.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
* Baseline.js 1.1
*
* Copyright 2013, Daniel Eden http://daneden.me
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: 2014-06-20
*/
(function (window, $) {
'use strict';
var baseline = (function () {
/**
* `_base` will later hold the value for the current baseline that matches
* the given breakpoint. `_breakpoints` will hold a reference to all
* breakpoints given to the baseline call.
*/
var _base = 0,
_breakpoints = {},
_dynamicBase;
/**
* @name _setBase
*
* Set the correct margin-bottom on the given element to get back to the
* baseline.
*
* @param {Element} element
*
* @private
*/
function _setBase (element) {
var height = element.offsetHeight,
current, old;
if( _dynamicBase ) {
/**
* Compute the _base through a user defined function on each execution.
* This could be used to get the current grid size for different breakpoints
* from an actual element property instead of defining those breakpoints in the options.
*/
_base = _dynamicBase();
}
else {
/**
* In this step we loop through all the breakpoints, if any were given.
* If the baseline call received a number from the beginning, this loop
* is simply ignored.
*/
for (var key in _breakpoints) {
current = key;
if (document.body.clientWidth > current && current >= old) {
_base = _breakpoints[key];
old = current;
}
}
}
/**
* We set the element's margin-bottom style to a number that pushes the
* adjacent element down far enough to get back onto the baseline.
*/
element.style.marginBottom = _base - (height % _base) + 'px';
}
/**
* @name _init
*
* Call `_setBase()` on the given element and add an event listener to the
* window to reset the baseline on resize.
*
* @param {Element} element
*
* @private
*/
function _init (element) {
_setBase(element);
if ('addEventListener' in window) {
window.addEventListener('resize', function () { _setBase(element); }, false);
} else if ('attachEvent' in window) {
window.attachEvent('resize', function () { _setBase(element); });
}
}
/**
* @name baseline
*
* Gets the correct elements and attaches the baseline behaviour to them.
*
* @param {String/Element/NodeList} elements
* @param {Number/Object} options
*/
return function baseline (elements, options) {
/**
* Accept a NodeList or a selector string and set `targets` to the
* relevant elements.
*/
var targets = typeof elements === 'string' ? document.querySelectorAll(elements) : elements,
len = targets.length;
/**
* Decide whether to set the `_breakpoints` or `_dynamicBase` variables or not.
* This will be relevant in the `_setBase()` function.
*/
if (typeof options === 'number') {
_base = parseInt(options, 10);
} else if (typeof options === 'function') {
_dynamicBase = options;
} else if (typeof options === 'object') {
var em = parseInt(getComputedStyle(document.body, null).getPropertyValue('font-size'), 10);
for (var point in _breakpoints) {
var unitless = /\d+em/.test(point) ? parseInt(point, 10) * em : /\d+px/.test(point) ? parseInt(point, 10) : point;
_breakpoints[unitless] = parseInt(_breakpoints[point], 10);
}
}
/**
* If we have multiple elements, loop through them, otherwise just
* initialise the functionality on the single element.
*/
if (len > 1) {
while (len--) { _init(targets[len]); }
} else {
_init(targets[0]);
}
};
}());
/**
* Export baseline as a jQuery or Zepto plugin if any of them are loaded,
* otherwise export as a browser global.
*/
if (typeof $ !== "undefined") {
$.extend($.fn, {
baseline: function (options) {
return baseline(this, options);
}
});
} else {
window.baseline = baseline;
}
}(window, window.jQuery || window.Zepto || undefined));