Below is an example of establishing a counter in html (sourced from internet).
JS
$(document).ready(function() {
var positionFromTop = 100;
$(window).scroll(function() {
var $y = $(window).scrollTop();
if ($y >= positionFromTop) {
var i;
for (i = 0; i <= 5400; i += 100) {
setTimeout(Counter_Fn(i), 3000);
}
}
});
});
function Counter_Fn(x) {
document.getElementById("Counter_1").innerHTML = x;
}
body {
background-color: #F46A6A;
color: #fff;
max-width: 800px;
margin: 1000px auto 0;
text-align: center;
display: table;
}
.counter {
display: table-cell;
margin: 1.5%;
font-size: 50px;
background-color: #FF6F6F;
width: 200px;
border-radius: 50%;
height: 200px;
vertical-align: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "Counter_1" class="counter">0</div>
While this is serving basic purpose of mine, but I want to add few more details as below
- I want counter should start when I scroll down to a specific point (once page is loaded). In current example, counter just starts when page load, not waiting for me till I reach it's position below
- Increment should be 100 not 1
- I also want to have comma seperator after 1,000
Any pointer to achieve above functionalities will be highly appreciated