Skip to content

Instantly share code, notes, and snippets.

@stephband
Last active April 9, 2016 14:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephband/7788077 to your computer and use it in GitHub Desktop.
Save stephband/7788077 to your computer and use it in GitHub Desktop.
Feature detect devices that send continuous scroll events. Typically fails on iOS, which only sends scroll events after a scroll gesture has come to rest.
(function(jQuery) {
var win = jQuery(window);
var sampleLength = 20;
var maxInterval = 250;
var avgInterval = 35;
var timeStamps = [];
function diff(n, i, array) {
return array[i + 1] - n;
}
function range(n, i, array) {
// Throw out large values as they are likely pauses in user behaviour.
// Throw out 0 values because, well, they likely come from duplicate
// scroll events.
return n > 0 && n < maxInterval;
}
function average(t, n, i, array) {
return t + n / array.length;
}
function scroll(e) {
timeStamps.push(e.timeStamp);
// Wait till the sample is big enough.
if (timeStamps.length < sampleLength) { return; }
// Then determine if the average time between samples is low enough
// to consider to be continuous scrolling.
var interval = timeStamps.map(diff).filter(range).reduce(average, 0);
if (interval > 0 && interval < avgInterval) {
jQuery.support.continuousScrollEvents = true;
if (window.console && console.log) {
console.log('Continuous scroll events detected. Average interval:', interval);
}
}
// Stop collecting samples.
win.off('scroll', scroll);
timeStamps.length = 0;
}
win.on('scroll', scroll);
// Assume that we don't have continuous scrolling by default.
jQuery.support.continuousScrollEvents = false;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment