-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintersection-observer-element.html
90 lines (81 loc) · 2.3 KB
/
intersection-observer-element.html
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
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="intersection-observer-element">
<template>
<style>
:host {
display: block;
}
</style>
<div id="slottedData">
<slot></slot>
</div>
</template>
<script>
/**
* `intersection-observer-element`
* Reflects contained elements visibility in attributes
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class IntersectionObserverElement extends Polymer.Element {
static get is() {
return 'intersection-observer-element';
}
static get properties() {
return {
thresholds: {
type: Array,
value: [0.0, 0.25, 0.5, 0.75, 1.0]
},
rootMargin: {
type: String,
value: "0px"
},
visible: {
type: Boolean,
value: false,
reflectToAttribute: true,
readOnly: true,
notify: true
},
ratio: {
type: Number,
reflectToAttribute: true,
readOnly: true
},
visibleLimit: {
type: Number,
value: 0.5,
reflectToAttribute: true
}
};
}
connectedCallback() {
super.connectedCallback();
this.observer = new IntersectionObserver(this.handleIntersectionCallback.bind(this), {
root: document.rootElement,
rootMargin: this.rootMargin,
threshold: this.thresholds
});
this.observer.observe(this.$.slottedData);
}
handleIntersectionCallback(entries) {
if (!document.hidden) {
for (let entry of entries) {
this._setRatio(Number(entry.intersectionRatio).toFixed(2));
if (this.ratio > this.visibleLimit) {
this._setVisible(true);
} else {
this._setVisible(false);
}
}
} else {
this._setVisible(false);
}
}
}
window.customElements.define(IntersectionObserverElement.is, IntersectionObserverElement);
</script>
</dom-module>