次のページに進む際、トップに戻る際、ページ遷移時にアニメーションがあると、サイトの印象はかなり強くなりますよね。
概要
この記事では既にページ遷移時のアニメーションを紹介されている記事を参考に、当サイトでカスタマイズしたデザインを適用したものを紹介します。
下記の記事を参考に作成しました。ありがとうございます!

専用のページ遷移ライブラリを追加しない、シンプルな方法です。この記事の実装ではjQueryを使用します。
デモ
まずはデモページをご覧ください。
こんな感じのカーテンがピロピローんみたいなアニメーションを実装します。
HTML
<body class="fo">
<div class="fade_group">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="fade_group2">
<div></div>
<div></div>
</div>
</body>
bodyにクラスを付け、後でJavascriptでこのクラスを取ったり付けたりします。
fade_group この要素は重ねる枚数によって調整してください。
枚数によっては疑似要素(before,after)の方がスマートだと思います。
Cocoonの場合
ワードプレスで直接bodyタグを編集できる場合は、bodyタグに以下をのように変更すればいいのですが、
<body <?php body_class('fo'); ?>>
なかなかbodyタグをいじれるテーマはないというか、いじるのは気が引けます。
下記の記事では、cocoonのボディ要素にクラスを付ける方法が紹介されています。

以下を子テーマのfunctions.phpに追加すると、cocoonには関数が用意されているので、body要素にクラスが追加されます。
add_filter( 'body_class_additional', function( $classes ) {$classes[] = 'fo';return $classes;});
後は、cocoonの子テーマのtmp-user/body-top-insert.php に、以下追加するだけ。
<div class="fade_group">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="fade_group2">
<div></div>
<div></div>
</div>
CSS
.fade_group div ,
.fade_group2 div {
content: '';
position: fixed;
top: 0;
left: -100vw;
width: 100vw;
height: 100%;
pointer-events: none;
}
.fade_group div {
z-index: 9999;
transition: .6s;
}
.fade_group2 div {
z-index: 9998;
transition: .8s;
}
.fade_group div:nth-of-type(1) {
background: rgba(175,151,94,.3);
}
.fade_group div:nth-of-type(2) {
background: rgba(175,151,94,.5);
transition-delay:.1s;
}
.fade_group div:nth-of-type(3) {
background: rgba(175,151,94,.7);
transition-delay:.2s;
}
.fade_group div:nth-of-type(4) {
background: rgba(50,50,50,1);
transition-delay:.3s;
}
.fade_group2 div:nth-of-type(1) {
background: rgba(175,151,94,.8);
transition-delay: .2s;
}
.fade_group2 div:nth-of-type(2) {
background: rgba(175,151,94,.8);
transition-delay: .3s;
}
body.fo .fade_group div{
left: 0;
}
body.fo .fade_group2 div{
left: 100vw;
}
Javascript(jQuery)
JavaScriptはDigiPressさんの記事を参考に、クラス名をCSSに合わせ、修飾キー付きクリックやページ内リンクの通常動作を保つように調整しています。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" crossorigin="anonymous"></script>
すでにjQueryを読み込んでいる場合は追加不要です。以下のコードはjQueryの読み込み後に設置してください。通常の同一サイト内リンクをクリックした場合だけ演出を付け、Ctrl・Commandなどの修飾キー付きクリック、別タブ、ダウンロード、ページ内リンクはブラウザ本来の動作を保ちます。
jQuery(function ($) {
function showPage() {
$('body').removeClass('fo');
}
if (document.readyState === 'complete') {
showPage();
} else {
$(window).on('load', showPage);
}
$(window).on('pageshow', showPage);
$(document).on('click', 'a[href]', function (event) {
if (event.isDefaultPrevented() || event.button !== 0 ||
event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
return;
}
const target = (this.getAttribute('target') || '').toLowerCase();
const href = (this.getAttribute('href') || '').trim();
if ((target && target !== '_self') || this.hasAttribute('download') || !href) {
return;
}
const destination = new URL(href, window.location.href);
if (!/^https?:$/.test(destination.protocol) ||
destination.origin !== window.location.origin ||
(destination.pathname === window.location.pathname &&
destination.search === window.location.search)) {
return;
}
event.preventDefault();
$('body').addClass('fo');
window.setTimeout(function () {
window.location.assign(destination.href);
}, 1100);
});
});
以上です。表示時も遷移時も、CSSと同じfoクラスを使います。遷移の待ち時間は、この例のCSSの最長時間に合わせて1100ミリ秒にしています。ブラウザの「戻る」で復元された際も、覆いが残らないようにしています。
ページ遷移時アニメーションまとめ
当ブログでも追加しようと思ったのですが、すこしクドイというか、邪魔すぎたので、実装は辞めましたww
せっかくなので、記事とデモページに残した形です。
仕組みは簡単ですし、cssアニメーションでいくらでもカスタマイズできるのが、面白いです。
クラスが付いたり消えたりで表示させてるので、応用もしやすいと思います。



コメント