Как сделать модальное окно с помощью JavaScript
Решил немного перепрофилировать свой блог - Ашман-парк нахрен никому не нужен.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Tutorial</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Модальное окно!</p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.9);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
JavaScript
var modal = document.getElementById('myModal');
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
"use strict";
modal.style.display = "block";
};
span.onclick = function () {
"use strict";
modal.style.display = "none";
};
window.onclick = function (event) {
"use strict";
if (event.target === modal) {
modal.style.display = "none";
}
};
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Tutorial</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Модальное окно!</p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.9);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
JavaScript
var modal = document.getElementById('myModal');
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
"use strict";
modal.style.display = "block";
};
span.onclick = function () {
"use strict";
modal.style.display = "none";
};
window.onclick = function (event) {
"use strict";
if (event.target === modal) {
modal.style.display = "none";
}
};
Комментарии
Отправить комментарий