自己封装一个弹窗JS
在我们平时的开发中,一定有很多地方需要用到弹窗提示功能。而系统自带的弹窗奇丑无比,而且我们不能自主控制。因此我们在开发过程中,自己封装一个弹窗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的更多相关文章
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...
- [js高手之路]javascript腾讯面试题学习封装一个简易的异步队列
这道js的面试题,是这样的,页面上有一个按钮,一个ul,点击按钮的时候,每隔1秒钟向ul的后面追加一个li, 一共追加10个,li的内容从0开始技术( 0, 1, 2, ....9 ),首先我们用闭包 ...
- js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型
一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...
- 使用vue.js封装一个包含图片的跑马灯组件
初衷: 学习完Vuejs后,来准备练习仿写一下老东家的门户页面,主要是为了熟悉一下常用插件的使用,比如video.js,wow.js,swiper等等:而其中涉及到一个包含图片跑马灯组件,大概长这样( ...
- js中封装一个自己的简单数学对象
封装一个数学对象求最大值最小值 <script> var myMath={ PI:3.1415926, max:function(){ var max=arguments[0];//注意a ...
- JS实现继承,封装一个extends方法
父类 function Person(name,age){ this.name = name; this.age = age; } Person.prototype = { eat:function( ...
- 使用 WebView2 封装一个生成 PDF 的 WPF 控件
使用 WebView2 封装一个生成 PDF 的 WPF 控件 最近在迁移项目到 .net6,发现项目中用的 PDF 库不支持 .net6,于是想着换一个库.结果找了一大圈,发现不是版本不支持,就是收 ...
- JS组件系列——封装自己的JS组件,你也可以
前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...
- JS组件系列——封装自己的JS组件
前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...
随机推荐
- .Net文件压缩
NuGet中下载Ionic.Zip: public static class ZipHelper { /// <summary> /// 压缩文件 /// </summary> ...
- 在1-10中选择一个数,输出x+xx+xxx+xxx....x之和,如:数字为2,则2+22=24
代码: package bao; import java.util.Random; public class a { public static void main(String[] args) { ...
- English trip V2 - A 1. Mother tongue 母语 Teacher:Lamb Key: Comparative 比较级 superlative 最高级
In this lesson you will learn to talk about languages. 课上内容(Lesson) mother tongue: Chinese official ...
- 覃超:Facebook的项目开发流程和工程师的绩效管理机制
覃超:Facebook的项目开发流程和工程师的绩效管理机制 http://mp.weixin.qq.com/s?__biz=MjM5MDE0Mjc4MA==&mid=2650992350&am ...
- 『流畅的Python』第5章笔记_一等函数
- vue轮播,vue-awesome-swiper动态数据渲染,loop无效,轮循无效
解决办法:在渲染数组数据前.判断是否为空 v-if="slideList.length>1" <template> <div class="ban ...
- ThinkPHP5.0源码学习之注册错误和异常处理机制
在base.php文件中,用一句代码\think\Error::register();实现错误和异常处理机制的注册. // 注册错误和异常处理机制 \think\Error::register(); ...
- mqtt------ mosca服务器端参数简介
一:服务器端 为什么使用mosca:mosca是基于node.js开发,上手难度相对较小,其次协议支持完整,除了不支持Qos 2,其它的基本都支持.持久化支持redis以及mongo.二次开发接口简单 ...
- .NET实现IoC
.NET里简易实现IoC 前言 在前面的篇幅中对依赖倒置原则和IoC框架的使用只是做了个简单的介绍,并没有很详细的去演示,可能有的朋友还是区分不了依赖倒置.依赖注入.控制反转这几个名词,或许知道的也只 ...
- ES5和ES6那些你必须知道的事儿(二)
ES5和ES6那些你必须知道的事儿 ES5新增的东西 二.对象方法 1.Object.getPrototypeOf(object) 返回对象的原型 function Pasta(grain, widt ...