# js公共弹窗组件(一键复制)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>常用公共alert弹窗,confirm弹窗,全局弹窗-jq22.com</title>
<script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<style>
* {
	margin:0;
	padding:0;
}
a {
	text-decoration:none
}
/* 弹窗 start */
.popup_mark {
	position:fixed;
	top:0;
	left:0;
	width:100%;
	height:100%;
	background:rgba(0,0,0,.5);
	z-index:999;
}
.popup_box {
	width:405px;
	min-height:260px;
	position:absolute;
	left:50%;
	top:50%;
	margin-left:-202px;
	margin-top:-130px;
	border-radius:8px;
	background:#fff;
}
.popup_box p {
	text-align:center;
	font-size:16px;
	color:#333;
	padding:0 10px;
	display:-webkit-box;
	-webkit-box-orient:vertical;
	-webkit-line-clamp:2;
	overflow:hidden;
}
.popup_box h1 {
	width:28px;
	height:28px;
	margin:60px auto 22px;
}
.popup_box h3 {
	width:96px;
	line-height:39px;
	background:rgba(255,152,0,1);
	border-radius:2px;
	font-size:14px;
	color:#fff;
	text-align:center;
	margin:50px auto 0;
	cursor:pointer;
}
.closePopup {
	position:absolute;
	right:11px;
	top:11px;
	width:13px;
	height:13px;
	background:#fff;
	border-radius:50%;
	cursor:pointer;
}
.closePopup:before {
	content:'\0a';
	display:block;
	width:13px;
	height:2px;
	background:#bbb;
	transform:rotate(45deg);
	position:absolute;
	right:0;
	top:6px;
}
.closePopup:after {
	content:'\0a';
	display:block;
	width:13px;
	height:2px;
	background:#bbb;
	transform:rotate(-45deg);
	position:absolute;
	right:0;
	top:6px;
}
.confirmBtn {
	width:100%;
	margin:50px auto 0;
	text-align:center;
}
.confirmBtn a {
	display:inline-block;
	width:94px;
	line-height:37px;
	background:rgba(255,152,0,1);
	border-radius:2px;
	font-size:14px;
	color:#fff;
	text-align:center;
	border:1px solid rgba(255,152,0,1);
}
.confirmBtn a:last-child {
	margin-left:20px;
	background:#fff;
	color:rgba(255,152,0,1);
}
/* 弹窗 end */</style>
</head>
<body>
<div>
    <button id="alert">alert弹窗</button>
    <button id="confirm">confirm弹窗</button>
</div>

<script type="text/javascript">
    function fn() {
        console.log('催定?除?')
    }
    document.getElementById("alert").addEventListener("click", function() {
        Popup.alert("提示一下")
    })
    document.getElementById("confirm").addEventListener("click", function() {
        Popup.confirm("催定?除?", fn)
    })
</script>

<script>
var Popup = {
    /* 
     * alert 弹窗 text 必传
     */
    alert: function(text) {
        var model = document.getElementById('popupMark');
        if (model) {
            var content = document.getElementById('alertText');
            content.innerText = text;
            model.style.display = 'block';
            return
        }
        var creatediv = document.createElement('div'); // 创建div
        creatediv.className = 'popup_mark'; // 添加class
        creatediv.setAttribute('id', 'popupMark'); // 添加ID
        var contentHtml = '<div class="popup_box">' +
            '<h1><img src=""/></h1>' +
            '<p id="alertText">' + text + '</p>' +
            '<h3 id="knowBtn">我知道了</h3>' +
            '<div class="closePopup" id="closePopup"></div>' +
            '</div>'
        creatediv.innerHTML = contentHtml;
        document.body.appendChild(creatediv);
        document.getElementById('closePopup').addEventListener('click', function() {
            Popup.sureAlert();
        })
        document.getElementById('knowBtn').addEventListener('click', function() {
            Popup.sureAlert();
        })
    },
    /* 
     * 确定弹窗
     */
    sureAlert: function() {
        var model = document.getElementById('popupMark');
        model.style.display = 'none'
    },
    confirm: function(text, fn) {
        var model = document.getElementById('popupConfirm');
        if (model) {
            var content = document.getElementById('confirmText');
            content.innerText = text;
            model.style.display = 'block';
            return
        }
        var creatediv = document.createElement('div'); // 创建div
        creatediv.className = 'popup_mark'; // 添加class
        creatediv.setAttribute('id', 'popupConfirm'); // 添加ID
        var contentHtml = '<div class="popup_box">' +
            '<h1><img src=""/></h1>' +
            '<p id="confirmText">' + text + '</p>' +
            '<div class="confirmBtn"><a id="yesConfirm" href="javascript:void(0)">确定</a><a id="cancelConfirm" href="javascript:void(0)">取消</a></div>' +
            '<div class="closePopup" id="closeConfirm"></div>' +
            '</div>'
        creatediv.innerHTML = contentHtml;
        document.body.appendChild(creatediv);
        document.getElementById('closeConfirm').addEventListener('click', function() {
            Popup.closeConfirm();
        })
        document.getElementById('cancelConfirm').addEventListener('click', function() {
            Popup.closeConfirm();
        })
        document.getElementById('yesConfirm').addEventListener('click', function() {
            Popup.sureConfirm(fn);
        })
    },
    closeConfirm: function() {
        var model = document.getElementById('popupConfirm');
        model.style.display = 'none'
    },
    sureConfirm: function(fn) {
        var model = document.getElementById('popupConfirm');
        model.style.display = 'none';
        if (typeof fn == 'function') {
            fn();
        }
    }
}
</script>

</body>
</html>

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
上次更新: 3/3/2023, 14:25:47