画面遷移-背景色が伸びる(上から下)-

動きを実現する仕組み

jQuery でローディング画面をフェードアウトし、body タグにクラスを付与。 クラスが付与されたらCSS で画面遷移の動きを表示した後、時間を遅らせてメインコンテンツを出現させる。


STEP1 HTMLコード


① head終了タグ直前に自作のCSSを読み込みます。*wordpressでfunctionにて設定の場合は省略

<head>

<link rel="stylesheet" type="text/css" href="css/5-1-14.css">

</head>

② body内にリンク元のHTMLとリンク先のHTMLを記載します。

<body>
 
 <div id="splash">
  <div id="splash-logo">ローディング</div>
 <!--/splash--></div>

 <div class="splashbg"></div><!---画面遷移用-->

  <header id="header">
   <h1>Logo</h1>
   <nav id="g-nav">
     <ul>
       <li><a href="#area-1">Area1</a></li>
       <li><a href="#area-2">Area2</a></li>
       <li><a href="#area-3">Area3</a></li>
       <li><a href="#area-4">Area4</a></li>
     </ul>
   </nav>
 </header>

 <main>

   <section class="scroll-point" id="area-1">
     <h2>Area 1</h2>
     <p>内容が入ります。</p>
   <!--/area1--></section>

   <section class="scroll-point" id="area-2">
     <h2>Area 2</h2>
     <p>内容が入ります。</p>
   <!--/area2--></section>

   <section class="scroll-point" id="area-3">
     <h2>Area 3</h2>
     <p>内容が入ります。</p>
   <!--/area3--></section>

   <section class="scroll-point" id="area-4">
     <h2>Area 4</h2>
     <p>内容が入ります。</p>
   <!--/area4--></section>

 <!--/main--></main>

 <footer id="footer">
   <small>© copyright.</small> 
 </footer>

</body>

③ body 終了タグ直前に jQuery、動きを制御する自作のJS の2 つを読み込みます。*wordpressでfunctionにて設定の場合は省略

  <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  <!--自作のJS-->
  <script src="js/5-1-14.js"></script>
</body>

④ WordpressでのfunctionでのCDNとファイルの読み込みは下記より。

//CDN形式のCSSとJSの読み込み
function add_my_files() {
  wp_enqueue_style(
  'fontawesome4.7',  //$handle
  '//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'  //$src
  );
  wp_enqueue_style(
    'fontawesome5.15.4',  //$handle
    '//use.fontawesome.com/releases/v5.15.4/css/all.css'  //$src
    );
  wp_enqueue_style(
    'fontawesome6.5.1',  //$handle
    '//cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css'  //$src
    );
  wp_enqueue_script(
    'jquery3.1.1',  //$handle
    '//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js',  //$src
    array(),  //$deps
    null,  //$ver
    true  //$in_footer
    );
}
add_action( 'wp_enqueue_scripts', 'add_my_files' );
//CSSとJSの読み込み
function my_enqueue_scripts()
{
  $version = wp_get_theme()->get( 'Version' );
  wp_enqueue_style('index-style', get_template_directory_uri() . '/css/index.css', array(), $version);
  wp_enqueue_style('singular-style', get_template_directory_uri() . '/css/singular.css', array(), $version);
  wp_enqueue_style('content-style', get_template_directory_uri() . '/css/content.css', array(), $version);
  wp_enqueue_style('archive-style', get_template_directory_uri() . '/css/archive.css', array(), $version);
  wp_enqueue_style('comments-style', get_template_directory_uri() . '/css/comments.css', array(), $version);
  wp_enqueue_style('404-style', get_template_directory_uri() . '/css/404.css', array(), $version);
  wp_enqueue_style('search-form-style', get_template_directory_uri() . '/css/searchform.css', array(), $version);
  wp_enqueue_style('search-style', get_template_directory_uri() . '/css/search.css', array(), $version);
  wp_enqueue_style('sidebar-style', get_template_directory_uri() . '/css/sidebar.css', array(), $version);
  wp_enqueue_style('header-style', get_template_directory_uri() . '/css/header.css', array(), $version);
  wp_enqueue_style('footer-style', get_template_directory_uri() . '/css/footer.css', array(), $version);
  wp_enqueue_style('navi-style', get_template_directory_uri() . '/css/navi.css', array(), $version);
  wp_enqueue_style('navi-accordion-style', get_template_directory_uri() . '/css/navi-accordion.css', array(), $version);
  wp_enqueue_style('catego-navi-style', get_template_directory_uri() . '/css/catego-navi.css', array(), $version);
  wp_enqueue_style('button-style', get_template_directory_uri() . '/css/button.css', array(), $version);
  wp_enqueue_script('navi-script', get_template_directory_uri() . '/js/navi.js', array('jquery'), $version, true);
  wp_enqueue_script('catego-navi-script', get_template_directory_uri() . '/js/catego-navi.js', array('jquery'), $version, true);
  wp_enqueue_script('page-top-script', get_template_directory_uri() . '/js/page-top.js', array('jquery'), $version, true);
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

⑤ header.phpの>head>内で下記を使って読み込み。

<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>"><!--スタイルシートの呼び出し-->
<?php wp_head(); ?><!--システム・プラグイン用-->


CSSでコーディング


STEP2 cssコード


/*========= ローディング画面のためのCSS ===============*/
#splash {
	position: fixed;
	width: 100%;
	height: 100%;
	background: #333;
	z-index: 9999999;
	text-align:center;
	color:#fff;
}

