フェード+スライド演出で自然に開閉するモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
html/css/js
フェード+スライド演出で自然に開閉するモーダルウインドウの作り方
HTML・CSS・JavaScriptを使って、フェードとスライドアニメーションを組み合わせたモーダルウインドウを作る方法を紹介します。このサンプルでは、ボタンをクリックするとモーダルウインドウがゆっくりフェードインしながらスライド表示され、閉じる際も自然なアニメーションで非表示になります。お知らせ画面、確認ダイアログ、設定画面など、滑らかな動きのあるモーダルUIを実装したい場合に利用できます。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
デモ
フェード+スライド演出のモーダルウインドウ
ボタンをクリックすると、モーダルがゆっくりフェード+スライド表示されます。
モーダルウインドウ
フェードインとスライドを組み合わせて、自然な開閉アニメーションを表現しています。
コードをコピーして使おう!
<!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="fade-slide-modal-window_wrap">
<!-- タイトル -->
<div class="fade-slide-modal-window_title">
フェード+スライド演出のモーダルウインドウ
</div>
<!-- 説明文 -->
<p class="fade-slide-modal-window_text">
ボタンをクリックすると、モーダルがゆっくりフェード+スライド表示されます。
</p>
<!-- モーダルを開くボタン -->
<button class="fade-slide-modal-window_open" type="button" data-modal-open>
モーダルを開く
</button>
</div>
<!-- モーダル背景 -->
<div class="fade-slide-modal-window_overlay" data-modal-overlay aria-hidden="true">
<!-- モーダル本体 -->
<div class="fade-slide-modal-window_modal" role="dialog" aria-modal="true">
<!-- モーダルタイトル -->
<div class="fade-slide-modal-window_modal-title">
モーダルウインドウ
</div>
<!-- モーダル説明文 -->
<p class="fade-slide-modal-window_modal-text">
フェードインとスライドを組み合わせて、自然な開閉アニメーションを表現しています。
</p>
<!-- 閉じるボタン -->
<button class="fade-slide-modal-window_close" type="button" data-modal-close>
閉じる
</button>
</div>
</div>
<!-- JavaScriptファイルを読み込み -->
<script src="script.js"></script>
</body>
</html>
/* ページ全体の設定 */
body{
margin:0;
font-family:sans-serif;
background:#ffffff;
}
/* デモ全体を囲むエリア */
.fade-slide-modal-window_wrap{
max-width:700px;
margin:40px auto;
padding:32px;
border:1px solid #e5e7eb;
border-radius:18px;
background:#f8fafc;
text-align:center;
}
/* タイトル */
.fade-slide-modal-window_title{
font-size:22px;
font-weight:700;
margin-bottom:12px;
}
/* 説明文 */
.fade-slide-modal-window_text{
margin-bottom:24px;
color:#4b5563;
}
/* モーダルを開くボタン */
.fade-slide-modal-window_open{
padding:12px 24px;
border:none;
border-radius:8px;
background:#0b6bff;
color:#ffffff;
font-weight:700;
cursor:pointer;
transition:opacity .3s ease,transform .3s ease;
}
/* モーダルを開くボタンのホバー */
.fade-slide-modal-window_open:hover{
opacity:.9;
transform:translateY(-2px);
}
/* モーダル背景 */
.fade-slide-modal-window_overlay{
position:fixed;
inset:0;
z-index:9999;
display:flex;
align-items:center;
justify-content:center;
padding:20px;
box-sizing:border-box;
background:rgba(0,0,0,.5);
opacity:0;
visibility:hidden;
pointer-events:none;
transition:opacity 1.2s ease-in-out,visibility 0s linear 1.2s;
}
/* モーダル表示時 */
.fade-slide-modal-window_overlay.is-active{
opacity:1;
visibility:visible;
pointer-events:auto;
transition:opacity 1.2s ease-in-out;
}
/* モーダル本体 */
.fade-slide-modal-window_modal{
width:100%;
max-width:520px;
padding:32px;
border-radius:18px;
background:#ffffff;
box-sizing:border-box;
text-align:left;
box-shadow:0 20px 40px rgba(0,0,0,.25);
transform:translateY(40px);
opacity:0;
transition:transform 1s cubic-bezier(.19,1,.22,1),opacity 1s ease-in-out;
}
/* モーダル本体の表示時 */
.fade-slide-modal-window_overlay.is-active .fade-slide-modal-window_modal{
transform:translateY(0);
opacity:1;
transition-delay:.12s;
}
/* モーダルの見出し */
.fade-slide-modal-window_modal-title{
font-size:24px;
font-weight:700;
margin-bottom:12px;
color:#111827;
}
/* モーダル内の説明文 */
.fade-slide-modal-window_modal-text{
margin-bottom:24px;
color:#4b5563;
line-height:1.8;
}
/* 閉じるボタン */
.fade-slide-modal-window_close{
padding:10px 20px;
border:none;
border-radius:8px;
background:#111827;
color:#ffffff;
font-weight:700;
cursor:pointer;
}
/* モーダル表示中の背景スクロール抑止 */
body.fade-slide-modal-window_lock{
overflow:hidden;
}
// モーダルを開くボタンを取得
const modalOpen=document.querySelector('[data-modal-open]');
// モーダル背景を取得
const modalOverlay=document.querySelector('[data-modal-overlay]');
// モーダルを閉じるボタンを取得
const modalClose=document.querySelector('[data-modal-close]');
// モーダルを開くボタンをクリックしたらモーダルを表示
modalOpen.addEventListener('click',function(){
modalOverlay.classList.add('is-active');
modalOverlay.setAttribute('aria-hidden','false');
document.body.classList.add('fade-slide-modal-window_lock');
});
// モーダルを閉じる処理
function closeModal(){
modalOverlay.classList.remove('is-active');
modalOverlay.setAttribute('aria-hidden','true');
document.body.classList.remove('fade-slide-modal-window_lock');
}
// 閉じるボタンをクリックしたらモーダルを閉じる
modalClose.addEventListener('click',closeModal);
// 背景部分をクリックしたらモーダルを閉じる
modalOverlay.addEventListener('click',function(event){
if(event.target===modalOverlay){
closeModal();
}
});
// Escapeキーを押したらモーダルを閉じる
window.addEventListener('keydown',function(event){
if(event.key==='Escape' && modalOverlay.classList.contains('is-active')){
closeModal();
}
});
ファイル構成と設置方法
このサンプルでは、HTML・CSS・JavaScriptをそれぞれ別ファイルに分けて使用します。index.html、style.css、script.js を同じフォルダに配置してください。
sample/
├── index.html
├── style.css
└── script.js
index.html をブラウザで開くと、フェードとスライド演出を組み合わせたモーダルウインドウを確認できます。ボタンをクリックするとモーダルが滑らかに表示され、背景クリックや閉じるボタン、Escapeキーで閉じられます。
フェード+スライド演出で自然に開閉するモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
コメント