2025年1月15日

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>モーダルウィンドウサンプル</title>
<style>
body {
font-family: Arial, sans-serif;
}
.modal {
display: none; /* 初期状態では表示しない */
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 600px;
border-radius: 10px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>モーダルウィンドウのデモ</h2>
<!-- トリガーボタン -->
<button id="myBtn">モーダルを開く</button>
<!-- モーダルウィンドウ -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>これはモーダルウィンドウのサンプルです。</p>
</div>
</div>
<script>
// モーダルウィンドウを開くボタン
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
// ボタンがクリックされたときにモーダルを開く
btn.onclick = function() {
modal.style.display = "block";
}
// ×ボタンがクリックされたときにモーダルを閉じる
span.onclick = function() {
modal.style.display = "none";
}
// モーダルの外側をクリックしたときにモーダルを閉じる
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>