• 全画面の読み込み中表示
  • ページ内の画像(+任意の画像)の読み込みのハンドリング
  • 読み込み完了後、撤収アニメーション前後でコールバック呼び出しあり(引数の type プロパティで判別可能)
<div class="loader_01">
  <div class="loader_01--inner">
    <div class="loader_01--progress">
      <div class="loader_01--progress_bar"></div>
    </div>
    <div class="loader_01--percent"><span class="loader_01--percent_num">0</span>%</div>
  </div>
</div>
/* No context defined. */
  • Content:
    // =====================================================================================
    //
    // loader_01
    //
    // =====================================================================================
    
    @use 'scss/global' as g;
    
    // -------------------------------------------------------------------------------------
    // アニメーション
    // -------------------------------------------------------------------------------------
    @keyframes loader_01-remove_anim {
      0% {
        opacity: 1;
      }
      100% {
        opacity: 0;
      }
    }
    
    .loader_01-remove_anim {
      animation: loader_01-remove_anim 0.5s ease-out;
      animation-fill-mode: forwards;
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 >
    // -------------------------------------------------------------------------------------
    .loader_01 {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 2147483647;
      width: 100%;
      height: 100%;
      background: #f1f1f1;
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 > 天地中央枠 >
    // -------------------------------------------------------------------------------------
    .loader_01--inner {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 100px;
      text-align: center;
      transform: translate(-50%, -50%);
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 > 天地中央枠 > プログレスバー枠
    // -------------------------------------------------------------------------------------
    .loader_01--progress {
      width: 100%;
      height: 2px;
      margin: 0 0 10px;
      background: #fff;
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 > 天地中央枠 > プログレスバー枠 > 現在地バー
    // -------------------------------------------------------------------------------------
    .loader_01--progress_bar {
      width: 100%;
      height: 100%;
      background: #000;
      transform: scaleX(0);
      transform-origin: top left;
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 > 天地中央枠 > 進捗パーセント枠
    // -------------------------------------------------------------------------------------
    .loader_01--percent {
      font-size: 1rem;
    }
    
    // -------------------------------------------------------------------------------------
    // 全体枠 > 天地中央枠 > 進捗パーセント枠 > 数値
    // -------------------------------------------------------------------------------------
    .loader_01--percent_num {
      font-size: 1.6rem;
    }
    
  • URL: /components/raw/loader_01/_loader_01.scss
  • Filesystem Path: src/assets/_parts/components/loader/loader_01/_loader_01.scss
  • Size: 2.6 KB
  • Content:
    // =====================================================================================
    //
    // loading_01
    //  - このファイルを「src/js/components」に移動の上、呼び出してください
    //  - 呼び出しの際、引数でRoots名を指定してください
    //    - 必要に応じて、第二引数に読み込み完了後のコールバック関数を、第三引数に読み込みに追加したい画像パスの配列を指定してください
    //  - 記述例:
    //      import loader_01 from './components/loader_01';
    //      loader_01('loading', e => { console.log(e) }, ['images.png']);
    //
    // =====================================================================================
    
    import $ from 'jquery';
    import ImageLoader from '@/js/classes/ImageLoader';
    
    export default function (rootsName, callback = () => {}, images = []) {
      let windowLoaded = false;
      let progress = 0;
      let loadProgress = 0;
    
      let $loading;
      let $bar;
      let $percent;
    
      const event = {
        BEFORE_REMOVE: 'beforeRemove',
        AFTER_REMOVE: 'afterRemove',
      };
    
      $(window).on('load.loading', () => {
        windowLoaded = true;
      });
    
      // DOM Ready
      $(() => {
        $loading = $('.' + rootsName);
        $bar = $('.' + rootsName + '--progress_bar');
        $percent = $('.' + rootsName + '--percent_num');
    
        if ($loading.length === 0) {
          return;
        }
    
        const imageLoader = new ImageLoader();
        imageLoader.addEventListener(ImageLoader.event.PROGRESS, onLoadProgress);
        imageLoader.addEventListener(ImageLoader.event.COMPLETE, onLoadComplete);
    
        const imgList = document.images;
        for (const img of imgList) {
          imageLoader.addFile(img.src);
        }
        imageLoader.addFileList(images);
    
        imageLoader.start();
    
        window.requestAnimationFrame(onTick);
      });
    
      // ロード画面撤収処理
      function remove() {
        callback({
          type: event.BEFORE_REMOVE,
        });
    
        $loading
          .on('animationend', function () {
            $loading.remove();
            callback({
              type: event.AFTER_REMOVE,
            });
          })
          .addClass('loader_01-remove_anim');
      }
    
      // 毎フレーム処理。瞬間的にロードが完了してもアニメーションはさせる
      function onTick() {
        if (progress >= loadProgress) {
          progress = loadProgress;
        } else {
          progress += 3;
        }
    
        $bar.css({
          transform: 'scaleX(' + Math.min(progress / 100, 1) + ')',
        });
    
        $percent.text(Math.min(progress, 100));
    
        // 一応、windowのロード完了は待つ
        if (progress >= 100 && windowLoaded) {
          window.cancelAnimationFrame(onTick);
          remove();
        } else {
          window.requestAnimationFrame(onTick);
        }
      }
    
      // 画像の読み込みが1個完了するごとの処理。進捗(%)を更新
      function onLoadProgress(e) {
        loadProgress = e.percent;
      }
    
      // 画像読み込み完了後の処理。一応、進捗100%にしておく
      function onLoadComplete(e) {
        loadProgress = 100;
      }
    }
    
  • URL: /components/raw/loader_01/loader_01.js
  • Filesystem Path: src/assets/_parts/components/loader/loader_01/loader_01.js
  • Size: 3 KB