在我们平时的开发中,一定有很多地方需要用到弹窗提示功能。而系统自带的弹窗奇丑无比,而且我们不能自主控制。因此我们在开发过程中,自己封装一个弹窗JS方便我们使用。

代码demo如下:

 // JavaScript Document
/*var allWidth = $(".menuList")[0].offsetWidth;
$(".menuList").height(allWidth*298/1400+"px");*/
var aAlert=function(content,btn1Word,callback){ //布局弹框格式
$("body").append('<div class="alertZZ">'+
'<div class="aAlert">'+
'<p style="text-align:center;font-size: 18px;margin-top: 15px;color:#fd6234;margin-left: -20px;"><img src="/webapp/discussion/ChyAppH5/img/tip.png" style="width: 23px;position: relative;top: 4px;margin-right: 4px;">提示</p>'+
/*'<img src="/model/images/guanbi.png" class="guanbi"/>'+*/
'<div class="showWord">'+
'</div>'+
/*'<div class="btnDiv"><button id="btn1">确定</button></div>'+*/
'<div class="btnDiv">确 定</div>'+
'</div>'+
'</div>'
); //弹框样式调整
$("body").css({'margin':'','padding':'','overflow':'hidden'});
$(".mainDiv").css('overflow','hidden');
$("img").css({/*width:100%;'display':'block',*/'border':'0px'});
$(".alertZZ").css({
'width':'100%','height':'100%','position':'fixed',
'background':'rgba(0,0,0,0.25)','top':'','left':'','overflow':'hidden',
'z-index':''
})
$(".alertZZ .aAlert").css({
'width':'260px','background':'#FFF','position':'absolute',
'margin':'auto','background':'#FFF','z-index':'',
'border-radius':'5px'
});
$(".alertZZ .aAlert .guanbi").css({'float':'right','margin':'10px 10px auto auto','padding':'10px','cursor': 'pointer'})
$(".alertZZ .aAlert .showWord").css({
'padding':'0 15px','color':'#7d7d7d','margin':'15px auto 30px auto',
'overflow':'hidden','line-height':'22px',
'font-size':'14px','text-align':'center'
})
$(".alertZZ .aAlert #btn1").css({
'width':'100px','background':'#263552',
'color':'#FFF','border':'0px','height':'36px',
'line-height':'36px','display':'block',
'font-size':'14px','letter-spacing':'2px',
'margin':'auto','font-family':'微软雅黑','outline': 'none'
})
$(".alertZZ .aAlert .btnDiv").css({
'background':'#fd6234','margin':'20px auto auto auto',
'text-align':'center','width':'100%','padding':'10px 0',
'color':'#fff','border-bottom-left-radius':'5px',
'border-bottom-right-radius':'5px'
}); //弹框居中显示
//alert("12"+document.documentElement.clientHeight); /*
var alertLeft=window.innerWidth/2-$(".aAlert").outerWidth()/2,
alertTop=window.innerHeight/3-$(".aAlert").outerHeight()/3;
$(".alertZZ .aAlert").css({'left':alertLeft+'px','top':alertTop+'px'});
*/
var alertLeft=window.innerWidth/-$(".aAlert").outerWidth()/,
alertTop=window.innerHeight/-$(".aAlert").outerHeight()/;
$(".alertZZ .aAlert").css({'left':alertLeft+'px','top':alertTop+'px'});
//添加弹框的内容
$(".alertZZ .aAlert .showWord").html(content);
if(btn1Word!=""){
$(".alertZZ .aAlert #btn1").html(btn1Word);
} //alert($(".showWord").html());
var str=$(".showWord").html();
//获取该div包含字符的个数
var strLength = ;
for(var i = ;i < str.length; i++){
if(str.charCodeAt(i) > ) //如果是汉字,则字符串长度加2
strLength += ;
else
strLength++;
}
//alert(strLength);
if(strLength>){$(".showWord").css('text-align','left');} //点击关闭按钮和这灰色的遮罩层,关闭弹框
$(".alertZZ .aAlert .btnDiv").click(function (){
$("body").css('overflow-y','auto');
$(".mainDiv").css('overflow-y','auto');
$(".alertZZ").remove();
//$(".alertZZ .aAlert").hide();
if($(".alertZZ .aAlert").hide()&&$(".alertZZ").hide()){
//callback();
}
});
$(".alertZZ .guanbi").click(function (){
$("body").css('overflow-y','auto');
$(".mainDiv").css('overflow-y','auto');
$(".alertZZ").remove();
//$(".alertZZ .aAlert").hide();
}); }

