JavaScript: Как написать модальное окно?
Помогите написать модальное окно, которое будет выводить текст. Проблема в том, что модальное окно должно быть написано на чистом js (без использования html или css). Только js. Окно должно появляться, когда я получаю message.
(function ($) {
async function receiveMessage(event) {
if (event.data.messageType === 'CLIENT_MESSAGE') {
console.log("MESSAGE", event);
(тут должно быть модальное окно)
}
}
$(document).ready(function () {
window.addEventListener('message', receiveMessage, false);
});
Все примеры, что я смотрела, всё равно содержат немного html, но мне нужно, чтобы использовался только js.
Ответы (2 шт):
Автор решения: Владлен Вожжаев
→ Ссылка
Если сложно сделать самостоятельно, то можно использовать готовые решения. Например вот модальное окно с фреймворком Bootstrap 4.
<!doctype html>
<html lang="ru">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary mt-5 ml-5" data-toggle="modal" data-target="#exampleModal">
Запустить модальное окно
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
Автор решения: Zhyvko Roman
→ Ссылка
const openBtn = document.querySelector('#open'),
modal = document.querySelector('#modal'),
closeBtn = document.querySelector('#close');
openBtn.addEventListener('click', () => {
modal.style.display = 'block';
});
closeBtn.addEventListener('click', () => {
modal.style.display = 'none';
});
window.addEventListener("click", (e) => {
const target = e.target;
if (target === modal) {
modal.style.display = "none";
}
});
.myModal {
display: none;
position: absolute;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.2);
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
min-height: 170px;
margin: 5px auto;
padding: 0;
max-width: 750px;
animation-name: animatetop;
animation-duration: 0.4s;
border-radius: 15px;
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0;
}
to {
top: 0;
opacity: 1;
}
}
/* The Close Button */
#close {
color: white;
float: right;
font-size: 35px;
font-weight: bold;
margin-right: 12px;
}
#close:hover,
#close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 7px 10px;
background-color: #339af0;
color: white;
border-radius: 15px 15px 0 0;
}
.modal-body {
display: flex;
justify-content: center;
padding: 2px 10px;
}
.warning-icon {
position: relative;
height: 80px;
width: 80px;
margin-right: 25px;
}
.message {
text-align: center;
line-height: 100px;
font-size: 30px;
margin: 0;
}
h2 {
font-size: 30px;
margin: 0;
}
<button id="open">Open</button>
<div id="modal" class="myModal">
<div class="modal-content">
<div id="close" data-close>×</div>
<div class="modal-header">
<h2>Message</h2>
</div>
<div class="modal-body">
<img class="warning-icon" src="https://icon-library.com/images/successful-icon/successful-icon-10.jpg" alt="">
<p class="message">Registration successful!</p>
</div>
</div>
</div>