JQuery で数字のカウントアップ・カウントダウンアニメーションを実装する

スポンサーリンク

JQuery で数字をカウントアップ・カウントダウンするには animate 関数で実現することが可能です。

Javascript 版も追加しました(2025.4.13)。

JQueryで数字をカウントアップするアニメーションのソースコード

はじめに、JQuery で数字をカウントアップするアニメーションの基本となるソースコードについて記載します。

JQuery ソースコード

$(function(){
	$('#number').animate({
		Counter: 5000
	}, {
		duration: 5000,
		easing: 'linear',
		step: function (now, fx) {
			$(this).html(Math.ceil(now));
		}
	});
});

Counter: 5000の箇所に最終的に表示したい数字を入力します。上記の例で言いますと、0から(初期値として0の場合)5000のカウントアップアニメーションを行うことになります。

duration: 5000の箇所に何秒間かけてアニメーションを行うかを記述します。1000 = 1秒ですので、上記の例では5000 = 5秒かけてアニメーションを行うことになります。

step: function (now, fx) 箇所の引数nowには0から5000までの数値が代入されます。上記の例では0〜1秒間に0〜1000が代入され、1〜2秒の間に1001〜2000が代入され、その繰り返しで5000までの値が代入されます。

この仕組みを利用して数字のカウントアップおよびカウントダウンアニメーションを実現します。

easing: ‘linear’について、jQuery API Documentation についてご確認ください。

参考:.animate() | jQuery API Documentation

以下では、ボタンを押して数字をカウントアップする方法、セレクトボタンを選択して数字をカウントアップする方法の例を記載します。

ボタンを押して数字をカウントアップする方法

ボタンを押して数字をカウントアップする方法は以下のようなソースコードで実現が可能です。

HTMLソースコード

<div id="ict_number">0</div>
<button id="ict_jquery_num_animation_btn">数字をカウントアップする</button>

JQuery ソースコード

$(function(){
	
	function ict_jquery_num_animation_btn_action() {
		$('#ict_number').animate({
			Counter: 5000
		}, {
			duration: 5000,
	        easing: 'linear',
	        step: function (now) {
	            $(this).html(Math.ceil(now));
	        }
		});
	}
			
	$(document).on('click', '#ict_jquery_num_animation_btn', (function() {
			ict_jquery_num_animation_btn_action();
		})
	);
});

実装例

0

セレクトボタンを選択して数字をカウントアップ・カウントダウンする方法

セレクトボタンを選択して数字をカウントアップ・カウントダウンする方法は、以下のようなソースコードで実現が可能です。

HTMLソースコード

<div id="ict_number_select">0</div>
<select id="ict_jquery_num_animation_select">
	<option value="5000">5,000円</option>
	<option value="6000">6,000円</option>
	<option value="7000">7,000円</option>
	<option value="8000">8,000円</option>
	<option value="9000">9,000円</option>
	<option value="10000">10,000円</option>
</select>

JQuery ソースコード

$(function(){
	
	function ict_jquery_num_animation_select_action(to) {
		$('#ict_number_select').animate({
			Counter: to
		}, {
			duration: 300,
	        easing: 'linear',
	        step: function (now) {
	            $(this).html(Math.ceil(now));
	        }
		});
	}
			
	$(document).on('change', '#ict_jquery_num_animation_select', (function() {
			ict_jquery_num_animation_select_action($(this).val());
		})
	);
});

実装例

0

以上ですが、JQuery で数字のカウントアップ・カウントダウンするアニメーションの実装方法でした。

中田勝久 ICTよろず相談所ではホームページ制作に関するご相談を受付しております。JQuery でお悩みの事がございましたらご相談ください。

※お詫び
2023年12月12日のホームページリニューアルの影響により、上記 JQuery の実装例が動作していませんでした。現在は修正済です。

上記と同じことを Javascript でやってみた

上記は JQuery で実現していましたが、プレーンな Javascript で数字をカウントアップ・カウントダウンさせるにはどうなのかな?とずっと思っていたまま放置していました。

重い腰を上げて、実行してみます。

調べてみると、requestAnimationFrame 関数を使うと実現できそうでした。

色々試した結果、以下のようなソースコードで一旦落ち着きましたので、メモ代わりに貼り付けておきます。

ボタンを押して数字をカウントアップ Javascript 版

<div id="ict_number_js">0</div>
<div><button id="ict_num_animation_btn_js">数字をカウントアップする</button></div>
const ict_number_js = document.getElementById("ict_number_js");
const btn = document.getElementById("ict_num_animation_btn_js");
const animateCountBtn = ((target) => {
	const current = parseInt( ict_number_js.textContent, 10 );
	const duration = 5000;
	const diff = target - current;
	
	let count = diff / ( 60 * (duration / 1000) );
	const absolute_count = count;
	
	const update = () => {
		const result = current + parseInt(count, 10);
	  	ict_number_js.textContent = result.toLocaleString();
	  	if ( target >= result ) {
	    	requestAnimationFrame(update);
	  	} else {
	    	ict_number_js.textContent = target.toLocaleString();
	  	}
	  	count = count + absolute_count;
	}
	requestAnimationFrame(update);
});
btn.addEventListener("click", (event) => {
	animateCountBtn(5000);
});

以下、ボタンを押して数字をカウントアップ Javascript 版の実行例です。

0

セレクトボタンを選択して数字をカウントアップ・カウントダウン javascript 版

<div id="ict_number_select_js">0</div>
<select id="ict_num_animation_select_js">
	<option value="0">選択してください</option>
	<option value="5000">5,000円</option>
	<option value="6000">6,000円</option>
	<option value="7000">7,000円</option>
	<option value="8000">8,000円</option>
	<option value="9000">9,000円</option>
	<option value="10000">10,000円</option>
</select>
const ict_number_select_js = document.getElementById("ict_number_select_js");
const select = document.getElementById("ict_num_animation_select_js");
const animateCountSelect = ((target) => {
	const current = parseInt( ict_number_select_js.textContent, 10 );
	const duration = 300;
	const diff = target - current;
	
	let count = diff / ( 60 * (duration / 1000) );
	const absolute_count = count;
	
	const update = () => {
		const result = current + parseInt(count, 10);
		let flg;
		if ( target > current ) {
			flg =  target >= result;
		} else {
			flg =  target < result;
		}
		ict_number_select_js.textContent = result;
		if (flg) {
			requestAnimationFrame(update);
		} else {
	    	ict_number_select_js.textContent = target;
	  	}
	  	count = count + absolute_count;
	}
	requestAnimationFrame(update);
});
select.addEventListener("change", (event) => {
	animateCountSelect(event.target.value);
});

以下、セレクトボタンを選択して数字をカウントアップ・カウントダウン javascript 版の実行例です。

0

コメント

タイトルとURLをコピーしました