如果在开发的过程中,我们需要点击弹窗上的按钮后,程序再按照我们的需求走下去。我们还可以在相应的页面写JS控制,如点击确认后再跳转页面。

附上代码:

 $.ajax({
url:url+"/******.do?method=******",
type:"post",
data:datas,
dataType:"json",
cache:false,
ifModified:true,
success:function(data){
aAlert("提交成功!");
$(".btnDiv").click(function(){
location.href = "www.baidu.com";
})
}
});

注意:其中 btnDiv 是弹窗中确定按钮的类名。

处不住的,只能遗弃;盼不来的,只能放弃;留不住的,只能终止。人生,就是这样,选择不易,放弃艰难。人生之苦,苦在选择,人生之难,难在放弃。人生,说到底,就是选择与放弃。

自己封装一个弹窗JS的更多相关文章

  1. [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)

    所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...

  2. [js高手之路]javascript腾讯面试题学习封装一个简易的异步队列

    这道js的面试题,是这样的,页面上有一个按钮,一个ul,点击按钮的时候,每隔1秒钟向ul的后面追加一个li, 一共追加10个,li的内容从0开始技术( 0, 1, 2, ....9 ),首先我们用闭包 ...

  3. js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型

    一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...

  4. 使用vue.js封装一个包含图片的跑马灯组件

    初衷: 学习完Vuejs后,来准备练习仿写一下老东家的门户页面,主要是为了熟悉一下常用插件的使用,比如video.js,wow.js,swiper等等:而其中涉及到一个包含图片跑马灯组件,大概长这样( ...

  5. js中封装一个自己的简单数学对象

    封装一个数学对象求最大值最小值 <script> var myMath={ PI:3.1415926, max:function(){ var max=arguments[0];//注意a ...

  6. JS实现继承,封装一个extends方法

    父类 function Person(name,age){ this.name = name; this.age = age; } Person.prototype = { eat:function( ...

  7. 使用 WebView2 封装一个生成 PDF 的 WPF 控件

    使用 WebView2 封装一个生成 PDF 的 WPF 控件 最近在迁移项目到 .net6,发现项目中用的 PDF 库不支持 .net6,于是想着换一个库.结果找了一大圈,发现不是版本不支持,就是收 ...

  8. JS组件系列——封装自己的JS组件,你也可以

    前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...

  9. JS组件系列——封装自己的JS组件

    前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...

随机推荐

  1. Java核心技术梳理-泛型

    一.引言 在学习集合的时候我们会发现一个问题,将一个对象丢到集合中后,集合并不记住对象的类型,统统都当做Object处理,这样我们取出来再使用时就得强制转换类型,导致代码臃肿,而且加入集合时都是以Ob ...

  2. .NET Core 管道过滤器扩展

    if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseEx ...

  3. pta-3

    一:实验代码 include <stdio.h> int main() { char ch; int income=0; int unhappy, sad, glad; unhappy = ...

  4. android -------- 获取手机设备信息

    最近在开发中,需要用到一些系统信息,总结了一下 /** * Created by zhangqie on 2019/2/26 * Describe: 系统工具类 */ public class Equ ...

  5. vue 日历组件只显示本月和下个月 -- 多选日期

    效果就是上图 代码贴出 1.在components > calander > index.vue <template> <div class="page&quo ...

  6. 基于c#的windows基础设计(学习日记1)【关于异或运算】

    第一次接触异或运算,总体来说比哈希算法简单的多,无论是理解还是代码的难易度,唯一不好的是在固定了密钥之后,随机性就小了很多,所以安全性比起哈希算法还是有所差距. 原理在网站上很多都有所以就不再赘述了. ...

  7. mysql 表中已经存在数据 修改字段类型 varchar(11) 改为 int(11)

    update tablename set s_role = '' alter table  tablename  modify column s_role int(11)

  8. Win10系列:C#应用控件基础11

    RichEditBox控件 富文本格式是一种跨平台的文档格式,在这种格式的文档中可以编辑文本.图片.链接等内容.通过RichEditBox控件可以对富文本格式的文档进行编辑. 在XAML文件中,Ric ...

  9. 命令行听歌http://www.linuxsir.org/bbs/thread280142.html?pageon=1#1584689

    在纯字符界面下听歌 利用 play 命令可以在命令行中播放音频文件,在纯字符界面下也没问题! ----------------------------------------------------- ...

  10. 微服务-网关-node.js by 大雄daysn

    目录 序言 一.node.js入门1.1 下载并安装1.2 从helloworld到一个web应用1.3 Express框架二.node.js搭建网关 三.node.js集群搭建   序言 首先一个问 ...