SHU BLOG

BLOG NEWS ブログ

TITLE

【jQuery】テキストアニメーションを実装、Text Rotatorの使い方!!

SHU[シュウ]

1991年生まれ九州の宮崎県育ち
高校卒業後、愛知県で自動車関係の
お仕事に5年間勤め、WEB業界に
転職致しました。
趣味:サッカー観戦、ゲーム、映画、漫画
基本インドアです!笑

シュウ
シュウ
今回は【jQuery】テキストアニメーションを実装、Text Rotatorの使い方!!
の解説になります!

こんな方に読んでほしい

  • jQueryを学び始めた方へ
  • プラグインのText Rotatorを使って「テキストアニメーションを実装」をしたい方向け
  • 今回はText Rotatorについて解説しております。

今回は、「Text Rotator」についての解説になります。
Text Rotator」になりますが、今でも簡単で使いやすいプラグインになっております。

イメージ図は上記のようになります。
指定した箇所のみテキストアニメーションが適用されます。
アニメーションの種類には、「fade」、「flipUp」などが存在します。

Text Rotatorについて

Text Rotatorについて

Text Rotatorとは、「テキストアニメーション」のことを示します。

jQueryが苦手な方でも問題ありません。
まずは下記のサイトから「Text Rotator」をダウンロードしましょう。

Text Rotatorにアクセス
1https://github.com/peachananr/simple-text-rotator
「Code」ボタンをクリック
2
「Download ZIP」ボタンをクリック
3

必要なファイルについて

ダウンロード後に必要なファイルについて解説していきます。
まずは、ファイル名の「simple-text-rotator-master」をご確認ください。

cssファイル
1「simple-text-rotator-master」 > 「simpletextrotator.css
jsファイル
2「simple-text-rotator-master」 > 「jquery.simple-text-rotator.js」の2点を使用します。
<!--css-->
<link rel="stylesheet" href="css/simpletextrotator.css">
<!--js-->
<script src="js/jquery.simple-text-rotator.js" type="text/javascript"></script>

また、「text-rotator.js」を使用する際には、text-rotatorを使用する宣言をしなければなりません。
以下のコードを合わせて指定しましょう。

<!--js-->
<script src="js/jquery.simple-text-rotator.js" type="text/javascript"></script>
<script>
	$("〇〇").textrotator({
	  animation: "dissolve",
	  separator: ",",
	  speed: 2000
	});
</script>

「text-rotator.js」と繋げて記述するのが良いでしょう。
また、ファイルの階層は、ご自身のフォルダに合わせてください。

次に、jquery本体のプラグイン設置します。
今回は2つ目のパターンを採用して解説します。

1つ目は、ファイルをダウンロードして使用するパターン、
2つ目は、ファイルをダウンロードせずに、「URL」から読み込むパターンになります。
どちらでも問題ありません。

<!--js-->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="js/jquery.simple-text-rotator.js" type="text/javascript"></script>
<script>
	$("〇〇").textrotator({
	  animation: "dissolve",
	  separator: ",",
	  speed: 2000
	});
</script>

必ず、jquery本体のプラグイン上、text-rotatorを下に設置しましょう。
jqueryをダウンロードしたい方は、こちらのリンクから。

記述場所について

基本的にはbody内に指定するようにしましょう。
基本構造の書き方は以下のようになります。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title> 【jQuery】テキストアニメーションを実装...</title>
<!--css-->
<link rel="stylesheet" href="css/simpletextrotator.css">
</head>
<body>

 コンテンツが入ります。

<!--js-->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="js/jquery.simple-text-rotator.js" type="text/javascript"></script>
<script>
	$("〇〇").textrotator({
	  animation: "dissolve",
	  separator: ",",
	  speed: 2000
	});
</script>
</body>
</html>

テキストアニメーションの設定について

テキストアニメーションの設定について

次に、テキストアニメーションの設定について解説します。

テキストアニメーションの設定について

デモサイトはコチラから。
基本構造の書き方は以下のようになります。

テキストアニメーションの設定

<!--01:fade-->
<div class="animate" id="section-1">
	<div>
		<h1 class="demo1">ABCD <span class="rotate">EFGH, IJKL, MNOP, QRST</span> UVWXYZ</h1>
	</div>
</div>

<!--02:flip-->
<div class="animate" id="section-2">
<div>
	<h1 class="demo2">ABCD <span class="rotate">EFGH, IJKL, MNOP, QRST</span> UVWXYZ</h1>
	</div>
</div>

<!--03:flipCube-->
<div class="animate" id="section-3">
	<div>
		<h1 class="demo3">ABCD <span class="rotate">EFGH, IJKL, MNOP, QRST</span> UVWXYZ</h1>
	</div>
</div>

<!--04:flipUp-->
<div class="animate" id="section-4">
	<div>
		<h1 class="demo4">ABCD <span class="rotate">EFGH, IJKL, MNOP, QRST</span> UVWXYZ</h1>
	</div>
</div>
/*--CSSは自由に変更可--*/
.animate{
	display: table;
	width: 100%;
	padding: 100px 0;
	text-align: center;
}	

#section-1 {
	background-color: #d25f70;
	color: #fff;
}		

#section-2 {
	background-color: #253555;
	color: #fff;
}	

#section-3 {
	background-color: #e6bab7;
	color: #fff;
}

#section-4 {
	background-color: #000;
	color: #fff;
}
$(document).ready(function(){
	$(".demo1 .rotate").textrotator({
        animation: "fade",
        speed: 1000
    });
    $(".demo2 .rotate").textrotator({
        animation: "flip",
        speed: 1250
    });
    $(".demo3 .rotate").textrotator({
        animation: "flipCube",
        speed: 1500
    });
    $(".demo4 .rotate").textrotator({
        animation: "flipUp",
        speed: 1750
	});
});

アニメーションを指定する際には、rotateで囲み、〇〇〇〇,〇〇〇〇,〇〇〇〇のように、(,)カンマを使用します。

オプションについて

書き方は以下のようになります。

指定できる値
  • animation
    アニメーションの種類を指定します。
    種類には、「fade」、「flip」、「flipUp」、「flipCube」、「flipCubeUp」、「spin」になります。
  • speed
    アニメーションの速さを指定します。
    数値の値が小さいほど早くなります。

こちらのサイトで他にも解説しております。

まとめ

今回はこれで以上です。

POINT
スポンサーリンク

SHU

1991年生まれ九州の宮崎県育ち高校卒業後、愛知県で自動車関係のお仕事に5年間勤め、WEB業界に転職致しました。
趣味:サッカー観戦、ゲーム、映画、漫画基本インドアです!笑
つくる、しあわせをテーマとして主にWEBに関する様々な情報を発信しています。

最新記事

関連記事

オススメ記事

月別アーカイブ

page_top