Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Created February 15, 2016 17:10
Show Gist options
  • Select an option

  • Save kvendrik/8c5fc016906c22f916a9 to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/8c5fc016906c22f916a9 to your computer and use it in GitHub Desktop.
Counter Test
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Counter Test</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 900 16px 'Source Sans Pro';
line-height: 1.5em;
background-color: indigo;
}
.counter {
font-size: 4em;
width: 120px;
height: 120px;
line-height: 115px;
text-align: center;
margin: 0 auto;
margin-top: 4em;
color: white;
border-radius: 100%;
border: solid 2px white;
opacity: 1;
}
.counter div {
transition: all .3s ease;
}
.counter.counter--btm div {
transform: translateY(20px);
}
.counter.counter--top div {
transform: translateY(-20px);
}
.counter.counter--fade div {
opacity: 0;
}
</style>
</head>
<body>
<div class="counter">
<div>1</div>
</div>
<script>
var startCounter = (function(){
var el = document.querySelector('.counter'),
numberEl = el.getElementsByTagName('div')[0],
i,
currInterval;
var callback = function(end){
el.classList.add('counter--top');
el.classList.add('counter--fade');
setTimeout(function(){
el.classList.remove('counter--top');
el.classList.add('counter--btm');
setTimeout(function(){
numberEl.innerHTML = i;
el.classList.remove('counter--btm');
el.classList.remove('counter--fade');
console.log(i, end);
if(i <= end){
clearInterval(currInterval);
} else {
i--;
}
}, 300);
}, 300);
};
return function(start, end){
numberEl.innerHTML = start;
i = start-1;
currInterval = setInterval(function(){
callback(end);
}, 1600);
};
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment