今回は、ウェブページに動くボックスを配置する方法を紹介します。
CSSとJavaScriptを使って楽しく学びながら、視覚的に魅力的なアニメーションを実現します。
See the Pen Untitled by kasasagi_kmnmc (@kasasagi_kmnmc) on CodePen.
HTMLの設定
まず、ウェブページの基本的な構造を設定します。
このコードをHTMLファイルにコピーしてください。
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>魅力的なウェブデザインを作ろう!動くボックスをCSSとJavaScriptで配置する方法</title>
    <link rel="stylesheet" href="styles.css"> <!-- CSSファイルのリンク -->
</head>
<body>
    <div class="content-overlay">
        <p>ここに重ねたいコンテンツを追加します。</p>
    </div>
    <div class="container">
        <!-- JavaScriptで生成するボックスが配置されます -->
    </div>
    <script src="script.js"></script> <!-- JavaScriptファイルのリンク -->
</body>
</html>CSSの設定
次に、スタイルを定義します。このスタイルで、ウェブページの見た目を整え、ボックスが動くようにします。styles.cssというファイルを作成し、以下のコードを貼り付けてください。
/* styles.css */
body {
    margin: 0;
    font-family: Arial, sans-serif;
}
.container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh; /* ウィンドウの高さいっぱいに設定 */
    overflow: visible; /* オーバーフローを可視に設定 */
    z-index: 0; /* コンテンツよりも下に配置 */
}
.content-overlay {
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: rgba(255, 255, 255, 0.5); /* 半透明の背景 */
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 24px;
    color: #333;
    z-index: 1; /* コンテンツよりも上に配置 */
    backdrop-filter: blur(5px); /* ブラーの強さを調整 */
}
.box {
    position: absolute;
    width: 50px;
    height: 50px;
    background-color: #3498db;
    opacity: 0;
}JavaScriptの設定
最後に、動くボックスを生成してアニメーションさせるJavaScriptコードを追加します。script.jsというファイルを作成し、以下のコードを貼り付けてください。
// script.js
document.addEventListener('DOMContentLoaded', function () {
    const container = document.querySelector('.container');
    const numBoxes = 10; // 生成するボックスの数
    // ボックスを生成して配置する関数
    function createBoxes() {
        for (let i = 0; i < numBoxes; i++) {
            const box = document.createElement('div');
            box.className = 'box';
            container.appendChild(box);
            animateBox(box); // ボックスをアニメーションさせる
        }
    }
    // ボックスをアニメーションさせる関数
    function animateBox(box) {
        // ランダムな幅と高さを設定
        const minWidth = 40; // 最小の幅
        const maxWidth = 50; // 最大の幅
        const minHeight = 40; // 最小の高さ
        const maxHeight = 50; // 最大の高さ
        const width = Math.floor(Math.random() * (maxWidth - minWidth + 1)) + minWidth;
        const height = Math.floor(Math.random() * (maxHeight - minHeight + 1)) + minHeight;
        box.style.width = width + 'px';
        box.style.height = height + 'px';
        // 初期位置をランダムに設定
        const initialX = Math.random() * (container.clientWidth - width);
        const initialY = Math.random() * (container.clientHeight - height);
        box.style.left = initialX + 'px';
        box.style.top = initialY + 'px';
        // ボックスの色をランダムに設定
        changeBoxColor(box);
        // ボックスをスムーズに動かすアニメーション
        function moveBox() {
            const targetX = Math.random() * (container.clientWidth - width);
            const targetY = Math.random() * (container.clientHeight - height);
            const duration = Math.random() * 4000 + 2000; // 2秒から6秒のランダムな時間
            const fadeOutTime = Math.random() * duration; // フェードアウトをランダムにするための時間
            box.style.transition = `transform ${duration}ms ease-in-out, opacity ${fadeOutTime}ms ease-in-out`; // フェードアウト時間をランダムに設定
            box.style.transform = `translate(${targetX - initialX}px, ${targetY - initialY}px)`;
            fadeIn(box); // フェードイン
            setTimeout(() => {
                fadeOut(box); // フェードアウト
                setTimeout(() => {
                    moveBox(); // アニメーションを継続
                }, 1000); // フェードアウト後に1秒待機して再度アニメーションを開始
            }, duration); // 移動が完了したらフェードアウト
        }
        // ボックスの色をランダムに変更する関数
        function changeBoxColor(box) {
            const newColor = getRandomColor();
            box.style.backgroundColor = newColor;
        }
        // ランダムな色を生成する関数
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }
        // フェードインを実行する関数
        function fadeIn(box) {
            box.style.opacity = '1';
        }
        // フェードアウトを実行する関数
        function fadeOut(box) {
            box.style.opacity = '0';
        }
        // 初めの動きを開始
        moveBox();
    }
    // ボックスを生成して配置する
    createBoxes();
});ページが読み込まれた後にスクリプトを実行
ページが完全に読み込まれた後にスクリプトを実行するためのイベントリスナーを設定します。
// ページが完全に読み込まれた後にスクリプトを実行
document.addEventListener('DOMContentLoaded', function () {
    const container = document.querySelector('.container');
    const numBoxes = 10; // 生成するボックスの数ボックスのアニメーション関数
生成したボックスに対して、ランダムなサイズ、位置、色を設定し、アニメーションを実行する関数を作成します。
    // ボックスをアニメーションさせる関数
    function animateBox(box) {
        // ランダムな幅と高さを設定
        const minWidth = 40; // 最小の幅
        const maxWidth = 50; // 最大の幅
        const minHeight = 40; // 最小の高さ
        const maxHeight = 50; // 最大の高さ
        const width = Math.floor(Math.random() * (maxWidth - minWidth + 1)) + minWidth;
        const height = Math.floor(Math.random() * (maxHeight - minHeight + 1)) + minHeight;
        box.style.width = width + 'px';
        box.style.height = height + 'px';        // 初期位置をランダムに設定
        const initialX = Math.random() * (container.clientWidth - width);
        const initialY = Math.random() * (container.clientHeight - height);
        box.style.left = initialX + 'px';
        box.style.top = initialY + 'px';
        // ボックスの色をランダムに設定
        changeBoxColor(box);ボックスを動かすアニメーション関数
ボックスをランダムに移動させ、フェードインおよびフェードアウトの効果を追加する関数を定義します。
        // ボックスをスムーズに動かすアニメーション
        function moveBox() {
            const targetX = Math.random() * (container.clientWidth - width);
            const targetY = Math.random() * (container.clientHeight - height);
            const duration = Math.random() * 4000 + 2000; // 2秒から6秒のランダムな時間
            const fadeOutTime = Math.random() * duration; // フェードアウトをランダムにするための時間
            box.style.transition = `transform ${duration}ms ease-in-out, opacity ${fadeOutTime}ms ease-in-out`; // フェードアウト時間をランダムに設定
            box.style.transform = `translate(${targetX - initialX}px, ${targetY - initialY}px)`;
            fadeIn(box); // フェードイン
            setTimeout(() => {
                fadeOut(box); // フェードアウト
                setTimeout(() => {
                    moveBox(); // アニメーションを継続
                }, 1000); // フェードアウト後に1秒待機して再度アニメーションを開始
            }, duration); // 移動が完了したらフェードアウト
        }See the Pen Untitled by kasasagi_kmnmc (@kasasagi_kmnmc) on CodePen.
まとめ
これで、ウェブページに動くボックスを配置する方法を学びました。CSSとJavaScriptを使って、ウェブデザインにアニメーションを取り入れることができましたね。ぜひこのコードを活用して、あなたのウェブサイトをもっと魅力的にしてください!楽しいウェブデザインの旅をお楽しみください!

 
  
  
  
   
    
