当サイト使用テーマ

\ SWELL【WordPressテーマ】 /
当サイト使用サーバー
\ レンタルサーバー【ConoHa WING】 /
プログラミング学習

この記事では、JavaScriptのライブラリ『Anime.js』を使って風船が上昇するアニメーションを実装するプログラミングコードを解説していきます。
最初に完成形を見ていきます。
ページの読み込みから少し待つと画面下側から風船がゆっくりと上昇していきます。
See the Pen js-07 by aqua (@aqua-programdiary) on CodePen.
まずは、HTMLのコーディングをしていきます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<link rel="stylesheet" href="css/style.css">
<title>バルーンアップアニメーション</title>
</head>
<body>
<div class="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
<script src="main.js"></script>
</body>
</html>
<!DOCTYPE html>:
<html lang="ja">:
lang属性で言語を指定しています。<head>:
<meta charset="UTF-8">:
<meta name="viewport" content="width=device-width, initial-scale=1.0">:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">:
<link rel="stylesheet" href="css/style.css">:
<title>バルーンアップアニメーション</title>:
<body>:
<div class="container"></div>:
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>:
<script src="main.js"></script>:
main.js を読み込んでいます。ここでバルーンアップアニメーションのコードを実装します。次にCSSのコーディングをしていきます。
@charset "utf-8";
body {
background-color: #95c0ec;
overflow: hidden;
}
.container {
position: absolute;
top: 150%;
left: 50%;
transform: translate(-50%,-50%);
}
.blue {
position: absolute;
color: blue;
}
.red {
position: absolute;
color: red;
}
.green {
position: absolute;
color: green;
}
.yellow {
position: absolute;
color: yellow;
}
.pink {
position: absolute;
color: pink;
}
@charset "utf-8";
body:
.container:
position: absolute; で絶対位置に配置され、top: 150%; left: 50%; および transform: translate(-50%, -50%); で画面の中央に配置されます。これによって初期状態では画面外に配置されています。.blue, .red, .green, .yellow, .pink:
position: absolute; で絶対位置に配置され、color プロパティでアイコンの色を指定しています。最後にJavaScriptのコーディングをして完成です。
今回は、Anime.jsを使用しているのでAnime.jsの記入方法でコーディングしていきます。
const container = document.querySelector('.container');
function createBalloon(className) {
for (let i = 1; i <= 150; i++) {
const icon = document.createElement('i');
icon.classList.add(className);
icon.classList.add('bi');
icon.classList.add('bi-balloon-fill');
container.append(icon);
}
}
createBalloon('blue');
createBalloon('red');
createBalloon('green');
createBalloon('yellow');
createBalloon('pink');
function animateBalloons(className) {
anime({
targets: `.${className}`,
duration: 200000,
delay: anime.stagger(1000),
loop: true,
easing: 'easeOutCirc',
translateX: function(){
return anime.random(-100, 100) + 'vw';
},
translateY: function(){
return anime.random(-200, -200) + 'vh';
},
scale: function(){
return anime.random(1,7);
},
});
}
animateBalloons('blue');
animateBalloons('red');
animateBalloons('green');
animateBalloons('yellow');
animateBalloons('pink');
const container = document.querySelector('.container');
.container クラスを持つ要素を取得しています。ここにアイコンが追加されます。function createBalloon(className) { ... }:
className はアイコンの色を示します。bi クラスと bi-balloon-fill クラスは、Bootstrap Icons ライブラリのアイコンを指定しています。function animateBalloons(className) { ... }:
className はアニメーション対象のアイコンの色を示します。anime ライブラリを使用してアニメーションを設定します。targets で対象の要素を指定し、duration でアニメーションの継続時間を設定します。delay でアイコンごとに遅延を設定し、loop でアニメーションをループさせます。easing でアニメーションのイージングを指定し、translateX と translateY でランダムな移動を指定します。scale でランダムな拡大率を指定します。createBalloon と animateBalloons 関数を呼び出し、各アイコンの色に対してアニメーションを開始します。