PageSourceSearch

https://ejssm.org/archives/wp-content/themes/infinity-pro/js/match-height.js?ver=0.5.2

js ejssm.org collected 2026-10-02 10:19:05 UTC 9,303 bytes, 387 lines download raw bytes

vendor: 9,303 bytes, lines 1-387
1/**
2 * jquery-match-height master by @liabru
3 * http://brm.io/jquery-match-height/
4 * License: MIT
5 */
6;
7(function(factory) { // eslint-disable-line no-extra-semi
8	'use strict';
9	if (typeof define === 'function' && define.amd) {
10		// AMD
11		define(['jquery'], factory);
12	} else if (typeof module !== 'undefined' && module.exports) {
13		// CommonJS
14		module.exports = factory(require('jquery'));
15	} else {
16		// Global
17		factory(jQuery);
18	}
19})(function($) {
20	/*
21	 *  internal
22	 */
23
24	var _previousResizeWidth = -1,
25		_updateTimeout = -1;
26
27	/*
28	 *  _parse
29	 *  value parse utility function
30	 */
31
32	var _parse = function(value) {
33		// parse value and convert NaN to 0
34		return parseFloat(value) || 0;
35	};
36
37	/*
38	 *  _rows
39	 *  utility function returns array of jQuery selections representing each row
40	 *  (as displayed after float wrapping applied by browser)
41	 */
42
43	var _rows = function(elements) {
44		var tolerance = 1,
45			$elements = $(elements),
46			lastTop = null,
47			rows = [];
48
49		// group elements by their top position
50		$elements.each(function() {
51			var $that = $(this),
52				top = $that.offset().top - _parse($that.css('margin-top')),
53				lastRow = rows.length > 0 ? rows[rows.length - 1] : null;
54
55			if (lastRow === null) {
56				// first item on the row, so just push it
57				rows.push($that);
58			} else {
59				// if the row top is the same, add to the row group
60				if (Math.floor(Math.abs(lastTop - top)) <= tolerance) {
61					rows[rows.length - 1] = lastRow.add($that);
62				} else {
63					// otherwise start a new row group
64					rows.push($that);
65				}
66			}
67
68			// keep track of the last row top
69			lastTop = top;
70		});
71
72		return rows;
73	};
74
75	/*
76	 *  _parseOptions
77	 *  handle plugin options
78	 */
79
80	var _parseOptions = function(options) {
81		var opts = {
82			byRow: true,
83			property: 'height',
84			target: null,
85			remove: false
86		};
87
88		if (typeof options === 'object') {
89			return $.extend(opts, options);
90		}
91
92		if (typeof options === 'boolean') {
93			opts.byRow = options;
94		} else if (options === 'remove') {
95			opts.remove = true;
96		}
97
98		return opts;
99	};
100
101	/*
102	 *  matchHeight
103	 *  plugin definition
104	 */
105
106	var matchHeight = $.fn.matchHeight = function(options) {
107		var opts = _parseOptions(options);
108
109		// handle remove
110		if (opts.remove) {
111			var that = this;
112
113			// remove fixed height from all selected elements
114			this.css(opts.property, '');
115
116			// remove selected elements from all groups
117			$.each(matchHeight._groups, function(key, group) {
118				group.elements = group.elements.not(that);
119			});
120
121			// TODO: cleanup empty groups
122
123			return this;
124		}
125
126		if (this.length <= 1 && !opts.target) {
127			return this;
128		}
129
130		// keep track of this group so we can re-apply later on load and resize events
131		matchHeight._groups.push({
132			elements: this,
133			options: opts
134		});
135
136		// match each element's height to the tallest element in the selection
137		matchHeight._apply(this, opts);
138
139		return this;
140	};
141
142	/*
143	 *  plugin global options
144	 */
145
146	matchHeight.version = 'master';
147	matchHeight._groups = [];
148	matchHeight._throttle = 80;
149	matchHeight._maintainScroll = false;
150	matchHeight._beforeUpdate = null;
151	matchHeight._afterUpdate = null;
152	matchHeight._rows = _rows;
153	matchHeight._parse = _parse;
154	matchHeight._parseOptions = _parseOptions;
155
156	/*
157	 *  matchHeight._apply
158	 *  apply matchHeight to given elements
159	 */
160
161	matchHeight._apply = function(elements, options) {
162		var opts = _parseOptions(options),
163			$elements = $(elements),
164			rows = [$elements];
165
166		// take note of scroll position
167		var scrollTop = $(window).scrollTop(),
168			htmlHeight = $('html').outerHeight(true);
169
170		// get hidden parents
171		var $hiddenParents = $elements.parents().filter(':hidden');
172
173		// cache the original inline style
174		$hiddenParents.each(function() {
175			var $that = $(this);
176			$that.data('style-cache', $that.attr('style'));
177		});
178
179		// temporarily must force hidden parents visible
180		$hiddenParents.css('display', 'block');
181
182		// get rows if using byRow, otherwise assume one row
183		if (opts.byRow && !opts.target) {
184
185			// must first force an arbitrary equal height so floating elements break evenly
186			$elements.each(function() {
187				var $that = $(this),
188					display = $that.css('display');
189
190				// temporarily force a usable display value
191				if (display !== 'inline-block' && display !== 'flex' && display !== 'inline-flex') {
192					display = 'block';
193				}
194
195				// cache the original inline style
196				$that.data('style-cache', $that.attr('style'));
197
198				$that.css({
199					'display': display,
200					'padding-top': '0',
201					'padding-bottom': '0',
202					'margin-top': '0',
203					'margin-bottom': '0',
204					'border-top-width': '0',
205					'border-bottom-width': '0',
206					'height': '100px',
207					'overflow': 'hidden'
208				});
209			});
210
211			// get the array of rows (based on element top position)
212			rows = _rows($elements);
213
214			// revert original inline styles
215			$elements.each(function() {
216				var $that = $(this);
217				$that.attr('style', $that.data('style-cache') || '');
218			});
219		}
220
221		$.each(rows, function(key, row) {
222			var $row = $(row),
223				targetHeight = 0;
224
225			if (!opts.target) {
226				// skip apply to rows with only one item
227				if (opts.byRow && $row.length <= 1) {
228					$row.css(opts.property, '');
229					return;
230				}
231
232				// iterate the row and find the max height
233				$row.each(function() {
234					var $that = $(this),
235						style = $that.attr('style'),
236						display = $that.css('display');
237
238					// temporarily force a usable display value
239					if (display !== 'inline-block' && display !== 'flex' && display !== 'inline-flex') {
240						display = 'block';
241					}
242
243					// ensure we get the correct actual height (and not a previously set height value)
244					var css = {
245						'display': display
246					};
247					css[opts.property] = '';
248					$that.css(css);
249
250					// find the max height (including padding, but not margin)
251					if ($that.outerHeight(false) > targetHeight) {
252						targetHeight = $that.outerHeight(false);
253					}
254
255					// revert styles
256					if (style) {
257						$that.attr('style', style);
258					} else {
259						$that.css('display', '');
260					}
261				});
262			} else {
263				// if target set, use the height of the target element
264				targetHeight = opts.target.outerHeight(false);
265			}
266
267			// iterate the row and apply the height to all elements
268			$row.each(function() {
269				var $that = $(this),
270					verticalPadding = 0;
271
272				// don't apply to a target
273				if (opts.target && $that.is(opts.target)) {
274					return;
275				}
276
277				// handle padding and border correctly (required when not using border-box)
278				if ($that.css('box-sizing') !== 'border-box') {
279					verticalPadding += _parse($that.css('border-top-width')) + _parse($that.css('border-bottom-width'));
280					verticalPadding += _parse($that.css('padding-top')) + _parse($that.css('padding-bottom'));
281				}
282
283				// set the height (accounting for padding and border)
284				$that.css(opts.property, (targetHeight - verticalPadding) + 'px');
285			});
286		});
287
288		// revert hidden parents
289		$hiddenParents.each(function() {
290			var $that = $(this);
291			$that.attr('style', $that.data('style-cache') || null);
292		});
293
294		// restore scroll position if enabled
295		if (matchHeight._maintainScroll) {
296			$(window).scrollTop((scrollTop / htmlHeight) * $('html').outerHeight(true));
297		}
298
299		return this;
300	};
301
302	/*
303	 *  matchHeight._applyDataApi
304	 *  applies matchHeight to all elements with a data-match-height attribute
305	 */
306
307	matchHeight._applyDataApi = function() {
308		var groups = {};
309
310		// generate groups by their groupId set by elements using data-match-height
311		$('[data-match-height], [data-mh]').each(function() {
312			var $this = $(this),
313				groupId = $this.attr('data-mh') || $this.attr('data-match-height');
314
315			if (groupId in groups) {
316				groups[groupId] = groups[groupId].add($this);
317			} else {
318				groups[groupId] = $this;
319			}
320		});
321
322		// apply matchHeight to each group
323		$.each(groups, function() {
324			this.matchHeight(true);
325		});
326	};
327
328	/*
329	 *  matchHeight._update
330	 *  updates matchHeight on all current groups with their correct options
331	 */
332
333	var _update = function(event) {
334		if (matchHeight._beforeUpdate) {
335			matchHeight._beforeUpdate(event, matchHeight._groups);
336		}
337
338		$.each(matchHeight._groups, function() {
339			matchHeight._apply(this.elements, this.options);
340		});
341
342		if (matchHeight._afterUpdate) {
343			matchHeight._afterUpdate(event, matchHeight._groups);
344		}
345	};
346
347	matchHeight._update = function(throttle, event) {
348		// prevent update if fired from a resize event
349		// where the viewport width hasn't actually changed
350		// fixes an event looping bug in IE8
351		if (event && event.type === 'resize') {
352			var windowWidth = $(window).width();
353			if (windowWidth === _previousResizeWidth) {
354				return;
355			}
356			_previousResizeWidth = windowWidth;
357		}
358
359		// throttle updates
360		if (!throttle) {
361			_update(event);
362		} else if (_updateTimeout === -1) {
363			_updateTimeout = setTimeout(function() {
364				_update(event);
365				_updateTimeout = -1;
366			}, matchHeight._throttle);
367		}
368	};
369
370	/*
371	 *  bind events
372	 */
373
374	// apply on DOM ready event
375	$(matchHeight._applyDataApi);
376
377	// update heights on load and resize events
378	$(window).bind('load', function(event) {
379		matchHeight._update(false, event);
380	});
381
382	// throttled update heights on resize events
383	$(window).bind('resize orientationchange', function(event) {
384		matchHeight._update(true, event);
385	});
386
387});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.