Skip to content

Instantly share code, notes, and snippets.

@anubhavbhatt
Created December 12, 2019 14:01
Show Gist options
  • Save anubhavbhatt/322016dce2ffd5cc11a071ca20ad925c to your computer and use it in GitHub Desktop.
Save anubhavbhatt/322016dce2ffd5cc11a071ca20ad925c to your computer and use it in GitHub Desktop.
jQuery Countdown Script
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
font-size: 30px
}
div.column-3 {
width: 30%;
display: inline;
border: 1px solid #939;
background-color: #909;
padding: 8px;
}
.countdown-section {
color: #fff;
width: 600px;
}
<div class="countdown-section">
<div class="column-3">
<span class="countdown-span" data-count="30000000"></span>
</div>
<div class="column-3">
<span class="countdown-span" data-count="2340000000"></span>
</div>
<div class="column-3">
<span class="countdown-span" data-count="100000"></span>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".countdown-section").countdown({
classname: 'countdown-section'
});
});
</script>
/**
* Make the below code as countdown.js and include the countdown js in the script.
*/
(function($) {
$.fn.countdown = function(options) {
// Default options
var settings = $.extend({
classname: options.classname
}, options);
$(this).find('.countdown-span').each(function(index, value) {
var $this = $(this),
countTo = $this.data('count');
var lastItem;
$({
countNum: $this.text()
}).animate({
countNum: countTo
}, {
duration: 2000,
easing: 'swing',
step: function() {
if (this.countNum >= 1000000000000) {
var convert = this.countNum;
var converted = Math.sign(convert) * ((Math.abs(convert) / 1000000000000).toFixed(1));
$this.text(converted + "T");
this.countNum = Math.floor(converted) + "T";
lastItem = this.countNum;
} else if (this.countNum >= 1000000000) {
var convert = this.countNum;
var converted = Math.sign(convert) * ((Math.abs(convert) / 1000000000).toFixed(1));
$this.text(converted + "B");
this.countNum = Math.floor(converted) + "B";
lastItem = this.countNum;
} else if (this.countNum >= 1000000) {
var convert = this.countNum;
var converted = Math.sign(convert) * ((Math.abs(convert) / 1000000).toFixed(1));
$this.text(converted + "M");
this.countNum = Math.floor(converted) + "M";
lastItem = this.countNum;
} else if (this.countNum >= 1000) {
var convert = this.countNum;
var converted = Math.sign(convert) * ((Math.abs(convert) / 1000).toFixed(1));
$this.text(converted + "K");
this.countNum = Math.floor(converted) + "K";
lastItem = this.countNum;
} else {
$this.text(Math.ceil(this.countNum));
var converted = $this.text();
$this.text(converted);
}
},
complete: function() {
$this.text(lastItem);
}
});
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment