在我们平时的开发中,一定有很多地方需要用到弹窗提示功能。而系统自带的弹窗奇丑无比,而且我们不能自主控制。因此我们在开发过程中,自己封装一个弹窗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. Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

    从报错信息中,我们就可以分析出错误原因是触发了数据源的自动化配置,然而当前项目其实并不需要数据源.查其根源是依赖方提供的API依赖中引用了一些多余的依赖触发了该自动化配置的加载. 如何解决 为了解决上 ...

  2. eclipse下的mybatis插件:MyBatipse

    MyBatipse:Eclipse 下的 Mybatis插件 MyBatipse是Eclipse的一个插件,提供了内容提示和Mybatis的配置文件验证功能: 特点 XMLEditor 的增强 代码自 ...

  3. read_csv 的 names 和 index_col 参数作用

  4. Android View 阴影的总结

    关于 Android 阴影,大家肯定不陌生的.但是Android 中到底有多少种方式可以实现阴影效果以及各种方式之间有什么区别和优缺点,这就是我想总结的.下面我们一个一个来说: 一.各种实现阴影的方式 ...

  5. 深入理解Plasma(三)Plasma MVP

    这一系列文章将围绕以太坊的二层扩容框架 Plasma,介绍其基本运行原理,具体操作细节,安全性讨论以及未来研究方向等.本篇文章主要介绍 Plasma 的一个最小实现 Plasma MVP(Minima ...

  6. C# 关键字this用法

    1.this代表当前类的实例对象 public class Test { public string Name{get;set;} public void TestChange(string strN ...

  7. 【网址】.net/dot net可再发行组件下载哪家强

    微软开发者文档中有篇文章包含了.net/dot net各版本下载全家福: https://msdn.microsoft.com/zh-cn/library/5a4x27ek.aspx 安全可靠.没有三 ...

  8. django2_开发web系统接口

    1.单独创建.../sign/views_if.py文件,开发添加发布会接口 from django.http import JsonResponse from cmdb.models import ...

  9. java中double和float精度丢失问题及解决方法

    在讨论两位double数0.2和0.3相加时,毫无疑问他们相加的结果是0.5.但是问题总是如此吗? 下面我们让下面两个doubles数相加,然后看看输出结果: @Test public void te ...

  10. 2010-10-08在浏览器中兼容+jQuery3

    一.实现背景图片铺满(兼容各种浏览器) <script type="text/javascript"> $(document).ready(function() { $ ...