<button id="openModal1" class="open-modal-btn btn-color1">ボタン1をクリック</button>
<button id="openModal2" class="open-modal-btn btn-color2">ボタン2をクリック</button>
<button id="openModal3" class="open-modal-btn btn-color3">ボタン3をクリック</button>
<button id="openModal4" class="open-modal-btn btn-color4">ボタン4をクリック</button>
<button id="openModal5" class="open-modal-btn btn-color5">ボタン5をクリック</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span id="closeModal" class="close">×</span>
<p>ここにメッセージを表示します</p>
</div>
</div>
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
background-color: #fff;
border-radius: 20px;
box-shadow: 0 4px 16px 0 rgba(0,0,0,0.2);
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
.open-modal-btn {
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
.btn-color1 {
background-color: #1d3557;
}
.btn-color2 {
background-color: #457b9d;
}
.btn-color3 {
background-color: #80bdc6;
}
.btn-color4 {
background-color: #a8dadc;
}
.btn-color5 {
background-color: #e63946;
}
window.onload = function() {
var modal = document.getElementById('myModal');
var btns = document.getElementsByClassName('open-modal-btn');
var span = document.getElementsByClassName('close')[0];
for (let i = 0; i < btns.length; i++) {
btns[i].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';
}
}
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.