-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (47 loc) · 1.42 KB
/
index.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
function inVisible(element) {
//Checking if the element is
//visible in the viewport
var WindowTop = $(window).scrollTop();
var WindowBottom = WindowTop + $(window).height();
var ElementTop = element.offset().top;
var ElementBottom = ElementTop + element.height();
//animating the element if it is
//visible in the viewport
if ((ElementBottom <= WindowBottom) && ElementTop >= WindowTop)
animate(element);
}
function animate(element) {
//Animating the element if not animated before
if (!element.hasClass('ms-animated')) {
var maxval = element.data('max');
var html = element.html();
element.addClass("ms-animated");
$({
countNum: element.html()
}).animate({
countNum: maxval
}, {
//duration 5 seconds
duration: 1000,
easing: 'linear',
step: function () {
element.html(Math.floor(this.countNum) + html);
},
complete: function () {
element.html(this.countNum + html);
}
});
}
}
//When the document is ready
$(function () {
//This is triggered when the
//user scrolls the page
$(window).scroll(function () {
//Checking if each items to animate are
//visible in the viewport
$("h2[data-max]").each(function () {
inVisible($(this));
});
})
});