身内の宣伝コーナー

 

フルオーダーから既製デザイン柔軟に対応!

袴ロンパースの製作販売してます!レンタルもやってます!

  

  

袴ロンパースのFor 結
 

よかったら見てみてね

 

 

 

ココから本題 

さてやっていきましょう

 

 

HTMLで

・指定フォルダ内の画像(jpg,png)を対象に

・横スクロールのスライドショー

を作成したい

 

そこから自分の要望を伝え、エラーを伝えていくと案外すんなりできました

 

ブログの性質上平文なのが申し訳ないですが

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Swiper画像スライドショー</title>

    <!-- Swiper.jsのCDNを追加 -->

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">

    <style>

        body {

            font-family: Arial, sans-serif;

            margin: 0;

            padding: 20px;

            overflow: hidden;

        }

 

        #swiper-container {

            width: 100%;

            height: 100vh;

            overflow: hidden;

        }

 

        .swiper-slide {

            width: auto;

            height: 100%;

            display: flex;

            align-items: center;

            justify-content: center;

        }

 

        .swiper-slide img {

            max-height: 100vh;

            max-width: 1000px; /* ここを横幅1200pxに制限 */

            width: 100%;

        }

 

        #folderInput {

            display: none;

        }

 

        #selectFolderButton {

            padding: 10px;

            background-color: #4caf50;

            color: white;

            border: none;

            border-radius: 4px;

            cursor: pointer;

        }

    </style>

</head>

<body>

    <button id="selectFolderButton" onclick="openFolderDialog()">フォルダを選択</button>

    <input type="file" id="folderInput" webkitdirectory directory multiple style="display: none;">

    <div id="swiper-container" class="swiper-container">

        <div class="swiper-wrapper" id="swiper-wrapper"></div>

        <div class="swiper-button-next"></div>

        <div class="swiper-button-prev"></div>

    </div>

 

    <!-- Swiper.jsのスクリプト -->

    <script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>

    <script>

        function openFolderDialog() {

            document.getElementById('folderInput').click();

        }

 

        document.getElementById('folderInput').addEventListener('change', handleFileSelect);

 

        function handleFileSelect(event) {

            const swiperWrapper = document.getElementById('swiper-wrapper');

            swiperWrapper.innerHTML = '';

 

            const files = event.target.files;

            for (let i = 0; i < files.length; i++) {

                if (files[i].type.startsWith('image/')) {

                    const swiperSlide = document.createElement('div');

                    swiperSlide.className = 'swiper-slide';

 

                    const image = document.createElement('img');

                    image.src = URL.createObjectURL(files[i]);

 

                    swiperSlide.appendChild(image);

                    swiperWrapper.appendChild(swiperSlide);

                }

            }

 

            initSwiper();

        }

 

        function initSwiper() {

            new Swiper('.swiper-container', {

                direction: 'horizontal',

                loop: true,

                slidesPerView: 'auto',

                spaceBetween: 10,

                navigation: {

                    nextEl: '.swiper-button-next',

                    prevEl: '.swiper-button-prev',

                },

                autoplay: {

                    delay: 1000,

                    disableOnInteraction: false,

                },

                speed: 1000,

                effect: 'slide',

               

                loop: true,

                loopedSlides: 1,

                centeredSlides: true,

               

            });

        }

    </script>

</body>

</html>

 

 

 

これで指定フォルダ内の画像でスライドショーしてくれました。

 

当然ながら中身の理解はある程度必要ですが、ほぼすべて書いてくれるので良いですね

 

HTMLならPC環境も問わずに動くはずなので、ありがたいです。

Pythonでも検討しましたがカクついたりPCが違うと変な動きになったりとしてしまったのでHTMLに落ち着きました。

 

CSSなどの指定もできるのか試すと面白そうですね

それこそホームページのHTMLも作れたりしちゃうのでしょうね

 

 

 

ココまで 

 



私の活動案内

 

        

 

  

 

この記事の関連商品