forked from chadliu23/event-infinite-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-infinite-scroll.html
162 lines (139 loc) · 4.55 KB
/
event-infinite-scroll.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
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
<!--
Element providing infinite scroll to track the parentNode on reaching bottom and end.
It will raise event when it reaches.
##### Example
<event-infinite-scroll
auto-start
scroll-offset="500"
loading-delay="1000"
on-reach-bottom="{{reachBottom}}"
on-reach-top="{{reachTop}}">
</event-infinite-scroll>
@element event-infinite-scroll
@blurb Element providing infinite scroll to track the parentNode on reaching bottom and end.
@status alpha
@homepage http://chadliu23.github.io/event-infinite-scroll
@demo demo/index.html
-->
<link rel="import" href="../polymer/polymer.html">
<dom-module id="event-infinite-scroll">
<template></template>
<script>
Polymer({
is: "event-infinite-scroll",
/**
* Fired when reach bottom.
*
* @event reach-bottom
*/
/**
* Fired when reach top.
*
* @event reach-top
*/
properties: {
/**
* track scroll on ready
* otherwise you can call observer when ever you want
*
* @boolean autoStart
*/
autoStart: {
type: Boolean,
value: false,
observer: '_startScroll'
},
/**
* The offset is used for triggering the notifications
* before the real top/bottom is reached
*
* @int scrollOffset
*/
scrollOffset: {
type: Number,
value: 200,
reflectToAttribute: true
},
/**
* You can set your item loading delay time when scroll infinite
*
* @int loadingDelay (in milliseconds)
*/
loadingDelay: {
type: Number,
value: 0,
reflectToAttribute: true
}
},
observerStatus: true,
observer: null,
/**
* Triggered when the autoStart status is changed
*
* @readyOnStart [boolean] autoStart
*/
_startScroll: function( newValue ) {
if ( newValue ) {
this.startObserve();
}
},
startObserve: function () {
this.parentNode.addEventListener( "scroll", function () {
this.scrolled();
}.bind( this ) );
this.parentNode.addEventListener( "mousewheel", function () {
this.scrolledWheel();
}.bind( this ) );
var self = this;
this.observer = new MutationObserver( function () {
window.setTimeout( function () {
if ( ! self.observerStatus ) {
this.observer.disconnect();
return;
}
this.scrolled();
}.bind( this ), 10 );
}.bind( this ) );
this.observer.observe( this.parentNode, {
childList: true,
subtree: true,
characterData: true
});
},
stopObserve: function () {
this.parentNode.removeEventListener("scroll");
this.parentNode.removeEventListener("mousewheel");
if (this.observer) {
this.observer.disconnect();
}
},
scrolledWheel: function () {
if ( this.parentNode.scrollHeight == this.parentNode.clientHeight ) {
this.scrolled();
}
},
scrolled: function () {
if ( this._isReachBottom() ) {
// fire the reach-bottom after with delay if loadingDelay is set
this.async( function () {
this.fire( "reach-bottom" );
}, this.loadingDelay );
}
else if ( this._isReachTop() ) {
// fire the reach-top after with delay if loadingDelay is set
this.async( function () {
this.fire( "reach-top" );
}, this.loadingDelay );
}
},
_isReachTop: function () {
return ( this.parentNode.scrollTop <= this.scrollOffset );
},
_isReachBottom: function () {
var scrollBottom = this.parentNode.scrollTop + this.parentNode.clientHeight;
var belowView = this.parentNode.scrollHeight - scrollBottom;
return ( belowView <= this.scrollOffset );
}
});
</script>
</dom-module>