#splash-logo {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}

/*========= 画面遷移のためのCSS ===============*/

/*画面遷移アニメーション*/
.splashbg{
    display: none;
}

/*bodyにappearクラスがついたら出現*/
body.appear .splashbg{
    display: block;
    content: "";
    position:fixed;
	z-index: 999;
    width: 100%;
    height: 100vh;
    top: 0;
	left: 0;
    transform: scaleY(0);
    background-color: #333;/*伸びる背景色の設定*/
	animation-name:PageAnime;
	animation-duration:1.2s;
	animation-timing-function:ease-in-out;
	animation-fill-mode:forwards;
}

@keyframes PageAnime{
	0% {
		transform-origin:top;
		transform:scaleY(0);
	}
	50% {
		transform-origin:top;
		transform:scaleY(1);
	}
	50.001% {
		transform-origin:bottom;
	}
	100% {
		transform-origin:bottom;
		transform:scaleY(0);
	}
}

/*画面遷移の後現れるコンテンツ設定*/
#header, main, #footer{
	opacity: 0;/*はじめは透過0に*/
}

/*bodyにappearクラスがついたら出現*/
body.appear #header, main, #footer{
	animation-name:PageAnimeAppear;
	animation-duration:1s;
	animation-delay: 0.8s;
	animation-fill-mode:forwards;
	opacity: 0;
}

/*wordpressの場合は下記のように*/
body.appear #header,body.appear main,body.appear #footer{
	animation-name:PageAnimeAppear;
	animation-duration:1s;
	animation-delay: 0.8s;
	animation-fill-mode:forwards;
	opacity: 0;
}

@keyframes PageAnimeAppear{
	0% {
	opacity: 0;
	}
	100% {
	opacity: 1;
}
}



/*========= 現在地表示のためのCSS ===============*/

#header{
  position: fixed;/*header固定*/
  height: 70px;/*Headerの高さ設定*/
  width:100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background:#333;
  color:#fff;
  text-align: center;
}


/* 以下はレイアウトのための CSS*/

nav ul{
    list-style: none;
    display: flex;
}

nav ul li a{
    display: block;
    text-decoration: none;
    color: #666;
    padding:10px;
    transition:all 0.3s;
}

nav ul li.current a,
nav ul li a:hover{
    color:#fff;
}

@media screen and (max-width:768px) {
    nav ul li a:hover{
    color: #666;
    }

    nav ul li.current a {
    color:#fff;
    }
}

section{
    padding: 300px 0;
}

footer{
    padding: 50px 0;
}


Jqueryを使ってJSコード作成


STEP3 JSコード


$(window).on('load',function(){
$("#splash-logo").delay(1200).fadeOut('slow');//ロゴを1.2秒でフェードアウトする記述

//=====ここからローディングエリア(splashエリア)を1.5秒でフェードアウトした後に動かしたいJSをまとめる
$("#splash").delay(1500).fadeOut('slow',function(){//ローディングエリア(splashエリア)を1.5秒でフェードアウトする記述

$('body').addClass('appear');//フェードアウト後bodyにappearクラス付与

});
//=====ここまでローディングエリア(splashエリア)を1.5秒でフェードアウトした後に動かしたいJSをまとめる

//=====ここから背景が伸びた後に動かしたいJSをまとめたい場合は
$('.splashbg').on('animationend', function() { 
//この中に動かしたいJSを記載
});
//=====ここまで背景が伸びた後に動かしたいJSをまとめる

});

動作確認は下記から


See the Pen Untitled by K O (@K-O-the-builder) on CodePen.