不借助其它的css代码和图片资源,可直接调用。
原创内容,请勿转载。
注意,不要直接复制文章里的封装代码,因为编辑器的原因,无论怎么编辑,它还是给修改了代码。文末提供了封装代码的下载地址(无需积分)
先上效果图
1、弹出一个类似prompt的输入框:
点击确定后,拿到输入的内容。点击取消关闭这个弹出框。
2、弹出一个自动消失的提示框:
3、弹出一个错误提示框:
4、弹出一个普通消息框:
5、弹出一个询问框:
6、等待框
封装实现:
function showmsgbox(id) {
this.obj = id;
this.result = "";
}
showmsgbox.prototype = {
style: function (style) {
switch (style) {
case "wrong":
case "error":
infostyle =
'data:image/svg+xml,%3Csvg%20width%3D%2224%22%20height%3D%2224%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E %3Cpath%20class="error"%20d%3D%22M12%2022C6.477%2022%202%2017.523%202%2012S6.477%202%2012%202s10%204.477%2010%2010-4.477%2010-10%2010zm-.763-15.864l.11%207.596h1.305l.11-7.596h-1.525zm.759%2010.967c.512%200%20.902-.383.902-.882%200-.5-.39-.882-.902-.882a.878.878%200%2000-.896.882c0%20.499.396.882.896.882z%22%2F%3E%3C%2Fsvg%3E';
break;
case "right":
infostyle = 'data:image/svg+xml,%3Csvg%20width%3D%2224%22%20height%3D%2224%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E %3Cpath%20class="right"%20d%3D%22M12%2022C6.477%2022%202%2017.523%202%2012S6.477%202%2012%202s10%204.477%2010%2010-4.477%2010-10%2010zm-1.177-7.86l-2.765-2.767L7%2012.431l3.119%203.121a1%201%200%20001.414%200l5.952-5.95-1.062-1.062-5.6%205.6z%22%2F%3E%3C%2Fsvg%3E';
break;
case "loading":
case "showLoading":
infostyle =
'data:image/gif;base64,R0lGODlhgACAAKIAAP///93d3bu7u5mZmQAA/wAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBQAEACwCAAIAfAB8AAAD/0i63P4wygYqmDjrzbtflvWNZGliYXiubKuloivP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R4PAwktQlG5 AAABCElEQVQ4y8WU2RKCMAxFtQsiuESJqIjV//9KAds0XRx50vvCBM60yU3CYvE/LYVUuii0kmL5 AVmVayCty1UGqeoNBNrUVcxsd5Bot42YPWS0D6iKzjkcG8RT687iN9aOOQ/IqIuNa1aXy/naYNN2 txNib7P3NZbuoB6xe7N3+6YkD8kfYz8aROeXc1VQOYgmgkBYSBLUdefp6a8DaSEV+/OgxAGUhXTE tINTFGgLFSFzG5grRUUWGupnDEHhdQ9sDizU2cR7fPJQJRZMaZuWhzIxMyORtCUVtcU3OBU12I/K qKdhibNR8UMHY3OND9jQsfENoWB8Zy3CvJWat5zz1nxy9fsP4xd6AcvoLo6BSLErAAAAJXRFWHRk YXRlOmNyZWF0ZQAyMDIyLTA1LTMwVDEyOjAzOjA5KzAzOjAw3pV8TwAAACV0RVh0ZGF0ZTptb2Rp ZnkAMjAyMi0wNS0zMFQxMjowMzowOSswMzowMK/IxPMAAAAASUVORK5CYII='
break;
case "info":
default:
infostyle =
'data:image/svg+xml,%3Csvg%20width%3D%2224%22%20height%3D%2224%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E %3Cpath%20class="error"%20d%3D%22M12%2022C6.477%2022%202%2017.523%202%2012S6.477%202%2012%202s10%204.477%2010%2010-4.477%2010-10%2010zm-.763-15.864l.11%207.596h1.305l.11-7.596h-1.525zm.759%2010.967c.512%200%20.902-.383.902-.882%200-.5-.39-.882-.902-.882a.878.878%200%2000-.896.882c0%20.499.396.882.896.882z%22%2F%3E%3C%2Fsvg%3E';
}
return infostyle;
},
show: function (message, style, config) {
if (typeof config == 'undefined') {
config = {};
}
var timeout = 1;
var msg = document.createElement("div");
if (typeof (config.timeout) == 'undefined') {
try {
var regex = /(<([^>]+)>)/gi;
var final = message.replace(regex, "");
timeout = final.length * 0.25;
} catch (e) {
timeout = 2.0;
}
} else {
if (config.timeout > 300) {
timeout = config.timeout / 1000
} else {
timeout = config.timeout;
}
}
rnd = Math.random() * 1000;
msg.id = rnd;
this.obj = msg;
this.id = rnd;
infostyle = this.style(style);
var width_str = ""
if (window.innerWidth > 500) {
width_str = "width: fit-content; margin:0 auto;max-width:35%;"
} else {
width_str = "width: fit-content; margin:0 auto;max-width:85%;"
}
var html = ''
if(style == '' || typeof style == 'undefined'){
style = 'info';
}
if (style == 'right' || style == 'wrong' || style == 'error' || style == 'confirm' || style == 'info') {
html = ' ';
}
} else if (style == 'loading') {
html = ' '
} else if(style == 'input'){
html = ' '
msg.innerHTML = html;
document.body.appendChild(msg);
var btns = document.getElementsByClassName('btn_' + rnd);
if (style == 'confirm' || style == 'error' || style == 'info' || style == 'wrong') {
for (var x in btns) {
if (typeof btns[x] == 'object') {
btns[x].addEventListener('click', function (e) {
try {
var btn_rnd = this.getAttribute('data-rnd');
document.body.removeChild(document.getElementById(btn_rnd));
} catch (e) {
}
if (typeof config.click != 'undefined') {
config.click(this.getAttribute('data-btn_index'));
}
});
}
}
}
if (style == 'input') {
for (var x in btns) {
if (typeof btns[x] == 'object') {
var that = this;
btns[x].addEventListener('click', function (e) {
var inp_val = document.getElementById('input' + rnd).value;
try {
var btn_rnd = this.getAttribute('data-rnd');
document.body.removeChild(document.getElementById(btn_rnd));
} catch (e) {
}
if (typeof config.click != 'undefined') {
that.result = inp_val;
config.click(this.getAttribute('data-btn_index'));
}
});
}
}
var textbox = document.getElementById('input' + rnd);
var n = textbox.value.length
textbox.focus();
if (navigator.userAgent.indexOf("MSIE") == -1) {
document.getElementById('input' + rnd).setSelectionRange(n, n);
} else {
var range = document.selection.createRange();
var textRange = textbox.createTextRange();
textRange.moveStart('character', n);
textRange.collapse();
textRange.select();
}
}
if (style == 'right') {
document.getElementById('cover_' + rnd).addEventListener('click', function (e) {
try {
var btn_rnd = this.getAttribute('data-rnd');
document.body.removeChild(document.getElementById(btn_rnd));
} catch (e) {
}
});
setTimeout(function () {
if(document.getElementById(rnd)){
document.body.removeChild(msg);
}
if (typeof config.redirect != 'undefined') {
location.href = config.redirect;
}
}, timeout * 1000);
}
return this;
},
close: function (who) {
console.log(who)
document.body.removeChild(document.getElementById(who.id));
},
showtext: function (text) {
try {
document.getElementById('wm_content_' + this.id).innerHTML = text;
} catch (e) {
console.error("catched error:" + e);
}
}
};
var msgbox = new showmsgbox();
为了使用起来更加方便,封装的代码直接实例化了对象。在这个情况下,尽量不要一次弹出2个及以上的弹窗。如果需要这样做,请自行实例化一个对象,使用此对象弹出窗口。例如:var mymsgbox = new showmsgbox();
mymsgbox.show("aaa')
使用方法:
说明:right类型的弹窗,可以自动关闭,也可以通过点击遮罩层关闭,也可以点击图标关闭。其它类型的弹窗,必须点击弹出窗口上的按钮关闭弹窗。
关于参数timeout的单位,如果你觉得是毫秒,它就是毫秒,如果你觉得是秒,它就是秒。你说了算。不必纠结。比如你想2秒关闭弹窗,你即可以写2,也可以写2000
调用方法始终只有一个,也就是 show()。
参数有三个:
第一个是要显示的内容。
第二个是弹窗类型,有以下几种类型:
第三个参数为json对象。可写的配置请看下面的示例。
1、弹出一个自动关闭的窗口
msgbox.show("操作成功", 'right');
如果希望用户点击之后跳转其它页面,请在第三个参数里写"redirect",例如:
msgbox.show("操作成功", 'right', {'redirect': 'https://blog.csdn.net/'});
注意事项:你可以在第三个参数中,指定 timeout 参数(和redirect同级),来决定这个弹窗多久之后自动关闭。若不指定,系统根据字数的多少计算出多久之后自动关闭。计算规则为 4个汉字为一秒。并且用户点击遮罩层可以自主关闭弹窗。
2、弹出一个错误提示窗口
msgbox.show("操作失败", 'error')
上面是最简单的调用。系统会给出一个 确定 按钮。点击后关闭弹窗。如果想玩点花样,可以这样调用:
msgbox.show("对不起,操作失败", 'error', {
button:['算了吧', '重试一下'],
click: function(e){
if(e == 0){
alert('你点的 算了吧');
} else if(e == 1){
msgbox.show("你点的 重试一下");
}
}
});
即在第三个参数中,传入你希望显示的按钮button(它是数组),以及点击按钮后的回调函数click
click函数的参数 e 是你点的第几个按钮。这个第几个按钮,是根据上面按钮数组的索引返回的。可以根据e来判断用户点击了哪个按钮。
3、弹出一个类似prompt的输入框
msgbox.show("请输入姓名", "input", {
button: ['取消', '确定'],
default: "WormJan",
click:function(e){
msgbox.show("你点的第" + e + "个按钮");
if(e == 1){
alert("输入的名字是" + msgbox.result)
} else{
alert("取消操作")
}
}
})
注意:如果不需要指定默认的值,第三个参数中可以省略default 参数。只指定 button 和 click即可。如果不指定button,系统默认会给一个取消和一个确定按钮。
4、弹出一个等待框:
var loading = msgbox.show("正在拉屎,请稍候", 'loading');
msgbox.showtext("拉完屎了,什么事?");
注意:showtext 用于更改等待时显示的内容。当然如果不喜欢可以不写。反正等待的不是你是用户。这里有一个非常重要的一点,如果你一次弹出了两个窗口,比如一个正确提示一个等待框,如果使用默认的对象(即msgbox),可能不会在等待框上显示文本变化。如果有这个需求,墙裂建议新定义一个对象来显示这个等待框。例如:
var msg = new showmsgbox();
var loading = msg.show("正在拉屎,请稍候", 'loading');
setTimeout(function(){
msg.showtext("爽,拉完了");
setTimeout(function(){
msg.close(loading)
}, 1000)
}, 1000)
msgbox.show("请输入姓名", "input", {
button: ['取消', '确定'],
default: "WormJan",
click:function(e){
msgbox.show("你点的第" + e + "个按钮");
if(e == 1){
alert("输入的名字是" + msgbox.result)
}
}
})
这时候,两个弹窗就互不影响了。
询问框和普通消息框如何使用,在你认真阅读了上面的内容后,我相信即使我不写示例代码,你已经可以自己写了。
我先去拉屎了,有问题请评论留言。
封装代码下载地址:
纯js封装一个多功能弹出框,兼容PC和移动端-Javascript文档类资源-CSDN下载