responsive-element.html
1.81 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
<dom-module id="responsive-element">
<style>
:host {
display: block;
}
</style>
<template>
<content></content>
</template>
</dom-module>
<script>
(function() {
var _els = [];
var _lastRecalc = new Date().getTime();
var _recalc = function() {
for (var i = 0; i < _els.length; i++) {
_els[i].recalc();
}
};
Polymer({
is: 'responsive-element',
properties: {
size: {
type: String,
readOnly: true,
reflectToAttribute: true
},
s: {
type: Number,
value: 768
},
m: {
type: Number,
value: 992
},
l: {
type: Number,
value: 1200
},
xl: {
type: Number,
value: 1600
}
},
attached: function() {
_els.push(this);
this.recalc();
},
detached: function() {
_els.splice(_els.indexOf(this), 1);
},
recalc: function() {
var w = this.getBoundingClientRect().width;
if (w < this.s) { this._setSize('xs'); }
else if (w < this.m) { this._setSize('s'); }
else if (w < this.l) { this._setSize('m'); }
else if (w < this.xl) { this._setSize('l'); }
else { this._setSize('xl'); }
}
});
var _debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
window.addEventListener('resize', _debounce(_recalc, 6));
})();
</script>