在我们平时的开发中,一定有很多地方需要用到弹窗提示功能。而系统自带的弹窗奇丑无比,而且我们不能自主控制。因此我们在开发过程中,自己封装一个弹窗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. [easyUI] lazyload 懒加载

    1.使用<img>标签将图片都写在网页上. <div style="height:450px;"><h1>请往下看,有图片的吆!</h1& ...

  2. blob对象的应用

    demo:https://pan.baidu.com/s/1hsq2vgK 最近在学习blob,利用blob编写了两个业务场景,详情请下载demo查看 1:大文件分片下载,服务器端使用.net接收客户 ...

  3. idea启动springboot项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

    有一次启动springboot项目的时候,报了一个非常奇怪的错误,说是找不到servletContext,springboot不是自带tomcat的吗? 在网上找了好久,说是用以下方式解决. 解决方式 ...

  4. 5、SAMBA服务二:配置实例

    ①:SAMBA服务一:参数详解 ②:SAMBA服务二:配置实例 5.2.3.Samba共享目录配置实例 1.允许匿名用户读取/it共享目录,修改/etc/samba/smb.conf,在最后添加以下内 ...

  5. MongoDB 教程(七):插入文档、更新文档、删除文档

    MongoDB 插入文档 文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式 —— BSON是一种类json的二进制形式的存储格式,简称Binary JSON. MongoDB ...

  6. vscode settings.json

    // 快捷键设置 keyiing.json // 将键绑定放入此文件中以覆盖默认值 [ /* // 转换大写 { "key" : "ctrl+shift+u", ...

  7. Linux---centos编译安装ffmpeg

    环境 系统环境:CentOS release 6.7 (Final) 需求 编译安装ffmpeg 获取依赖 安装依赖包 yum install -y autoconf automake cmake f ...

  8. Learn how to use git

    Git配置 $ git config --global user.name "Your Name" $ git config --global user.email "e ...

  9. 安装numpy&pandas

    https://www.scipy.org/scipylib/download.html 上述网站下载对应自己电脑版本的numpy.我的电脑是win32位,py是3.6的. 然后运行cmd,再按照如下 ...

  10. 剑指Offer 38. 二叉树的深度 (二叉树)

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题目地址 https://www.nowcoder.com/prac ...