Last active
August 22, 2019 16:37
-
-
Save kreativan/8678dea4bc1b9aa41ca589f4d536381d to your computer and use it in GitHub Desktop.
Animate numbers, when they enter viewport using uikit scrollspy componenet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function animateNumbers() { | |
var elements = document.querySelectorAll(".tm-animate-number"); | |
elements.forEach(e => { | |
let current = 0; | |
let end = e.getAttribute("data-number"); | |
let end_2 = (end > 200) ? end - 50 : end; | |
let increase_by = (end > 200) ? 23 : 1; | |
let duration = (end > 50) ? 5 : 80; | |
UIkit.scrollspy(e, function() { | |
e.addEventListener("inview", function() { | |
var timer = setInterval(function() { | |
current += increase_by; | |
e.innerHTML = current; | |
if (current >= end_2) { | |
clearInterval(timer); | |
} | |
}, duration); | |
// Last numbers increase by 1 if its more then 200 | |
if(end > 200) { | |
var timer2 = setInterval(function() { | |
current += 1; | |
e.innerHTML = current; | |
if (current == end) { | |
clearInterval(timer2); | |
} | |
}, 20); | |
} | |
}); | |
}); | |
}); | |
} | |
// Run Function | |
animateNumbers(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<span class="tm-animate-number" data-number="1200"></span> | |
<span class="tm-animate-number" data-number="220"></span> | |
<span class="tm-animate-number" data-number="10"></span> | |
<span class="tm-animate-number" data-number="2000"></span> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment