3 Copyright 2013 The Polymer Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file.
9 <title>infinite-scroll
</title>
10 <meta name=
"viewport" content=
"width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
29 -webkit-transform: translate3d(
0,
0,
0);
30 box-sizing: border-box;
35 font-family: sans-serif;
38 border-bottom:
1px solid lightgray;
45 justify-content: center;
50 background-color: lightblue;
56 flex-direction: column;
57 padding:
10px
10px
10px
0px;
82 text-overflow: ellipsis;
100 <div class=
"item conversation">
101 <div class=
"avatar">A
</div>
102 <div class=
"summary">
103 <div class=
"topline">
104 <div class=
"participants">{{ participants }}
</div>
105 <div class=
"time">{{ time }}
</div>
107 <div class=
"bottomline">
108 <div class=
"preview"><span class=
"subject">{{ subject }}
</span> —
109 <span class=
"snippet">{{ snippet }}
</span></div>
110 <div class=
"trinkets">☆</div>
118 function getRandomItem(array
) {
119 return array
[Math
.floor(Math
.random() * array
.length
)];
121 function generateFakeData() {
122 var kNumberOfItems
= 500;
123 var possibleParticipants
= [
124 'Adam', 'Ojan', 'Elliot', 'Chris',
126 var possibleTimes
= [
127 'Now', 'Yesterday', 'Last week',
129 var possibleSubjects
= [
130 'Do you even bench?',
131 'I like to scroll forever',
133 'What if my subject is really long? Longer than that. Like really, really, long?',
135 var possibleSnippets
= [
136 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.',
137 'When, in disgrace with fortune and men\'s eyes, I all alone beweep my outcast state,',
138 'We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility',
140 var data
= new Array(kNumberOfItems
);
141 for (var i
= 0; i
< kNumberOfItems
; ++i
) {
143 participants
: getRandomItem(possibleParticipants
),
144 time
: getRandomItem(possibleTimes
),
145 subject
: getRandomItem(possibleSubjects
),
146 snippet
: getRandomItem(possibleSnippets
),
148 console
.log(data
[i
]);
153 var data
= generateFakeData();
158 var kPhysicalCount
= 30;
160 var container
= document
.getElementById('container');
161 var runway
= document
.getElementById('runway');
163 var template
= runway
.querySelector('template');
164 var items
= new Array(kPhysicalCount
);
165 for (var i
= 0; i
< kPhysicalCount
; ++i
) {
166 var fragment
= template
.content
.cloneNode(true);
167 runway
.appendChild(fragment
);
168 var item
= runway
.lastElementChild
;
170 item
.transformValue_
= 0;
171 item
.participants_
= item
.querySelector('.participants').firstChild
;
172 item
.time_
= item
.querySelector('.time').firstChild
;
173 item
.subject_
= item
.querySelector('.subject').firstChild
;
174 item
.snippet_
= item
.querySelector('.snippet').firstChild
;
179 var physicalHeight
= kHeight
* kPhysicalCount
;
181 function updateText(item
, index
) {
182 var datum
= data
[index
% data
.length
];
183 item
.participants_
.nodeValue
= datum
.participants
;
184 item
.time_
.nodeValue
= datum
.time
;
185 item
.subject_
.nodeValue
= datum
.subject
;
186 item
.snippet_
.nodeValue
= datum
.snippet
;
189 container
.addEventListener('scroll', function(e
) {
190 var scrollTop
= container
.scrollTop
;
192 var firstVirtualIndex
= Math
.floor(scrollTop
/ kHeight
);
193 var firstPhysicalIndex
= firstVirtualIndex
% kPhysicalCount
;
195 var baseVirtualIndex
= firstVirtualIndex
- firstPhysicalIndex
;
197 var baseTransformValue
= kHeight
* baseVirtualIndex
;
198 var nextTransformValue
= baseTransformValue
+ physicalHeight
;
200 var baseTransformString
= 'translate3d(0,' + baseTransformValue
+ 'px,0)';
201 var nextTransformString
= 'translate3d(0,' + nextTransformValue
+ 'px,0)';
203 window
.requestAnimationFrame(function() {
204 for (var i
= 0; i
< firstPhysicalIndex
; ++i
) {
206 if (item
.transformValue_
!= nextTransformValue
) {
207 updateText(item
, baseVirtualIndex
+ kPhysicalCount
+ i
);
208 item
.style
.WebkitTransform
= nextTransformString
;
210 item
.transformValue_
= nextTransformValue
;
212 for (var i
= firstPhysicalIndex
; i
< kPhysicalCount
; ++i
) {
214 if (item
.transformValue_
!= baseTransformValue
) {
215 updateText(item
, baseVirtualIndex
+ i
);
216 item
.style
.WebkitTransform
= baseTransformString
;
218 item
.transformValue_
= baseTransformValue
;