Last active
August 13, 2021 15:47
-
-
Save alex-boom/5287d57706b8db140aa1886300500be9 to your computer and use it in GitHub Desktop.
animated-counter
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
// ресурс где можно посмотреть https://jsfiddle.net/2v3mq3nh/4/ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<style> | |
html, | |
body { | |
height: 100%; | |
} | |
#upper-push { | |
height: 100%; | |
width: 100%; | |
display: block; | |
background: red; | |
color: white; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="upper-push"> | |
Scroll down | |
</div> | |
<div id="numbers"> | |
<span class="fig-number">25</span> | |
<span class="fig-number">78</span> | |
</div> | |
<script> | |
// inViewport jQuery plugin | |
// http://stackoverflow.com/a/26831113/383904 | |
$(function ($, win) | |
{ | |
$.fn.inViewport = function (cb) | |
{ | |
return this.each(function (i, el) | |
{ | |
function visPx() | |
{ | |
var H = $(this).height(), | |
r = el.getBoundingClientRect(), t = r.top, b = r.bottom; | |
return cb.call(el, Math.max(0, t > 0 ? H - t : (b < H ? b : H))); | |
} visPx(); | |
$(win).on("resize scroll", visPx); | |
}); | |
}; | |
}(jQuery, window)); | |
jQuery(function ($) | |
{ // DOM ready and $ in scope | |
$(".fig-number").inViewport(function (px) | |
{ // Make use of the `px` argument!!! | |
// if element entered V.port ( px>0 ) and | |
// if prop initNumAnim flag is not yet set | |
// = Animate numbers | |
if (px > 0 && !this.initNumAnim) { | |
this.initNumAnim = true; // Set flag to true to prevent re-running the same animation | |
$(this).prop('Counter', 0).animate({ | |
Counter: $(this).text() | |
}, { | |
duration: 1000, | |
step: function (now) | |
{ | |
$(this).text(Math.ceil(now)); | |
} | |
}); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment