モーダルウィンドウ ジェネレーター
CSSとJavaScriptで、表示位置、サイズ、開閉アニメーション、暗転オーバーレイの有無などを細かくカスタマイズできるモーダルウィンドウを生成します。
プレビュー
<!-- モーダル起動トリガーボタン -->
<button type="button" class="modal-open-btn">モーダルウィンドウを開く</button>
<!-- モーダルコンテナ (背面オーバーレイ) -->
<div class="modal-overlay" id="myModal">
<!-- モーダルウィンドウ本体 -->
<div class="modal-window">
<div class="modal-header">
<h3 class="modal-title">モーダルタイトル</h3>
<button type="button" class="modal-close-icon" aria-label="閉じる">×</button>
</div>
<div class="modal-body">
<p>ここにモーダルの本文が入ります。ボタンや画像などのコンテンツも配置可能です。</p>
</div>
<div class="modal-footer">
<button type="button" class="modal-btn btn-cancel">キャンセル</button>
<button type="button" class="modal-btn btn-ok">OK</button>
</div>
</div>
</div>
/* --- 起動ボタンのスタイル --- */
.modal-open-btn {
padding: 12px 24px;
font-size: 16px;
color: #fff;
background-color: #ff69b4;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.2s;
}
.modal-open-btn:hover {
background-color: #ff1493;
}
/* --- オーバーレイ (背景暗転) のスタイル --- */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: {{show_overlay_css}};
display: flex;
z-index: 9999;
box-sizing: border-box;
/* 表示位置のスタイル指定 */
{{position_styles}}
/* 初期状態は非表示 */
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease;
}
/* 表示時のスタイル */
.modal-overlay.is-active {
opacity: 1;
pointer-events: auto;
}
/* --- モーダルウィンドウ本体のスタイル --- */
.modal-window {
width: 90%;
max-width: {{modal_width}}px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
overflow: hidden;
/* アニメーション用初期状態 */
{{animation_init_styles}}
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
/* 表示(アクティブ)時のアニメーション状態 */
.modal-overlay.is-active .modal-window {
{{animation_active_styles}}
}
/* --- モーダル内部各パーツのスタイル --- */
.modal-header, .modal-body, .modal-footer {
padding: 15px 20px;
}
.modal-header {
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-title {
margin: 0;
font-size: 18px;
}
.modal-close-icon {
background: none;
border: none;
font-size: 22px;
cursor: pointer;
color: #999;
}
.modal-footer {
border-top: 1px solid #eee;
display: flex;
justify-content: flex-end;
gap: 10px;
}
.modal-btn {
padding: 6px 16px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
background: #fff;
}
.btn-ok {
background-color: #ff69b4;
color: #fff;
border-color: #ff69b4;
}
(() => {
// 必要な各要素の取得
const openBtn = document.querySelector('.modal-open-btn');
const overlay = document.querySelector('.modal-overlay');
const closeIcon = document.querySelector('.modal-close-icon');
const cancelBtn = document.querySelector('.btn-cancel');
const okBtn = document.querySelector('.btn-ok');
if (!openBtn || !overlay) return;
// モーダルを開く関数
const openModal = () => {
overlay.classList.add('is-active');
};
// モーダルを閉じる関数
const closeModal = () => {
overlay.classList.remove('is-active');
};
// イベントリスナーの登録
openBtn.addEventListener('click', openModal);
if (closeIcon) closeIcon.addEventListener('click', closeModal);
if (cancelBtn) cancelBtn.addEventListener('click', closeModal);
if (okBtn) okBtn.addEventListener('click', () => {
alert('OKがクリックされました');
closeModal();
});
// 背面オーバーレイのクリックで閉じるかどうかの制御
const closeOnOverlay = {{close_on_overlay}};
if (closeOnOverlay) {
overlay.addEventListener('click', (e) => {
// クリックされたのがウィンドウの外側(overlay自体)である場合のみ閉じる
if (e.target === overlay) {
closeModal();
}
});
}
})();
機能、使い方
- 起動ボタンをクリックすると、設定されたアニメーション効果(フェード、ズーム、スライド)を伴ってモーダルウィンドウが起動します。
- 表示位置を画面の「中央」「四隅」「上下中央」の全7箇所からフレキシブルに選択可能です。
- 背面オーバーレイをクリックした際の「閉じる」「閉じない」制御のほか、背景を暗く透過させるか、完全に透明にするかなどを柔軟に切り替えられます。
- OKボタン、キャンセルボタン、右上の「×」アイコンが最初から定義されており、すぐに実用可能な構造となっています。
コードの説明
opacityとpointer-events: noneを組み合わせて表示状態を管理し、モーダル非表示時にキーボードフォーカスやクリック操作が裏側の要素へ透過するように制御しています。
e.target === overlayを判定条件として設定することで、モーダル内部(.modal-window)をクリックしてもモーダルが閉じず、背景部分をクリックした際のみクローズする誤作動防止ロジックを実装しています。
transformとcubic-bezierイージング関数を組み合わせ、滑らかで洗練された拡大・縮小や滑り出しのモーション演出を行っています。