-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-popup.js
95 lines (82 loc) · 2.37 KB
/
modal-popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Membuat elemen modal dengan JavaScript
const modalOverlay = document.createElement('div');
const modalBox = document.createElement('div');
const modalTitle = document.createElement('h2');
const modalContent = document.createElement('p');
const closeButton = document.createElement('button');
const openButton = document.createElement('button');
// Menyusun struktur modal
modalTitle.innerText = 'Modal Title';
modalContent.innerText = 'This is a simple modal pop-up created with pure JavaScript.';
closeButton.innerText = 'Close';
openButton.innerText = 'Open Modal';
// Menambahkan elemen modal ke dalam modalBox
modalBox.appendChild(modalTitle);
modalBox.appendChild(modalContent);
modalBox.appendChild(closeButton);
// Menyusun modal overlay
modalOverlay.appendChild(modalBox);
// Menyembunyikan modal di awal
modalOverlay.style.display = 'none';
// Menambahkan modal dan tombol buka ke dalam body
document.body.appendChild(modalOverlay);
document.body.appendChild(openButton);
// Menambahkan CSS secara langsung melalui JavaScript
document.body.style.cssText = `
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
`;
modalOverlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
`;
modalBox.style.cssText = `
background-color: #fff;
width: 300px;
padding: 20px;
border-radius: 8px;
text-align: center;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
`;
closeButton.style.cssText = `
background-color: #ff5c5c;
border: none;
color: white;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
`;
openButton.style.cssText = `
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
`;
// Fungsi untuk membuka modal
openButton.onclick = function() {
modalOverlay.style.display = 'flex';
};
// Fungsi untuk menutup modal
closeButton.onclick = function() {
modalOverlay.style.display = 'none';
};
// Fungsi untuk menutup modal saat mengklik di luar modal box
window.onclick = function(event) {
if (event.target === modalOverlay) {
modalOverlay.style.display = 'none';
}
};