-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollAmount.js
76 lines (60 loc) · 2.13 KB
/
scrollAmount.js
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
/*!
* Run a callback after the user scrolls, calculating the distance and direction scrolled
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Function} callback The callback function to run
* @param {Integer} refresh How long to wait between scroll events [optional]
*/
var scrollDistance = function (callback, refresh) {
// Make sure a valid callback was provided
if (!callback || typeof callback !== 'function') return;
if (!window.startedScroll) {
window.scrollStartTime = Date.now();
window.startedScroll = true;
}
var isScrolling, start, end, distance, timeTaken;
window.addEventListener('scroll', function (event) {
if (!start) {
start = window.pageYOffset;
}
window.clearTimeout(isScrolling);
isScrolling = setTimeout(function() {
end = window.pageYOffset;
distance = end - start;
window.startedScroll = false;
timeTaken = Date.now() - window.scrollStartTime;
callback(distance, timeTaken);
window.startedScroll = false;
window.scrollStartTime = Date.now();
start = null;
end = null;
distance = null;
}, refresh || 61);
}, false);
};
$(document).ready(function() {
var BATCH_SIZE = 1000;
var counter = 1;
for (var i = 0 ; i < BATCH_SIZE ; i++) {
$('#main-div').append("<p>" + i.toString() + "</p>");
counter++;
}
scrollDistance(function (distance, timeTaken) {
if (distance == 0) return;
var randomId = parseInt(Math.random().toString(10) * 100000);
var elementId = 'amount-div-' + randomId.toString();
var divContent = parseInt(Math.abs(distance), 10) + 'px ' + (distance < 0 ? '↑' : '↓') + " @" + (distance * 1000 / timeTaken).toPrecision(4).toString() + 'px/s';
$("#amount").append("<div id='" + elementId + "'>" + divContent + "</div>");
setTimeout(function() {
$("#" + elementId).remove();
}, 10000);
});
$(window).scroll(function() {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
var currCounter = counter;
for (var i = currCounter; i < currCounter + BATCH_SIZE ; i++) {
$('body').append("<p>" + i.toString() + "</p>");
}
counter += BATCH_SIZE;
}
});
});