jQuery确认提示框插件myAlert
发布时间:2018-02-24, 11:48:56 分类:HTML | 编辑 off 网址 | 辅助
正文 8282字数 384,931阅读
css myAlert.css
*{margin: 0;padding: 0;}
.clearfix:after{content: '';display: table;width: 100%;clear: both;}
.myModa{position: fixed;top: 0;left: 0;bottom: 0;right:0;background: rgba(0,0,0,0.2); }
.myModa .myAlertBox{width: 300px;border-radius: 5px;border: 1px solid #d9d9d9;background: #fff;overflow: hidden;margin: 0 auto; }
.myModa .myAlertBox h6{background: #f2f2f2;padding: 10px;line-height: 20px;font-size: 16px;text-align: center;}
.myModa .myAlertBox p{padding: 20px;line-height: 26px;font-size: 14px;color: #808080;}
.myModa .myAlertBox .btn{cursor: pointer; width: 80px;line-height: 36px;border-radius: 5px;text-align: center;font-size: 16px;margin: 0 auto;margin-bottom: 20px; background: #D9D9D9;}
.myModa .myAlertBox .col2 .col{width:120px;float: left;}
.myModa .myAlertBox .col2 .col .btn{width: 100%;}
.myModa .myAlertBox .col2{padding: 0 20px;}
.myToast{position: fixed; display: inline-block;padding: 5px 10px;line-height: 24px;font-size: 14px;color: #fff;background:#000; max-width: 300px;_width: expression(this.offsetWidth >300 ? '300px': 'auto' );*width: expression(this.offsetWidth >300 ? '300px': 'auto' );border-radius: 5px;}
Run code
Cut to clipboard
js myAlert.js
(function() {
$.extend({
myAlert: function(options) {//参数格式{title:'Title',message:'message',callback:function(){alert('callback')}}or"需要提示的话"
var option={title:"提示",message:"程序员太傻,忘记输入提示内容啦……",callback:function(){}}
if(typeof(options)=="string"){
option.message=options
}else{
option=$.extend(option,options);
}
var top=$(window).height()*0.3;
$('body').append('<div class="myModa"><div class="myAlertBox" style="margin-top:'+top+'px"><h6>'+option.title+'</h6><p>'+option.message+'</p><div class="btn sure">确定</div></div></div>');
$('.btn.sure').click(function(){
$('.myModa').remove();
option.callback();
})
},
myConfirm: function(options) {//参数格式{title:'Title',message:'message',callback:function(){alert('callback')}}or"需要提示的话"$.myConfrim()
var option={title:"提示",message:"程序员太傻,忘记输入提示内容啦……",callback:function(){}}
if(typeof(options)=="string"){
option.message=options
}else{
option=$.extend(option,options);
}
var top=$(window).height()*0.3;
$('body').append('<div class="myModa"><div class="myAlertBox" style="margin-top:'+top+'px"><h6>'+option.title+'</h6><p>'+option.message+'</p><div class="col2"><div class="col" style="margin-right: 20px;"><div class="btn exit">取消</div></div><div class="col"><div class="btn sure">确定</div></div></div></div></div>');
$('.btn.exit').click(function(){
$('.myModa').remove();
})
$('.btn.sure').click(function(){
$('.myModa').remove();
option.callback();
})
},
myToast:function(message){
var top=$(window).height()*0.3;
$('body').append('<div class="myToast">'+message+'</div>');
console.log($('.myToast').outerWidth())
var top=($(window).height()-$('.myToast').height())/2;
var left=($('body').width()-$('.myToast').width())/2;
$('.myToast').css({'top':top+'px','left':left+'px'});
setTimeout(function(){
$('.myToast').fadeOut(300);
setTimeout(function(){
$('.myToast').remove();
},300)
},1000)
}
});
})(jQuery)
Run code
Cut to clipboard
完整代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery确认提示框插件myAlert</title>
<style>
*{margin: 0;padding: 0;}
.clearfix:after{content: '';display: table;width: 100%;clear: both;}
.myModa{position: fixed;top: 0;left: 0;bottom: 0;right:0;background: rgba(0,0,0,0.2); }
.myModa .myAlertBox{width: 300px;border-radius: 5px;border: 1px solid #d9d9d9;background: #fff;overflow: hidden;margin: 0 auto; }
.myModa .myAlertBox h6{background: #f2f2f2;padding: 10px;line-height: 20px;font-size: 16px;text-align: center;}
.myModa .myAlertBox p{padding: 20px;line-height: 26px;font-size: 14px;color: #808080;}
.myModa .myAlertBox .btn{cursor: pointer; width: 80px;line-height: 36px;border-radius: 5px;text-align: center;font-size: 16px;margin: 0 auto;margin-bottom: 20px; background: #D9D9D9;}
.myModa .myAlertBox .col2 .col{width:120px;float: left;}
.myModa .myAlertBox .col2 .col .btn{width: 100%;}
.myModa .myAlertBox .col2{padding: 0 20px;}
.myToast{position: fixed; display: inline-block;padding: 5px 10px;line-height: 24px;font-size: 14px;color: #fff;background:#000; max-width: 300px;_width: expression(this.offsetWidth >300 ? '300px': 'auto' );*width: expression(this.offsetWidth >300 ? '300px': 'auto' );border-radius: 5px;}</style>
<script type="text/javascript" src="https://lizhenqiu.com/templates/default/jquery.min.js"></script>
<script>(function() {
$.extend({
myAlert: function(options) {//参数格式{title:'Title',message:'message',callback:function(){alert('callback')}}or"需要提示的话"
var option={title:"提示",message:"程序员太傻,忘记输入提示内容啦……",callback:function(){}}
if(typeof(options)=="string"){
option.message=options
}else{
option=$.extend(option,options);
}
var top=$(window).height()*0.3;
$('body').append('<div class="myModa"><div class="myAlertBox" style="margin-top:'+top+'px"><h6>'+option.title+'</h6><p>'+option.message+'</p><div class="btn sure">确定</div></div></div>');
$('.btn.sure').click(function(){
$('.myModa').remove();
option.callback();
})
},
myConfirm: function(options) {//参数格式{title:'Title',message:'message',callback:function(){alert('callback')}}or"需要提示的话"$.myConfrim()
var option={title:"提示",message:"程序员太傻,忘记输入提示内容啦……",callback:function(){}}
if(typeof(options)=="string"){
option.message=options
}else{
option=$.extend(option,options);
}
var top=$(window).height()*0.3;
$('body').append('<div class="myModa"><div class="myAlertBox" style="margin-top:'+top+'px"><h6>'+option.title+'</h6><p>'+option.message+'</p><div class="col2"><div class="col" style="margin-right: 20px;"><div class="btn exit">取消</div></div><div class="col"><div class="btn sure">确定</div></div></div></div></div>');
$('.btn.exit').click(function(){
$('.myModa').remove();
})
$('.btn.sure').click(function(){
$('.myModa').remove();
option.callback();
})
},
myToast:function(message){
var top=$(window).height()*0.3;
$('body').append('<div class="myToast">'+message+'</div>');
console.log($('.myToast').outerWidth())
var top=($(window).height()-$('.myToast').height())/2;
var left=($('body').width()-$('.myToast').width())/2;
$('.myToast').css({'top':top+'px','left':left+'px'});
setTimeout(function(){
$('.myToast').fadeOut(300);
setTimeout(function(){
$('.myToast').remove();
},300)
},1000)
}
});
})(jQuery)
</script>
</head>
<body>
<center>
<button onClick="$.myAlert('这里是提示框内的内容');">点击弹出提示框</button><br/><br/>
<button onClick="$.myAlert({title:'Title',message:'message',callback:function(){alert(1)}});">点击弹出提示框(带有callback)</button><br/><br/>
<button onClick="$.myConfirm({title:'确认框提示标题',message:'确认框提示内容',callback:function(){alert('callback')}})">点击弹出确认框</button><br/><br/>
<button onClick="$.myToast('提示内容')">点击弹出自动消失的提示</button><br/><br/>
</center>
</body>
</html>
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes" /> <title>jQuery手机自定义确认提示框代码</title> <style> *{ margin: 0; padding:0; list-style: none; font-size: 16px; color: rgba(0,0,0,.8); } a{ text-decoration: none; } /*jq-alert弹出层封装样式*/ .jq-alert{ position: fixed; top:0; left:0; width:100%; height:100%; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-direction: row; flex-direction: row; -webkit-justify-content: center; -webkit-align-items: center; justify-content: center; align-items: center; background-color: rgba(0,0,0,.3); z-index: 99; } .jq-alert .alert{ background-color: #FFF; width:80%; height:auto; border-radius: 4px; overflow: hidden; } .jq-alert .alert .title{ position: relative; margin: 0; font-size: .3rem; height: .8rem; line-height: .8rem; text-align: center; font-weight: normal; color: rgba(0,0,0,.8); } /*.jq-alert .alert .title:after{*/ /*position: absolute;*/ /*content: "";*/ /*bottom: 0;*/ /*left:0;*/ /*width: 100%;*/ /*height: 1px;*/ /*background-color: #ededed;*/ /*transform: scaley(.5);*/ /*-webkit-transform: scaley(.5);*/ /*}*/ .jq-alert .alert .content{ padding: .3rem; font-size: .28rem; color: rgba(0,0,0,.6); } .jq-alert .alert .content .prompt{ width:100%; } .jq-alert .alert .content .prompt .prompt-content{ font-size: .28rem; color: rgba(0,0,0,.54); } .jq-alert .alert .content .prompt .prompt-text{ background:none; border:none; outline: none; width: 100%; height: .6rem; box-sizing: border-box; margin-top: .2rem; background-color: #FFF; border:1px solid #dcdcdc; text-indent:5px; } .jq-alert .alert .content .prompt .prompt-text:focus{ border: 1px solid #2196F3; background-color: rgba(33,150,243,.08); } .jq-alert .alert .fd-btn{ position: relative; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-direction: row; flex-direction: row; -webkit-justify-content: center; -webkit-align-items: center; justify-content: center; align-items: center; } .jq-alert .alert .fd-btn:after{ position: absolute; content: ""; top:0; left:0; width:100%; height: 1px; background-color: #F3F3F3; } .jq-alert .alert .fd-btn a{ width:100%; height: .8rem; font-size: .26rem; display: flex; flex-direction: row; align-items: center; justify-content: center; color: rgba(0,0,0,.8); } .jq-alert .alert .fd-btn a.cancel{ position: relative; color: rgba(0,0,0,.5); } .jq-alert .alert .fd-btn a.cancel:after{ content: ""; position: absolute; top:.1rem; right:0; width: 1px; height: .6rem; background-color: #F3F3F3; } .jq-alert .alert .fd-btn a.confirm{ color: #2196F3; } .jq-alert .alert .fd-btn a.confirm:active{ background-color: #2196F3; color: #FFF; } /*toast弹出层*/ .jq-toast{ z-index: 999; position:fixed; top:0; left:0; width:100%; height: 100%; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; flex-direction: row; -webkit-flex-direction: row; -ms-flex-direction: row; justify-content: center; -webkit-justify-content: center; align-items: center; -webkit-align-items: center; } .jq-toast .toast{ max-width: 80%; padding: .1rem .2rem; background-color: rgba(0,0,0,.48); color: #FFF; border-radius: 4px; font-size: .28rem; } </style> <script type="text/javascript" src="https://lizhenqiu.com/templates/default/jquery.min.js"></script> <script> /*alert弹出层*/ function jqalert(param) { var title = param.title, content = param.content, yestext = param.yestext, notext = param.notext, yesfn = param.yesfn, nofn = param.nofn, nolink = param.nolink, yeslink = param.yeslink, prompt = param.prompt, click_bg = param.click_bg; if (click_bg === undefined){ click_bg = true; } if (yestext === undefined){ yestext = '确认'; } if (!nolink){ nolink = 'javascript:void(0);'; } if (!yeslink){ yeslink = 'javascript:void(0);'; } var htm = ''; htm +='<div class="jq-alert" id="jq-alert"><div class="alert">'; if(title) htm+='<h2 class="title">'+title+'</h2>'; if (prompt){ htm += '<div class="content"><div class="prompt">'; htm += '<p class="prompt-content">'+prompt+'</p>'; htm += '<input type="text" class="prompt-text"></div>'; htm +='</div>'; }else { htm+='<div class="content">'+content+'</div>'; } if (!notext){ htm+='<div class="fd-btn"><a href="'+yeslink+'" class="confirm" id="yes_btn">'+yestext+'</a></div>' htm+='</div>'; }else { htm+='<div class="fd-btn">'+ '<a href="'+nolink+'" data-role="cancel" class="cancel">'+notext+'</a>'+ '<a href="'+yeslink+'" class="confirm" id="yes_btn">'+yestext+'</a>'+ '</div>'; htm+='</div>'; } $('body').append(htm); var al = $('#jq-alert'); al.on('click','[data-role="cancel"]',function () { al.remove(); if (nofn){ param.nofn(); nofn = ''; } param = {}; }); $(document).delegate('.alert','click',function (event) { event.stopPropagation(); }); $(document).delegate('#yes_btn','click',function () { setTimeout(function () { al.remove(); },300); if (yesfn){ param.yesfn(); yesfn =''; } param = {}; }); if(click_bg === true){ $(document).delegate('#jq-alert','click',function () { setTimeout(function () { al.remove(); },300); yesfn =''; nofn = ''; param = {}; }); } } /*toast 弹出提示*/ function jqtoast(text,sec) { var _this = text; var this_sec = sec; var htm = ''; htm += '<div class="jq-toast" style="display: none;">'; if (_this){ htm +='<div class="toast">'+_this+'</div></div>'; $('body').append(htm); $('.jq-toast').fadeIn(); }else { jqalert({ title:'提示', content:'提示文字不能为空', yestext:'确定' }) } if (!sec){ this_sec = 2000; } setTimeout(function () { $('.jq-toast').fadeOut(function () { $(this).remove(); }); _this = ''; },this_sec); }</script> <script type="text/javascript"> setSize(); addEventListener('resize',setSize); function setSize() { document.documentElement.style.fontSize = document.documentElement.clientWidth/750*100+'px'; } </script> <style> .show-list{ width:80%; margin: 0 auto; } .show-list li{ height: 1rem; font-size: .3rem; display: flex; flex-direction: row; justify-content: center; align-items: center; border-bottom: 1px solid #dcdcdc; } </style> </head> <body> <ul class="show-list"> <li id="demo1">toast</li> <li id="demo2">alert</li> <li id="demo3">confirm</li> <li id="demo4">prompt</li> <li id="demo5">点击按钮跳转</li> <li id="demo6">取消默认点击背景消失</li> </ul> <script type="text/javascript"> $(function () { $('#demo1').click(function () { jqtoast('你点击了toast') }) $('#demo2').click(function () { jqalert({ title:'提示', content:'自定义弹窗内容,居左对齐显示,告知需要确认的信息等' }) }) $('#demo3').click(function () { jqalert({ title:'提示', content:'自定义弹窗内容,居左对齐显示,告知需要确认的信息等', yestext:'知道了', notext:'取消' }) }) $('#demo4').click(function () { jqalert({ title:'提示', prompt:'请问你叫什么名字?', yestext:'提交', notext:'取消', yesfn:function () { jqtoast('提交成功'); }, nofn:function () { jqtoast('你点击了取消'); } }) }) $('#demo5').click(function () { jqalert({ title:'提示', content:'自定义弹窗内容,居左对齐显示,告知需要确认的信息等', yeslink:'https://b.lizhenqiu.com/' }) }) $('#demo6').click(function () { jqalert({ title:'提示', content:'自定义弹窗内容,居左对齐显示,告知需要确认的信息等', click_bg:false }) }) }); </script> </body> </html>