Skip to content

Commit 8de266f

Browse files
committed
prefetch max scroll top value and cap it using the prefetch value to avoid one roundtrip
1 parent 7ff6861 commit 8de266f

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

js/stroll.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -273,29 +273,32 @@
273273

274274
/**
275275
* Fetches the latest properties from the DOM to ensure that
276-
* this list is in sync with its contents.
276+
* this list is in sync with its contents. This is typically
277+
* only used once (per list) at initialization.
277278
*/
278279
TouchList.prototype.sync = function() {
279280
this.items = Array.prototype.slice.apply( this.element.children );
280281

281-
// Cache the list height so we don't need to go back to the DOM so much
282282
this.listHeight = this.element.offsetHeight;
283283

284-
this.top.natural = this.element.scrollTop;
285-
this.top.value = this.top.natural;
284+
var item;
286285

287286
// One loop to get the properties we need from the DOM
288287
for( var i = 0, len = this.items.length; i < len; i++ ) {
289-
var item = this.items[i];
288+
item = this.items[i];
290289
item._offsetHeight = item.offsetHeight;
291290
item._offsetTop = item.offsetTop;
292291
item._offsetBottom = item._offsetTop + item._offsetHeight;
293292
item._state = '';
294293

295-
// Animating ppacity is a MAJOR performance hit on mobile so we can't allow it
294+
// Animating opacity is a MAJOR performance hit on mobile so we can't allow it
296295
item.style.opacity = 1;
297296
}
298297

298+
this.top.natural = this.element.scrollTop;
299+
this.top.value = this.top.natural;
300+
this.top.max = item._offsetBottom - this.listHeight;
301+
299302
// Force an update
300303
this.update( true );
301304

@@ -365,7 +368,7 @@
365368
if( this.touch.isAccellerating && sameDirection ) {
366369
clearInterval( this.touch.accellerateTimeout );
367370

368-
// Increase velocity exponentially
371+
// Increase velocity significantly
369372
this.velocity += ( this.touch.previous - this.touch.value ) / 10;
370373
}
371374
else {
@@ -383,6 +386,7 @@
383386
var distanceMoved = this.touch.start - this.touch.value;
384387

385388
if( !this.touch.isAccellerating ) {
389+
// Apply velocity based on the start position of the touch
386390
this.velocity = ( this.touch.start - this.touch.value ) / 10;
387391
}
388392

@@ -419,7 +423,9 @@
419423
if( this.velocity || this.touch.offset ) {
420424
// Scroll the DOM and add on the offset from touch
421425
this.element.scrollTop = scrollTop;
422-
scrollTop = this.element.scrollTop;
426+
427+
// Keep the scroll value within bounds
428+
scrollTop = Math.max( 0, Math.min( this.element.scrollTop, this.top.max ) );
423429

424430
// Cache the currently set scroll top and touch offset
425431
this.top.value = scrollTop - this.touch.offset;

js/stroll.min.js

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)