モーダルウインドウを画面の外側からスライドインさせ、閉じると反対側へスライドアウトさせる方法を紹介します。 開いたときと同じ方向へ戻るのではなく、画面を通り抜けるように移動するのが特徴です。
今回のデモでは、「モーダルを開く」ボタンをクリックすると、モーダルウインドウが画面左側から現れて中央で停止します。 ×ボタン・モーダルの外側・Escキーで閉じると、今度は画面右側へ移動して消えます。 フェード効果は使用せず、最初から最後まで不透明な状態でスライドします。
スライドする方向は、左から右だけでなく、右から左・上から下・下から上の合計4パターンに変更できます。 いずれも画面中央で停止し、閉じると進行方向の画面外へ抜ける動作です。 方向ごとの変更方法は、下記の「ファイル構成と設置方法」で説明します。
デモ
画面左側から中央へスライドインし、閉じると右側へスライドアウトする動作を確認できます。
「モーダルを開く」をクリックすると、画面左側から中央へスライドインします。×ボタン・モーダルの外側・Escキーで閉じると、画面右側へスライドアウトします。スライドする方向は、左右だけでなく上下にも変更できます。
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードの設定 -->
<meta charset="UTF-8">
<!-- スマートフォンなどの画面幅に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>左から右へスライドするモーダルウインドウ</title>
<!-- モーダルウインドウのCSSを読み込む -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- モーダルウインドウ全体 -->
<div class="pass-through-modal_wrap">
<!-- モーダルを開くボタン -->
<button class="pass-through-modal_open" id="pass-through-modal-open" type="button">
モーダルを開く
</button>
<!-- 左から入り、右へ抜けるモーダル -->
<div class="pass-through-modal_modal" id="pass-through-modal-modal" role="dialog" aria-modal="true" aria-hidden="true" aria-labelledby="pass-through-modal-title">
<!-- モーダルを閉じるボタン -->
<button class="pass-through-modal_close" id="pass-through-modal-close" type="button" aria-label="閉じる">
×
</button>
<!-- モーダルのタイトル -->
<div class="pass-through-modal_title" id="pass-through-modal-title">
モーダルウインドウ
</div>
<!-- モーダルの説明文 -->
<p class="pass-through-modal_text">
画面左側からスライドインして中央で停止します。閉じると画面右側へスライドアウトします。
</p>
</div>
</div>
<!-- モーダルの開閉処理を読み込む -->
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードの設定 -->
<meta charset="UTF-8">
<!-- スマートフォンなどの画面幅に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>右から左へスライドするモーダルウインドウ</title>
<!-- モーダルウインドウのCSSを読み込む -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- モーダルウインドウ全体 -->
<div class="pass-through-modal_wrap">
<!-- モーダルを開くボタン -->
<button class="pass-through-modal_open" id="pass-through-modal-open" type="button">
モーダルを開く
</button>
<!-- 右から入り、左へ抜けるモーダル -->
<div class="pass-through-modal_modal" id="pass-through-modal-modal" role="dialog" aria-modal="true" aria-hidden="true" aria-labelledby="pass-through-modal-title">
<!-- モーダルを閉じるボタン -->
<button class="pass-through-modal_close" id="pass-through-modal-close" type="button" aria-label="閉じる">
×
</button>
<!-- モーダルのタイトル -->
<div class="pass-through-modal_title" id="pass-through-modal-title">
モーダルウインドウ
</div>
<!-- モーダルの説明文 -->
<p class="pass-through-modal_text">
画面右側からスライドインして中央で停止します。閉じると画面左側へスライドアウトします。
</p>
</div>
</div>
<!-- モーダルの開閉処理を読み込む -->
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードの設定 -->
<meta charset="UTF-8">
<!-- スマートフォンなどの画面幅に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>上から下へスライドするモーダルウインドウ</title>
<!-- モーダルウインドウのCSSを読み込む -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- モーダルウインドウ全体 -->
<div class="pass-through-modal_wrap">
<!-- モーダルを開くボタン -->
<button class="pass-through-modal_open" id="pass-through-modal-open" type="button">
モーダルを開く
</button>
<!-- 上から入り、下へ抜けるモーダル -->
<div class="pass-through-modal_modal" id="pass-through-modal-modal" role="dialog" aria-modal="true" aria-hidden="true" aria-labelledby="pass-through-modal-title">
<!-- モーダルを閉じるボタン -->
<button class="pass-through-modal_close" id="pass-through-modal-close" type="button" aria-label="閉じる">
×
</button>
<!-- モーダルのタイトル -->
<div class="pass-through-modal_title" id="pass-through-modal-title">
モーダルウインドウ
</div>
<!-- モーダルの説明文 -->
<p class="pass-through-modal_text">
画面上側からスライドインして中央で停止します。閉じると画面下側へスライドアウトします。
</p>
</div>
</div>
<!-- モーダルの開閉処理を読み込む -->
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードの設定 -->
<meta charset="UTF-8">
<!-- スマートフォンなどの画面幅に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>下から上へスライドするモーダルウインドウ</title>
<!-- モーダルウインドウのCSSを読み込む -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- モーダルウインドウ全体 -->
<div class="pass-through-modal_wrap">
<!-- モーダルを開くボタン -->
<button class="pass-through-modal_open" id="pass-through-modal-open" type="button">
モーダルを開く
</button>
<!-- 下から入り、上へ抜けるモーダル -->
<div class="pass-through-modal_modal" id="pass-through-modal-modal" role="dialog" aria-modal="true" aria-hidden="true" aria-labelledby="pass-through-modal-title">
<!-- モーダルを閉じるボタン -->
<button class="pass-through-modal_close" id="pass-through-modal-close" type="button" aria-label="閉じる">
×
</button>
<!-- モーダルのタイトル -->
<div class="pass-through-modal_title" id="pass-through-modal-title">
モーダルウインドウ
</div>
<!-- モーダルの説明文 -->
<p class="pass-through-modal_text">
画面下側からスライドインして中央で停止します。閉じると画面上側へスライドアウトします。
</p>
</div>
</div>
<!-- モーダルの開閉処理を読み込む -->
<script src="script.js"></script>
</body>
</html>
コードをコピーする場合 使用するコードのタブを選び、「このタブをコピー」をクリックすると、現在表示中のコードをコピーできます。 サンプル一式を取得する場合 HTML・CSSなどすべてのファイルが必要な場合は、「サンプル一式をダウンロード」をクリックしてください。
Q ダウンロードしたけど、これをネット上で動かすにはどうするの?
Googleアカウントがあれば、InfinityFreeで無料サーバーを作成して、ダウンロードしたサンプルをネット上で動かせます。
InfinityFreeを初めて利用する場合は①から、すでにサーバーを作成している場合は②へ進んでください。
ファイル構成と設置方法
今回のサンプルは、モーダルウインドウが画面の外側から中央へスライドインし、閉じると反対側へスライドアウトする仕組みです。
左から右・右から左・上から下・下から上の4種類を用意しており、それぞれ独立したサンプルとしてダウンロードできます。
どの方向もHTML・CSS・JavaScriptの3ファイルで構成されています。
HTMLでは開閉ボタンとモーダル本体を配置し、CSSでは移動方向・表示位置・アニメーション速度を設定しています。
JavaScriptでは開閉操作と移動完了後の初期位置への復帰を制御しています。
sample/
├── index.html
├── style.css
└── script.js
各ファイルの役割
index.htmlには、モーダルを開くボタン、閉じるための×ボタン、タイトル、説明文を配置しています。
4種類とも基本構造は同じですが、モーダル内の説明文は移動方向に合わせて異なります。
style.cssでは、モーダルの横幅・余白・背景色・表示位置に加え、スライドインとスライドアウトの移動方向を設定しています。
4種類の主な違いは、transformで指定する移動位置です。
script.jsでは、開く・閉じる操作、アニメーション中の連続操作の防止、閉じた後の初期位置への復帰を行います。
移動方向はCSSで決めているため、JavaScriptの基本処理は4種類とも共通です。
設置方法・使い方
使用したい方向のコードカードから「サンプル一式をダウンロード」をクリックし、ZIPファイルを保存して展開してください。
各ZIPには、その方向専用のindex.html・style.css・script.jsが収録されます。
3つのファイルをsampleフォルダ内の同じ階層に配置し、index.htmlをブラウザで開くと動作します。
4種類をすべて設置する必要はなく、使用したい方向のサンプルを1つ選択してください。
「モーダルを開く」をクリックすると、指定した方向からモーダルが画面中央へ移動します。
×ボタン・モーダルの外側・Escキーのいずれかで閉じると、反対側へ移動します。
スライドアウト完了後は次回の表示に備えて初期位置へ戻ります。
4方向の移動を決める仕組み
モーダルの移動方向は、style.cssの.pass-through-modal_modalと
.pass-through-modal_modal.is-exitingに設定したtransformで決まります。
通常の.pass-through-modal_modalは開く前の初期位置、
.is-exitingは閉じるときの移動先です。
4種類のサンプルでは、この2つの位置を変更しています。
① 左から右へスライドする場合
画面左側の外を初期位置とし、モーダルを閉じると画面右側の外へ移動します。
style.cssの該当部分は次のとおりです。
.pass-through-modal_modal {
transform: translate(calc(-50% - 100vw), -50%); /* 画面左側の外へ配置する */
}
.pass-through-modal_modal.is-exiting {
transform: translate(calc(-50% + 100vw), -50%); /* 画面右側の外へ移動する */
}
100vwは画面の横幅に相当する値です。
初期位置では横方向へ-100vwを加えて左側へ移動し、
閉じるときは+100vwを加えて右側へ移動します。
② 右から左へスライドする場合
左から右への動作とは反対に、画面右側の外を初期位置とし、閉じると画面左側へ移動します。
.pass-through-modal_modal {
transform: translate(calc(-50% + 100vw), -50%); /* 画面右側の外へ配置する */
}
.pass-through-modal_modal.is-exiting {
transform: translate(calc(-50% - 100vw), -50%); /* 画面左側の外へ移動する */
}
初期位置と終了位置で100vwの符号を入れ替えることで、
右側から入って左側へ抜ける動作になります。
③ 上から下へスライドする場合
上下方向では、横方向ではなく縦方向の移動量を変更します。
画面上側の外を初期位置とし、閉じると画面下側へ移動します。
.pass-through-modal_modal {
transform: translate(-50%, calc(-50% - 100vh)); /* 画面上側の外へ配置する */
}
.pass-through-modal_modal.is-exiting {
transform: translate(-50%, calc(-50% + 100vh)); /* 画面下側の外へ移動する */
}
100vhは画面の高さに相当する値です。
縦方向へ-100vhを加えると上側へ、
+100vhを加えると下側へ移動します。
④ 下から上へスライドする場合
画面下側の外を初期位置とし、閉じると画面上側へ移動します。
上から下への動作とは、縦方向の移動量の符号が反対になります。
.pass-through-modal_modal {
transform: translate(-50%, calc(-50% + 100vh)); /* 画面下側の外へ配置する */
}
.pass-through-modal_modal.is-exiting {
transform: translate(-50%, calc(-50% - 100vh)); /* 画面上側の外へ移動する */
}
4種類とも、画面中央で停止する位置は共通です。
横方向・縦方向の移動量を変更しても、中央に表示するときは同じtransformを使用しています。
画面中央で停止する仕組み
モーダルを開くと、JavaScriptによってis-activeクラスが追加されます。
style.cssでは、このクラスが付いたときの表示位置を次のように設定しています。
/* =========================================
開いた状態
左側から画面中央へスライドイン
========================================= */
.pass-through-modal_modal.is-active {
transform: translate(-50%, -50%); /* モーダルを画面中央に配置する */
visibility: visible; /* モーダルを表示する */
pointer-events: auto; /* モーダル内の操作を有効にする */
transition: transform 1s ease, visibility 0s; /* 1秒で中央へ移動する */
}
translate(-50%, -50%)は、モーダル自身の横幅と高さの半分だけ位置を調整する指定です。
本体に設定したtop: 50%;とleft: 50%;を組み合わせることで、画面中央に配置できます。
visibility: visible;でモーダルを表示し、
pointer-events: auto;でボタンなどの操作を有効にしています。
この設定は4種類とも共通です。
スライド速度と表示状態の設定
スライドの速度は、style.cssのtransitionで設定しています。
左から右へ移動するサンプルでは、次の指定を使用しています。
transition: transform 1s ease, visibility 0s linear 1s; /* 移動は1秒。非表示は移動完了後に切り替える */
transform 1s easeは、位置の移動を1秒かけて行う指定です。
1sを大きくすると移動が遅くなり、小さくすると速くなります。
visibility 0s linear 1sの最後の1sは、非表示への切り替えを遅らせる時間です。
移動速度を変更する場合は、この待ち時間も移動時間に合わせてください。
また、.is-activeと.is-exitingにある
transform 1s easeも同じ時間に変更します。
今回はopacityによる透明度のアニメーションを使用していないため、
モーダルは不透明な状態のままスライドします。
開閉操作を制御するJavaScript
script.jsでは、openModal()でモーダルを開き、
closeModal()で閉じる処理を実行します。
開閉状態はisOpen、アニメーションの実行状態はisAnimatingで管理しています。
次のコードは4種類に共通する処理です。
/* =========================================
モーダルを開く
========================================= */
function openModal() {
// 表示中またはアニメーション中は実行しない
if (isOpen || isAnimating) {
return;
}
isOpen = true;
isAnimating = true;
// 左側の初期位置から中央へ移動
modal.classList.add("is-active");
modal.setAttribute("aria-hidden", "false");
}
/* =========================================
モーダルを閉じる
========================================= */
function closeModal() {
// 非表示中またはアニメーション中は実行しない
if (!isOpen || isAnimating) {
return;
}
isOpen = false;
isAnimating = true;
// 中央表示を解除し、右側への移動を開始
modal.classList.remove("is-active");
modal.classList.add("is-exiting");
}
openModal()では、is-activeクラスを追加することで、
CSSの中央位置への移動を開始します。
closeModal()ではis-activeを解除して
is-exitingを追加し、CSSで指定した終了位置へ移動させます。
isAnimatingがtrueの間は次の操作を受け付けないため、
スライド途中に開閉ボタンを繰り返し押しても、移動処理が重複しないようになっています。
右から左・上から下・下から上のサンプルでも、処理の流れは同じです。
スライドアウト後に初期位置へ戻す仕組み
モーダルが反対側へ抜けた後は、次に開くための初期位置へ戻す必要があります。
script.jsでは、transitionendイベントを使用して移動の終了を検知しています。
/* =========================================
アニメーション終了後の処理
========================================= */
modal.addEventListener("transitionend", function(event) {
// モーダル本体の移動完了だけを処理
if (event.target !== modal || event.propertyName !== "transform") {
return;
}
// 開くアニメーションが終了した場合
if (modal.classList.contains("is-active")) {
isAnimating = false;
closeButton.focus();
return;
}
// 閉じるアニメーションが終了した場合
if (modal.classList.contains("is-exiting")) {
// アニメーションなしで左側の初期位置へ戻す
modal.classList.add("is-resetting");
modal.classList.remove("is-exiting");
modal.setAttribute("aria-hidden", "true");
// 初期位置のスタイルを確定させる
void modal.offsetWidth;
// 次回の開閉に備えて復帰用クラスを解除
modal.classList.remove("is-resetting");
isAnimating = false;
openButton.focus();
}
});
event.propertyName !== "transform"によって、位置の移動が完了したときだけ処理を進めます。
開く動作が完了した場合はisAnimatingをfalseに戻し、
閉じるボタンへフォーカスを移動します。
閉じる動作が完了した場合はis-resettingクラスを追加し、
is-exitingを解除します。
このときis-resettingによってCSSのアニメーションを一時的に無効化するため、
画面の反対側から初期位置へ戻る動きは表示されません。
void modal.offsetWidth;で初期位置のスタイルを確定させた後、
is-resettingを解除して次回の開閉に備えます。
×ボタン・外側クリック・Escキーで閉じる仕組み
モーダルを閉じる操作には、いずれもcloseModal()を使用しています。
×ボタンのクリックに加えて、モーダルの外側をクリックした場合やEscキーを押した場合にも閉じる処理を実行します。
/* =========================================
ボタンによる開閉
========================================= */
// 開くボタンをクリックしたとき
openButton.addEventListener("click", openModal);
// 閉じるボタンをクリックしたとき
closeButton.addEventListener("click", closeModal);
/* =========================================
モーダルの外側をクリックして閉じる
========================================= */
document.addEventListener("pointerdown", function(event) {
// 閉じている場合やアニメーション中は処理しない
if (!isOpen || isAnimating) {
return;
}
// モーダル内と開くボタンは対象外
if (modal.contains(event.target) || openButton.contains(event.target)) {
return;
}
// モーダルの外側がクリックされたら閉じる
closeModal();
});
/* =========================================
Escキーで閉じる
========================================= */
document.addEventListener("keydown", function(event) {
// Escキーが押された場合に閉じる
if (event.key === "Escape") {
closeModal();
}
});
modal.contains(event.target)でクリック位置がモーダル内かどうかを判定しています。
モーダル内や開くボタンをクリックした場合は閉じず、それ以外の場所をクリックするとcloseModal()を実行します。
Escキーについてはevent.key === "Escape"で判定しています。
これらの閉じる操作は4方向すべてで共通です。
モーダルの横幅とレスポンシブ対応
style.cssでは、モーダルの横幅をwidth: calc(100% - 40px);、
最大横幅をmax-width: 420px;に設定しています。
これにより、広い画面では最大420pxまで表示し、狭い画面では左右に余裕を残した横幅になります。
最大横幅を広げたい場合は420pxを大きくし、コンパクトにしたい場合は小さくしてください。
width: calc(100% - 40px); /* 画面の左右に合計40pxの余裕を確保する */
max-width: 420px; /* モーダルの最大横幅 */
さらに、画面幅が640px以下の場合は、モーダル内の余白と文字サイズを小さくしています。
4種類とも同じレスポンシブ設定を使用しています。
/* =========================================
スマートフォン対応
========================================= */
@media (max-width: 640px) {
/* モーダル内の余白を小さくする */
.pass-through-modal_modal {
padding: 28px 20px; /* 上下28px・左右20pxの内側余白 */
}
/* タイトルの文字サイズを小さくする */
.pass-through-modal_title {
font-size: 20px; /* スマートフォン用のタイトルサイズ */
}
/* 説明文の文字サイズを小さくする */
.pass-through-modal_text {
font-size: 15px; /* スマートフォン用の説明文サイズ */
}
}
PCではモーダル内の余白を上下32px・左右28pxにしていますが、
640px以下では上下28px・左右20pxに変更します。
タイトルは22pxから20px、説明文は16pxから15pxへ縮小しています。
スマートフォンで余白や文字サイズを変更したい場合は、
@media (max-width: 640px)内の各数値を調整してください。
コメント