matchheight 0.1.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/match-height.d.ts +9 -0
- package/dist/match-height.js +94 -0
- package/dist/match-height.module.js +86 -0
- package/dist/throttle.d.ts +2 -0
- package/package.json +12 -12
- package/readme.md +4 -2
- package/rollup.config.mjs +32 -0
- package/src/match-height.ts +111 -0
- package/src/{throttle.js → throttle.ts} +4 -4
- package/tsconfig.json +21 -0
- package/dist/MatchHeight.js +0 -124
- package/dist/MatchHeight.min.js +0 -7
- package/dist/MatchHeight.module.js +0 -116
- package/rollup.config.js +0 -44
- package/src/MatchHeight.js +0 -79
- package/src/main.js +0 -21
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @author yomotsu
|
|
3
|
+
* MatchHeight
|
|
4
|
+
* https://github.com/yomotsu/MatchHeight
|
|
5
|
+
* Released under the MIT License.
|
|
6
|
+
*/
|
|
7
|
+
(function (global, factory) {
|
|
8
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
9
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
10
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.MatchHeight = factory());
|
|
11
|
+
})(this, (function () { 'use strict';
|
|
12
|
+
|
|
13
|
+
function throttle(fn, threshold) {
|
|
14
|
+
let last, deferTimer;
|
|
15
|
+
return function () {
|
|
16
|
+
const now = Date.now();
|
|
17
|
+
if (last && now < last + threshold) {
|
|
18
|
+
clearTimeout(deferTimer);
|
|
19
|
+
deferTimer = setTimeout(function () {
|
|
20
|
+
last = now;
|
|
21
|
+
fn();
|
|
22
|
+
}, threshold);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
last = now;
|
|
26
|
+
fn();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const errorThreshold = 1;
|
|
32
|
+
class MatchHeight {
|
|
33
|
+
constructor(selector = '[data-mh]') {
|
|
34
|
+
this._remains = [];
|
|
35
|
+
this._selector = selector;
|
|
36
|
+
const update = this.update.bind(this);
|
|
37
|
+
const throttledUpdate = throttle(update, 200);
|
|
38
|
+
if (document.readyState === 'loading') {
|
|
39
|
+
document.addEventListener('DOMContentLoaded', update, { once: true });
|
|
40
|
+
}
|
|
41
|
+
if (document.readyState === 'interactive') {
|
|
42
|
+
document.addEventListener('load', update, { once: true });
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
update();
|
|
46
|
+
}
|
|
47
|
+
window.addEventListener('resize', throttledUpdate);
|
|
48
|
+
this.disconnect = () => {
|
|
49
|
+
window.removeEventListener('resize', throttledUpdate);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
update() {
|
|
53
|
+
const elements = document.querySelectorAll(this._selector);
|
|
54
|
+
if (elements.length === 0)
|
|
55
|
+
return;
|
|
56
|
+
this._remains = Array.prototype.map.call(elements, (el) => {
|
|
57
|
+
return {
|
|
58
|
+
el,
|
|
59
|
+
top: 0,
|
|
60
|
+
height: 0,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
this._remains.forEach((item) => {
|
|
64
|
+
item.el.style.minHeight = '';
|
|
65
|
+
});
|
|
66
|
+
this._process();
|
|
67
|
+
}
|
|
68
|
+
_process() {
|
|
69
|
+
this._remains.forEach((item) => {
|
|
70
|
+
const bb = item.el.getBoundingClientRect();
|
|
71
|
+
item.top = bb.top;
|
|
72
|
+
item.height = bb.height;
|
|
73
|
+
});
|
|
74
|
+
this._remains.sort((a, b) => a.top - b.top);
|
|
75
|
+
const processingTop = this._remains[0].top;
|
|
76
|
+
const processingTargets = this._remains.filter(item => Math.abs(item.top - processingTop) <= errorThreshold);
|
|
77
|
+
const maxHeightInRow = Math.max(...processingTargets.map((item) => item.height));
|
|
78
|
+
processingTargets.forEach((item) => {
|
|
79
|
+
const error = processingTop - item.top + errorThreshold;
|
|
80
|
+
const paddingAndBorder = parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-top')) +
|
|
81
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-bottom')) +
|
|
82
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-top-width')) +
|
|
83
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-bottom-width'));
|
|
84
|
+
item.el.style.minHeight = `${maxHeightInRow - paddingAndBorder + error}px`;
|
|
85
|
+
});
|
|
86
|
+
this._remains.splice(0, processingTargets.length);
|
|
87
|
+
if (0 < this._remains.length)
|
|
88
|
+
this._process();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return MatchHeight;
|
|
93
|
+
|
|
94
|
+
}));
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @author yomotsu
|
|
3
|
+
* MatchHeight
|
|
4
|
+
* https://github.com/yomotsu/MatchHeight
|
|
5
|
+
* Released under the MIT License.
|
|
6
|
+
*/
|
|
7
|
+
function throttle(fn, threshold) {
|
|
8
|
+
let last, deferTimer;
|
|
9
|
+
return function () {
|
|
10
|
+
const now = Date.now();
|
|
11
|
+
if (last && now < last + threshold) {
|
|
12
|
+
clearTimeout(deferTimer);
|
|
13
|
+
deferTimer = setTimeout(function () {
|
|
14
|
+
last = now;
|
|
15
|
+
fn();
|
|
16
|
+
}, threshold);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
last = now;
|
|
20
|
+
fn();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const errorThreshold = 1;
|
|
26
|
+
class MatchHeight {
|
|
27
|
+
constructor(selector = '[data-mh]') {
|
|
28
|
+
this._remains = [];
|
|
29
|
+
this._selector = selector;
|
|
30
|
+
const update = this.update.bind(this);
|
|
31
|
+
const throttledUpdate = throttle(update, 200);
|
|
32
|
+
if (document.readyState === 'loading') {
|
|
33
|
+
document.addEventListener('DOMContentLoaded', update, { once: true });
|
|
34
|
+
}
|
|
35
|
+
if (document.readyState === 'interactive') {
|
|
36
|
+
document.addEventListener('load', update, { once: true });
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
update();
|
|
40
|
+
}
|
|
41
|
+
window.addEventListener('resize', throttledUpdate);
|
|
42
|
+
this.disconnect = () => {
|
|
43
|
+
window.removeEventListener('resize', throttledUpdate);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
update() {
|
|
47
|
+
const elements = document.querySelectorAll(this._selector);
|
|
48
|
+
if (elements.length === 0)
|
|
49
|
+
return;
|
|
50
|
+
this._remains = Array.prototype.map.call(elements, (el) => {
|
|
51
|
+
return {
|
|
52
|
+
el,
|
|
53
|
+
top: 0,
|
|
54
|
+
height: 0,
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
this._remains.forEach((item) => {
|
|
58
|
+
item.el.style.minHeight = '';
|
|
59
|
+
});
|
|
60
|
+
this._process();
|
|
61
|
+
}
|
|
62
|
+
_process() {
|
|
63
|
+
this._remains.forEach((item) => {
|
|
64
|
+
const bb = item.el.getBoundingClientRect();
|
|
65
|
+
item.top = bb.top;
|
|
66
|
+
item.height = bb.height;
|
|
67
|
+
});
|
|
68
|
+
this._remains.sort((a, b) => a.top - b.top);
|
|
69
|
+
const processingTop = this._remains[0].top;
|
|
70
|
+
const processingTargets = this._remains.filter(item => Math.abs(item.top - processingTop) <= errorThreshold);
|
|
71
|
+
const maxHeightInRow = Math.max(...processingTargets.map((item) => item.height));
|
|
72
|
+
processingTargets.forEach((item) => {
|
|
73
|
+
const error = processingTop - item.top + errorThreshold;
|
|
74
|
+
const paddingAndBorder = parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-top')) +
|
|
75
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-bottom')) +
|
|
76
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-top-width')) +
|
|
77
|
+
parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-bottom-width'));
|
|
78
|
+
item.el.style.minHeight = `${maxHeightInRow - paddingAndBorder + error}px`;
|
|
79
|
+
});
|
|
80
|
+
this._remains.splice(0, processingTargets.length);
|
|
81
|
+
if (0 < this._remains.length)
|
|
82
|
+
this._process();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { MatchHeight as default };
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "matchheight",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"author": "Yomotsu",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"jsnext:main": "dist/
|
|
9
|
-
"module": "dist/
|
|
6
|
+
"repository": "yomotsu/matchheight",
|
|
7
|
+
"main": "dist/match-height.js",
|
|
8
|
+
"jsnext:main": "dist/match-height.module.js",
|
|
9
|
+
"module": "dist/match-height.module.js",
|
|
10
|
+
"types": "dist/match-height.d.ts",
|
|
10
11
|
"devDependencies": {
|
|
11
|
-
"
|
|
12
|
-
"rollup": "^
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
12
|
+
"@rollup/plugin-typescript": "^11.1.2",
|
|
13
|
+
"rollup": "^3.28.0",
|
|
14
|
+
"terser": "^5.19.2",
|
|
15
|
+
"tslib": "^2.6.1",
|
|
16
|
+
"typescript": "^5.1.6"
|
|
16
17
|
},
|
|
17
18
|
"scripts": {
|
|
18
19
|
"dev": "rollup --config --watch",
|
|
19
|
-
"build": "rollup --config"
|
|
20
|
-
"release": "rollup --config && uglifyjs dist/MatchHeight.js -cm --preamble \"/*!\n * @author yomotsu\n * MatchHeight\n * https://github.com/yomotsu/MatchHeight\n * Released under the MIT License.\n */\" > dist/MatchHeight.min.js"
|
|
20
|
+
"build": "rollup --config && terser dist/MatchHeight.js -o dist/MatchHeight.min.js --comments '/^!/'"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"matchHeight",
|
package/readme.md
CHANGED
|
@@ -13,9 +13,10 @@ No dependencies, Light weight, Makes elements equal height.
|
|
|
13
13
|
|
|
14
14
|
### Standalone
|
|
15
15
|
|
|
16
|
-
Copy MatchHeight.js from `/dist/MatchHeight.js` and place it in your project
|
|
16
|
+
Copy MatchHeight.js from `/dist/MatchHeight.js` and place it in your project, then call `new MatchHeight()`.
|
|
17
17
|
```html
|
|
18
|
-
<script src="./js/
|
|
18
|
+
<script src="./js/match-height.js"></script>
|
|
19
|
+
<script>new MatchHeight();</script>
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
### NPM
|
|
@@ -26,6 +27,7 @@ $ npm install --save matchheight
|
|
|
26
27
|
then
|
|
27
28
|
```javascript
|
|
28
29
|
import MatchHeight from 'matchheight';
|
|
30
|
+
MatchHeight();
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
for more info, see [the demo](https://yomotsu.github.io/MatchHeight/examples/).
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import pkg from './package.json' assert { type: "json" };
|
|
2
|
+
import typescript from 'typescript';
|
|
3
|
+
import rollupTypescript from '@rollup/plugin-typescript';
|
|
4
|
+
|
|
5
|
+
const license = `/*!
|
|
6
|
+
* @author yomotsu
|
|
7
|
+
* MatchHeight
|
|
8
|
+
* https://github.com/yomotsu/MatchHeight
|
|
9
|
+
* Released under the MIT License.
|
|
10
|
+
*/`;
|
|
11
|
+
|
|
12
|
+
export default {
|
|
13
|
+
input: './src/match-height.ts',
|
|
14
|
+
output: [
|
|
15
|
+
{
|
|
16
|
+
format: 'umd',
|
|
17
|
+
name: 'MatchHeight',
|
|
18
|
+
file: pkg.main,
|
|
19
|
+
banner: license,
|
|
20
|
+
indent: '\t',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
format: 'es',
|
|
24
|
+
file: pkg.module,
|
|
25
|
+
banner: license,
|
|
26
|
+
indent: '\t',
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
plugins: [
|
|
30
|
+
rollupTypescript( { typescript } ),
|
|
31
|
+
],
|
|
32
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import throttle from './throttle';
|
|
2
|
+
|
|
3
|
+
type Item = {
|
|
4
|
+
el: HTMLElement;
|
|
5
|
+
top: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const errorThreshold = 1; // in px
|
|
10
|
+
|
|
11
|
+
class MatchHeight {
|
|
12
|
+
|
|
13
|
+
private _selector: string;
|
|
14
|
+
private _remains: Item[] = [];
|
|
15
|
+
disconnect: () => void;
|
|
16
|
+
|
|
17
|
+
constructor( selector: string = '[data-mh]' ) {
|
|
18
|
+
|
|
19
|
+
this._selector = selector;
|
|
20
|
+
|
|
21
|
+
const update = this.update.bind( this );
|
|
22
|
+
const throttledUpdate = throttle( update, 200 );
|
|
23
|
+
|
|
24
|
+
if ( document.readyState === 'loading' ) {
|
|
25
|
+
|
|
26
|
+
document.addEventListener( 'DOMContentLoaded', update, { once: true } );
|
|
27
|
+
|
|
28
|
+
} if ( document.readyState === 'interactive' ) {
|
|
29
|
+
|
|
30
|
+
document.addEventListener( 'load', update, { once: true } );
|
|
31
|
+
|
|
32
|
+
} else {
|
|
33
|
+
|
|
34
|
+
update();
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
window.addEventListener( 'resize', throttledUpdate );
|
|
39
|
+
|
|
40
|
+
this.disconnect = () => {
|
|
41
|
+
|
|
42
|
+
window.removeEventListener( 'resize', throttledUpdate );
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
update() {
|
|
49
|
+
|
|
50
|
+
const elements = document.querySelectorAll( this._selector );
|
|
51
|
+
|
|
52
|
+
if ( elements.length === 0 ) return;
|
|
53
|
+
|
|
54
|
+
this._remains = Array.prototype.map.call( elements, ( el: HTMLElement ): Item => {
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
el,
|
|
58
|
+
top: 0,
|
|
59
|
+
height: 0,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
} ) as Item[];
|
|
63
|
+
// remove all height before
|
|
64
|
+
this._remains.forEach( ( item ) => {
|
|
65
|
+
|
|
66
|
+
item.el.style.minHeight = '';
|
|
67
|
+
|
|
68
|
+
} );
|
|
69
|
+
|
|
70
|
+
this._process();
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private _process() {
|
|
75
|
+
|
|
76
|
+
this._remains.forEach( ( item ) => {
|
|
77
|
+
|
|
78
|
+
const bb = item.el.getBoundingClientRect();
|
|
79
|
+
|
|
80
|
+
item.top = bb.top;
|
|
81
|
+
item.height = bb.height;
|
|
82
|
+
|
|
83
|
+
} );
|
|
84
|
+
|
|
85
|
+
this._remains.sort( ( a, b ) => a.top - b.top );
|
|
86
|
+
|
|
87
|
+
const processingTop = this._remains[ 0 ].top;
|
|
88
|
+
const processingTargets = this._remains.filter( item => Math.abs( item.top - processingTop ) <= errorThreshold );
|
|
89
|
+
const maxHeightInRow = Math.max( ...processingTargets.map( ( item ) => item.height ) );
|
|
90
|
+
|
|
91
|
+
processingTargets.forEach( ( item ) => {
|
|
92
|
+
|
|
93
|
+
const error = processingTop - item.top + errorThreshold;
|
|
94
|
+
const paddingAndBorder =
|
|
95
|
+
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'padding-top' ) ) +
|
|
96
|
+
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'padding-bottom' ) ) +
|
|
97
|
+
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'border-top-width' ) ) +
|
|
98
|
+
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'border-bottom-width' ) );
|
|
99
|
+
item.el.style.minHeight = `${ maxHeightInRow - paddingAndBorder + error }px`;
|
|
100
|
+
|
|
101
|
+
} );
|
|
102
|
+
|
|
103
|
+
this._remains.splice( 0, processingTargets.length );
|
|
104
|
+
|
|
105
|
+
if ( 0 < this._remains.length ) this._process();
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export default MatchHeight;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
function throttle( fn,
|
|
1
|
+
function throttle( fn: () => void, threshold: number ) {
|
|
2
2
|
|
|
3
|
-
let last, deferTimer;
|
|
3
|
+
let last: number, deferTimer: number;
|
|
4
4
|
|
|
5
5
|
return function () {
|
|
6
6
|
|
|
7
7
|
const now = Date.now();
|
|
8
8
|
|
|
9
|
-
if ( last && now < last +
|
|
9
|
+
if ( last && now < last + threshold ) {
|
|
10
10
|
|
|
11
11
|
clearTimeout( deferTimer );
|
|
12
12
|
deferTimer = setTimeout( function () {
|
|
@@ -14,7 +14,7 @@ function throttle( fn, threshhold ) {
|
|
|
14
14
|
last = now;
|
|
15
15
|
fn();
|
|
16
16
|
|
|
17
|
-
},
|
|
17
|
+
}, threshold );
|
|
18
18
|
|
|
19
19
|
} else {
|
|
20
20
|
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": "src",
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"target": "es2017",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"removeComments": true,
|
|
9
|
+
"experimentalDecorators": true,
|
|
10
|
+
"noImplicitAny": true,
|
|
11
|
+
"noImplicitThis": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noUnusedLocals": true,
|
|
14
|
+
"noUnusedParameters": true,
|
|
15
|
+
"strictNullChecks": true,
|
|
16
|
+
"strictFunctionTypes": true,
|
|
17
|
+
"noFallthroughCasesInSwitch": true
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"exclude": ["node_modules"]
|
|
21
|
+
}
|
package/dist/MatchHeight.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author yomotsu
|
|
3
|
-
* MatchHeight
|
|
4
|
-
* https://github.com/yomotsu/MatchHeight
|
|
5
|
-
* Released under the MIT License.
|
|
6
|
-
*/
|
|
7
|
-
(function (global, factory) {
|
|
8
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
9
|
-
typeof define === 'function' && define.amd ? define(factory) :
|
|
10
|
-
(global.MatchHeight = factory());
|
|
11
|
-
}(this, (function () { 'use strict';
|
|
12
|
-
|
|
13
|
-
function throttle(fn, threshhold) {
|
|
14
|
-
|
|
15
|
-
var last = void 0,
|
|
16
|
-
deferTimer = void 0;
|
|
17
|
-
|
|
18
|
-
return function () {
|
|
19
|
-
|
|
20
|
-
var now = Date.now();
|
|
21
|
-
|
|
22
|
-
if (last && now < last + threshhold) {
|
|
23
|
-
|
|
24
|
-
clearTimeout(deferTimer);
|
|
25
|
-
deferTimer = setTimeout(function () {
|
|
26
|
-
|
|
27
|
-
last = now;
|
|
28
|
-
fn();
|
|
29
|
-
}, threshhold);
|
|
30
|
-
} else {
|
|
31
|
-
|
|
32
|
-
last = now;
|
|
33
|
-
fn();
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
var errorThreshold = 1; // in px
|
|
39
|
-
var initialized = false;
|
|
40
|
-
var elements = void 0;
|
|
41
|
-
var remains = void 0;
|
|
42
|
-
|
|
43
|
-
var MatchHeight$1 = {
|
|
44
|
-
init: function init() {
|
|
45
|
-
|
|
46
|
-
initialized = true;
|
|
47
|
-
elements = document.querySelectorAll('[data-mh]');
|
|
48
|
-
MatchHeight$1.update();
|
|
49
|
-
},
|
|
50
|
-
update: function update() {
|
|
51
|
-
|
|
52
|
-
if (!initialized) {
|
|
53
|
-
|
|
54
|
-
MatchHeight$1.init();
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (elements.length === 0) return;
|
|
59
|
-
|
|
60
|
-
remains = Array.prototype.map.call(elements, function (el) {
|
|
61
|
-
|
|
62
|
-
return { el: el };
|
|
63
|
-
});
|
|
64
|
-
// remove all height before
|
|
65
|
-
remains.forEach(function (item) {
|
|
66
|
-
|
|
67
|
-
item.el.style.minHeight = '';
|
|
68
|
-
});
|
|
69
|
-
process();
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
function process() {
|
|
74
|
-
|
|
75
|
-
remains.forEach(function (item) {
|
|
76
|
-
|
|
77
|
-
var bb = item.el.getBoundingClientRect();
|
|
78
|
-
|
|
79
|
-
item.top = bb.top;
|
|
80
|
-
item.height = bb.height;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
remains.sort(function (a, b) {
|
|
84
|
-
return a.top - b.top;
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
var processingTop = remains[0].top;
|
|
88
|
-
var processingTargets = remains.filter(function (item) {
|
|
89
|
-
return Math.abs(item.top - processingTop) <= errorThreshold;
|
|
90
|
-
});
|
|
91
|
-
var maxHeightInRow = Math.max.apply(Math, processingTargets.map(function (item) {
|
|
92
|
-
return item.height;
|
|
93
|
-
}));
|
|
94
|
-
|
|
95
|
-
processingTargets.forEach(function (item) {
|
|
96
|
-
|
|
97
|
-
var error = processingTop - item.top + errorThreshold;
|
|
98
|
-
var paddingAndBorder = parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-top'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-bottom'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-top-width'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-bottom-width'), 10);
|
|
99
|
-
item.el.style.minHeight = maxHeightInRow - paddingAndBorder + error + 'px';
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
remains.splice(0, processingTargets.length);
|
|
103
|
-
|
|
104
|
-
if (0 < remains.length) process();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
var throttledUpdate = throttle(MatchHeight$1.update, 200);
|
|
108
|
-
|
|
109
|
-
window.addEventListener('DOMContentLoaded', function onDomReady() {
|
|
110
|
-
|
|
111
|
-
MatchHeight$1.init();
|
|
112
|
-
window.removeEventListener('DOMContentLoaded', onDomReady);
|
|
113
|
-
});
|
|
114
|
-
window.addEventListener('load', function onLoad() {
|
|
115
|
-
|
|
116
|
-
MatchHeight$1.update();
|
|
117
|
-
window.removeEventListener('load', onLoad);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
window.addEventListener('resize', throttledUpdate);
|
|
121
|
-
|
|
122
|
-
return MatchHeight$1;
|
|
123
|
-
|
|
124
|
-
})));
|
package/dist/MatchHeight.min.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author yomotsu
|
|
3
|
-
* MatchHeight
|
|
4
|
-
* https://github.com/yomotsu/MatchHeight
|
|
5
|
-
* Released under the MIT License.
|
|
6
|
-
*/
|
|
7
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.MatchHeight=e()}(this,function(){"use strict";function t(){i.forEach(function(t){var e=t.el.getBoundingClientRect();t.top=e.top,t.height=e.height}),i.sort(function(t,e){return t.top-e.top});var o=i[0].top,n=i.filter(function(t){return Math.abs(t.top-o)<=e}),r=Math.max.apply(Math,n.map(function(t){return t.height}));n.forEach(function(t){var n=o-t.top+e,i=parseFloat(window.getComputedStyle(t.el).getPropertyValue("padding-top"),10)+parseFloat(window.getComputedStyle(t.el).getPropertyValue("padding-bottom"),10)+parseFloat(window.getComputedStyle(t.el).getPropertyValue("border-top-width"),10)+parseFloat(window.getComputedStyle(t.el).getPropertyValue("border-bottom-width"),10);t.el.style.minHeight=r-i+n+"px"}),i.splice(0,n.length),0<i.length&&t()}var e=1,o=!1,n=void 0,i=void 0,r={init:function(){o=!0,n=document.querySelectorAll("[data-mh]"),r.update()},update:function(){if(!o)return void r.init();0!==n.length&&(i=Array.prototype.map.call(n,function(t){return{el:t}}),i.forEach(function(t){t.el.style.minHeight=""}),t())}},d=function(t,e){var o=void 0,n=void 0;return function(){var i=Date.now();o&&i<o+e?(clearTimeout(n),n=setTimeout(function(){o=i,t()},e)):(o=i,t())}}(r.update,200);return window.addEventListener("DOMContentLoaded",function t(){r.init(),window.removeEventListener("DOMContentLoaded",t)}),window.addEventListener("load",function t(){r.update(),window.removeEventListener("load",t)}),window.addEventListener("resize",d),r});
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author yomotsu
|
|
3
|
-
* MatchHeight
|
|
4
|
-
* https://github.com/yomotsu/MatchHeight
|
|
5
|
-
* Released under the MIT License.
|
|
6
|
-
*/
|
|
7
|
-
function throttle(fn, threshhold) {
|
|
8
|
-
|
|
9
|
-
var last = void 0,
|
|
10
|
-
deferTimer = void 0;
|
|
11
|
-
|
|
12
|
-
return function () {
|
|
13
|
-
|
|
14
|
-
var now = Date.now();
|
|
15
|
-
|
|
16
|
-
if (last && now < last + threshhold) {
|
|
17
|
-
|
|
18
|
-
clearTimeout(deferTimer);
|
|
19
|
-
deferTimer = setTimeout(function () {
|
|
20
|
-
|
|
21
|
-
last = now;
|
|
22
|
-
fn();
|
|
23
|
-
}, threshhold);
|
|
24
|
-
} else {
|
|
25
|
-
|
|
26
|
-
last = now;
|
|
27
|
-
fn();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var errorThreshold = 1; // in px
|
|
33
|
-
var initialized = false;
|
|
34
|
-
var elements = void 0;
|
|
35
|
-
var remains = void 0;
|
|
36
|
-
|
|
37
|
-
var MatchHeight$1 = {
|
|
38
|
-
init: function init() {
|
|
39
|
-
|
|
40
|
-
initialized = true;
|
|
41
|
-
elements = document.querySelectorAll('[data-mh]');
|
|
42
|
-
MatchHeight$1.update();
|
|
43
|
-
},
|
|
44
|
-
update: function update() {
|
|
45
|
-
|
|
46
|
-
if (!initialized) {
|
|
47
|
-
|
|
48
|
-
MatchHeight$1.init();
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (elements.length === 0) return;
|
|
53
|
-
|
|
54
|
-
remains = Array.prototype.map.call(elements, function (el) {
|
|
55
|
-
|
|
56
|
-
return { el: el };
|
|
57
|
-
});
|
|
58
|
-
// remove all height before
|
|
59
|
-
remains.forEach(function (item) {
|
|
60
|
-
|
|
61
|
-
item.el.style.minHeight = '';
|
|
62
|
-
});
|
|
63
|
-
process();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
function process() {
|
|
68
|
-
|
|
69
|
-
remains.forEach(function (item) {
|
|
70
|
-
|
|
71
|
-
var bb = item.el.getBoundingClientRect();
|
|
72
|
-
|
|
73
|
-
item.top = bb.top;
|
|
74
|
-
item.height = bb.height;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
remains.sort(function (a, b) {
|
|
78
|
-
return a.top - b.top;
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
var processingTop = remains[0].top;
|
|
82
|
-
var processingTargets = remains.filter(function (item) {
|
|
83
|
-
return Math.abs(item.top - processingTop) <= errorThreshold;
|
|
84
|
-
});
|
|
85
|
-
var maxHeightInRow = Math.max.apply(Math, processingTargets.map(function (item) {
|
|
86
|
-
return item.height;
|
|
87
|
-
}));
|
|
88
|
-
|
|
89
|
-
processingTargets.forEach(function (item) {
|
|
90
|
-
|
|
91
|
-
var error = processingTop - item.top + errorThreshold;
|
|
92
|
-
var paddingAndBorder = parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-top'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('padding-bottom'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-top-width'), 10) + parseFloat(window.getComputedStyle(item.el).getPropertyValue('border-bottom-width'), 10);
|
|
93
|
-
item.el.style.minHeight = maxHeightInRow - paddingAndBorder + error + 'px';
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
remains.splice(0, processingTargets.length);
|
|
97
|
-
|
|
98
|
-
if (0 < remains.length) process();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
var throttledUpdate = throttle(MatchHeight$1.update, 200);
|
|
102
|
-
|
|
103
|
-
window.addEventListener('DOMContentLoaded', function onDomReady() {
|
|
104
|
-
|
|
105
|
-
MatchHeight$1.init();
|
|
106
|
-
window.removeEventListener('DOMContentLoaded', onDomReady);
|
|
107
|
-
});
|
|
108
|
-
window.addEventListener('load', function onLoad() {
|
|
109
|
-
|
|
110
|
-
MatchHeight$1.update();
|
|
111
|
-
window.removeEventListener('load', onLoad);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
window.addEventListener('resize', throttledUpdate);
|
|
115
|
-
|
|
116
|
-
export default MatchHeight$1;
|
package/rollup.config.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import babel from 'rollup-plugin-babel'
|
|
2
|
-
|
|
3
|
-
const license = `/*!
|
|
4
|
-
* @author yomotsu
|
|
5
|
-
* MatchHeight
|
|
6
|
-
* https://github.com/yomotsu/MatchHeight
|
|
7
|
-
* Released under the MIT License.
|
|
8
|
-
*/`
|
|
9
|
-
|
|
10
|
-
export default {
|
|
11
|
-
entry: './src/main.js',
|
|
12
|
-
indent: '\t',
|
|
13
|
-
sourceMap: false,
|
|
14
|
-
plugins: [
|
|
15
|
-
babel( {
|
|
16
|
-
exclude: 'node_modules/**',
|
|
17
|
-
presets: [
|
|
18
|
-
[ 'env', {
|
|
19
|
-
targets: {
|
|
20
|
-
browsers: [
|
|
21
|
-
'last 2 versions',
|
|
22
|
-
'ie >= 9'
|
|
23
|
-
]
|
|
24
|
-
},
|
|
25
|
-
loose: true,
|
|
26
|
-
modules: false
|
|
27
|
-
} ]
|
|
28
|
-
]
|
|
29
|
-
} )
|
|
30
|
-
],
|
|
31
|
-
targets: [
|
|
32
|
-
{
|
|
33
|
-
format: 'umd',
|
|
34
|
-
moduleName: 'MatchHeight',
|
|
35
|
-
dest: 'dist/MatchHeight.js',
|
|
36
|
-
banner: license
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
format: 'es',
|
|
40
|
-
dest: 'dist/MatchHeight.module.js',
|
|
41
|
-
banner: license
|
|
42
|
-
}
|
|
43
|
-
]
|
|
44
|
-
};
|
package/src/MatchHeight.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
const errorThreshold = 1; // in px
|
|
2
|
-
let initialized = false;
|
|
3
|
-
let elements;
|
|
4
|
-
let remains;
|
|
5
|
-
|
|
6
|
-
const MatchHeight = {
|
|
7
|
-
|
|
8
|
-
init() {
|
|
9
|
-
|
|
10
|
-
initialized = true;
|
|
11
|
-
elements = document.querySelectorAll( '[data-mh]' );
|
|
12
|
-
MatchHeight.update();
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
update() {
|
|
17
|
-
|
|
18
|
-
if ( ! initialized ) {
|
|
19
|
-
|
|
20
|
-
MatchHeight.init();
|
|
21
|
-
return;
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if ( elements.length === 0 ) return;
|
|
26
|
-
|
|
27
|
-
remains = Array.prototype.map.call( elements, ( el ) => {
|
|
28
|
-
|
|
29
|
-
return { el: el };
|
|
30
|
-
|
|
31
|
-
} );
|
|
32
|
-
// remove all height before
|
|
33
|
-
remains.forEach( ( item ) => {
|
|
34
|
-
|
|
35
|
-
item.el.style.minHeight = '';
|
|
36
|
-
|
|
37
|
-
} );
|
|
38
|
-
process();
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
function process() {
|
|
45
|
-
|
|
46
|
-
remains.forEach( ( item ) => {
|
|
47
|
-
|
|
48
|
-
const bb = item.el.getBoundingClientRect();
|
|
49
|
-
|
|
50
|
-
item.top = bb.top;
|
|
51
|
-
item.height = bb.height;
|
|
52
|
-
|
|
53
|
-
} );
|
|
54
|
-
|
|
55
|
-
remains.sort( ( a, b ) => a.top - b.top );
|
|
56
|
-
|
|
57
|
-
const processingTop = remains[ 0 ].top;
|
|
58
|
-
const processingTargets = remains.filter( item => Math.abs( item.top - processingTop ) <= errorThreshold );
|
|
59
|
-
const maxHeightInRow = Math.max( ...processingTargets.map( ( item ) => item.height ) );
|
|
60
|
-
|
|
61
|
-
processingTargets.forEach( ( item ) => {
|
|
62
|
-
|
|
63
|
-
const error = processingTop - item.top + errorThreshold;
|
|
64
|
-
const paddingAndBorder =
|
|
65
|
-
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'padding-top' ), 10 ) +
|
|
66
|
-
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'padding-bottom' ), 10 ) +
|
|
67
|
-
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'border-top-width' ), 10 ) +
|
|
68
|
-
parseFloat( window.getComputedStyle( item.el ).getPropertyValue( 'border-bottom-width' ), 10 );
|
|
69
|
-
item.el.style.minHeight = `${ maxHeightInRow - paddingAndBorder + error }px`;
|
|
70
|
-
|
|
71
|
-
} );
|
|
72
|
-
|
|
73
|
-
remains.splice( 0, processingTargets.length );
|
|
74
|
-
|
|
75
|
-
if ( 0 < remains.length ) process();
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export default MatchHeight;
|
package/src/main.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import throttle from './throttle.js';
|
|
2
|
-
import MatchHeight from './MatchHeight.js';
|
|
3
|
-
|
|
4
|
-
const throttledUpdate = throttle( MatchHeight.update, 200 );
|
|
5
|
-
|
|
6
|
-
window.addEventListener( 'DOMContentLoaded', function onDomReady() {
|
|
7
|
-
|
|
8
|
-
MatchHeight.init();
|
|
9
|
-
window.removeEventListener( 'DOMContentLoaded', onDomReady );
|
|
10
|
-
|
|
11
|
-
} );
|
|
12
|
-
window.addEventListener( 'load', function onLoad() {
|
|
13
|
-
|
|
14
|
-
MatchHeight.update();
|
|
15
|
-
window.removeEventListener( 'load', onLoad );
|
|
16
|
-
|
|
17
|
-
} );
|
|
18
|
-
|
|
19
|
-
window.addEventListener( 'resize', throttledUpdate );
|
|
20
|
-
|
|
21
|
-
export default MatchHeight;
|