forked from nathanyarnold/rHeight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrHeight.js
304 lines (247 loc) · 9.04 KB
/
rHeight.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Description:
jQuery plugin that uses min-height to force DOM elements to the height of the viewport.
Basically a CSS style="height:100%" analogue to style="width:100%".
Allows for:
- min-width thresholds (functionality doesn't engage until cpecified min-width is reached in viewport),
- min-heights and max-heights on elements using either pixels or width:height ratios,
- offsets that subtract either a set pixels amount or the combined height of specified DOM elements from total height,
- extention of set height to all specified child-nodes.
Note: Currently requires CSS box-model "box-sizing:border-box" on all elements to work.
To-do:
1. Calculate heights with default CSS box-model (height + margin + border),
*/
(function( $ ){
var pluginName = 'rHeight',
selectors = {
root: 'data-rheight',
rootAttr: 'data-rheight-attr',
rootOffset: 'data-rheight-offset',
thresholdWidth: 'data-rheight-threshold-width',
thresholdHeight:'data-rheight-threshold-height',
minHeight: 'data-rheight-minheight',
maxHeight: 'data-rheight-maxheight',
child: 'data-rheight-child',
childAttr: 'data-rheight-child-attr',
childOffset: 'data-rheight-child-offset',
childThresholdWidth: 'data-rheight-child-threshold-width',
childThresholdHeight: 'data-rheight-child-threshold-height'
},
// Objects to work on
root = false,
viewport = {
width: 0,
height: 0
},
resizeTimer = false,
// METHODS
methods = {
init: function() {
//console.log("$.fn['"+ pluginName +"'].methods.init()");
// bail if there are no images to work with
if ( root.length < 1 ) return;
//console.log(root);
// set fixed-height on page load
window.setTimeout( function() {
methods.resize();
}, 10
);
// eventListener on any page-resize
$( window ).on( 'orientationchange', $.proxy( this, 'resize' ) );
$( window ).on( 'resize', $.proxy( this, 'handleResize' ) );
},
// use this, so we're not CONSTANTLY re-calculating during a resize event
handleResize: function() {
// if we're already doing this, don't bother doing it again
if ( resizeTimer )
clearTimeout( resizeTimer )
// run method on resize
resizeTimer = window.setTimeout( function() {
methods.resize();
}, 50);
},
resize: function() {
//console.log("$.fn['"+ pluginName +"'].methods.resize()");
// get viewport dimensions
methods._setViewport();
// resize
root.each(function() {
var $this = $( this );
// get threshold, if it doesn't make it, don't do this
if ( $this.attr( selectors.thresholdWidth ) && viewport.width < $this.attr( selectors.thresholdWidth ) ) {
methods.disable( $this );
return;
};
// get height threshold, if it doesn't make it, don't do this
if ( $this.attr( selectors.thresholdHeight ) && viewport.height < $this.attr( selectors.thresholdHeight ) ) {
methods.disable( $this );
return;
};
// otherwise, resize
methods._resizeNode( $this );
});
},
// not using, reset styles
disable: function ( $mod ) {
//console.log(" - $.fn['"+ pluginName +"'].methods.reset()");
$mod.attr( 'style', '' );
var $children = $mod.find( '['+ selectors.child +']' );
$children.each( function() {
$(this).attr( 'style', '' );
});
},
// set styles for each registered item
_resizeNode: function ( $mod ) {
//console.log(" - $.fn['"+ pluginName +"'].methods.handleModule()");
// get threshold, if it doesn't make it, don't do this
if ( viewport.width < $mod.attr( selectors.minHeight ) ) {
methods.disable( $mod );
return;
};
// figure out offset
var offset = methods._getOffset( $mod, selectors.rootOffset );
// define height
var newHeight = viewport.height - offset;
var attr = methods._getAttr( $mod.attr( selectors.rootAttr ) );
//console.log(' newHeight: '+ newHeight);
//console.log(' attr: '+ attr );
// respect min-height
if ( $mod.attr( selectors.minHeight ) && newHeight < methods._getHeight( $mod.attr( selectors.minHeight ), viewport.width ) )
newHeight = methods._getHeight( $mod.attr( selectors.minHeight ), viewport.width );
// respect max-height
if ( $mod.attr( selectors.maxHeight ) && newHeight > methods._getHeight( $mod.attr( selectors.maxHeight ), viewport.width ) )
newHeight = methods._getHeight( $mod.attr( selectors.maxHeight ), viewport.width );
$mod.attr( 'style', attr +':'+ newHeight +'px;' );
// look for inner nodes to resize as well
methods._resizeChildren( $mod, newHeight );
},
_resizeChildren: function( $mod, height ) {
//console.log("$.fn['"+ pluginName +"'].methods._resizeChildren($mod, '"+ height +"')");
var $children = $mod.find( '['+ selectors.child +']' );
$children.each( function() {
var $this = $(this);
// get threshold, if it doesn't make it, don't do this
if ( $this.attr( selectors.childThresholdWidth ) && viewport.width < $this.attr( selectors.childThresholdWidth ) ) {
methods._disableChild( $this );
return;
};
// get height threshold, if it doesn't make it, don't do this
if ( $this.attr( selectors.childThresholdHeight ) && viewport.height < $this.attr( selectors.childThresholdHeight ) ) {
methods._disableChild( $this );
return;
};
methods._resizeChild( $this, height );
});
},
_resizeChild: function( $child, height ) {
//console.log("$.fn['"+ pluginName +"'].methods._resizeChild($child, '"+ height +"')");
//console.log( $child );
// figure out offset
var offset = methods._getOffset( $child, selectors.childOffset, height );
//console.log(' - offset:'+ offset);
// resize
var attr = methods._getAttr( $child.attr( selectors.childAttr ) );
var val = methods._getAttrValue ( $child, $child.attr( selectors.childAttr ), height-offset );
$child.attr( 'style', attr +':'+ val +'px;' );
},
_disableChild: function( $child ) {
$child.attr( 'style', '' );
},
_setViewport: function() {
//console.log("$.fn['"+ pluginName +"'].methods._setViewport()");
viewport.width = $( document.body ).width();
viewport.height = $( window ).height();
},
_getAttr: function( attr ) {
//console.log(" $.fn['"+ pluginName +"'].methods._getAttr('"+ attr +"')");
switch ( attr ) {
case 'min-height':
case 'max-height':
case 'height':
case 'margin-top':
case 'padding-top':
case 'border-top':
case 'margin-bottom':
case 'padding-bottom':
case 'border-bottom':
case 'top':
case 'bottom':
return attr;
case 'center':
return 'margin-top';
default:
return 'min-height';
};
},
_getAttrValue: function( $this, attr, height ) {
//console.log(" $.fn['"+ pluginName +"'].methods._getAttrValue('"+ attr +"', '"+ height +"')");
if ( attr=='center' ) {
// if outerHeight is larger, return zero, otherwise, center vertically
var rAttr = ( height > $this.outerHeight() ) ? ( height - $this.outerHeight() ) / 2 : 0;
} else {
var rAttr = height;
}
return rAttr;
},
// returns a pixel height value where a pixel or ratio is sent in
_getHeight: function( height, width ) {
//console.log(" - $.fn['"+ pluginName +"'].methods._getHeight("+ height +", "+ width +")");
if ( height.indexOf(':') < 0 ) {
// if absulute value, simply return
return parseInt(height);
} else {
// otherwise, we have a ratio, so calculate height from that
var tmp = height.split(':');
return parseInt(width) * parseInt(tmp[1]) / parseInt(tmp[0]);
};
},
_getOffset: function( $mod, offsetAttr, height ) {
//console.log(" - $.fn['"+ pluginName +"'].methods._getOffset( $mod, '"+ offsetAttr +"')");
// just return 0 if attribute is not set
if ( !$mod.attr( offsetAttr ) || $mod.attr( offsetAttr )=="0")
return 0;
// return 0 if there's an attribute, but no value
var value = $mod.attr( offsetAttr );
if ( !value )
return 0;
// split values by comma
var valuesArray = value.split(',');
// go through each item, get offset for each
var total = 0;
$.each(valuesArray, function(i, value) {
var subTotal = methods._getOffsetItem( value.trim() );
total += subTotal;
});
return total;
},
_getOffsetItem: function( value ) {
// get int value
var intValue = parseInt( value );
// simply return px value, if it's an int
if ( value==intValue || value==intValue+'px')
return intValue;
// or return offset if it's a %
if ( value==intValue+'%')
return Math.round(height * .01 * intValue);
// else return element height
var $node = $( value );
if ( $node.length > 0 ) {
var height = 0;
$node.each( function() {
height += $(this).outerHeight( true );
});
return height;
} else {
return 0;
};
},
EOF: null
};
$.fn[pluginName] = function () {
root = this;
methods.init();
};
$( function() {
$( '['+ selectors.root + ']' )[ pluginName ]();
} );
})(jQuery);