基于layer简单的弹层封装
/**
* 产生长度为32的Guid字符串
*/
function getGuid32() {
var rt_str = String.fromCharCode(65 + Math.floor(Math.random() * 26));
for (i = 0; i < 31; ++i) {
var num = Math.floor(Math.random() * (26 + 26 + 10));
var ch_str;
if (num < 10) {
ch_str = num.toString();
}
else if (num < 10 + 26) {
ch_str = String.fromCharCode(65 + num - 10);
}
else {
ch_str = String.fromCharCode(97 + num - 10 - 26);
}
rt_str += ch_str;
}
return rt_str;
} /**
* 根据guid删除alert-div元素
* @param Id guid
*/
function delAlertDivById(Id) {
//等待layer自动处理好
setTimeout(function () {
var $alert_div = $(".alert-div[data-guid='" + Id + "']");
if ($alert_div.length == 0) {
clearInterval(id);
}
else {
$alert_div.remove();
}
}, 1200);
//上面这个延时数值很重要,因为layer没提供close的回调,所以只能如此等待
} /**
* 关闭弹层相关的清理工作
* @param that myAlert对象
*/
function closeWork(that) {
clearTimeout(that.autoCloseTimerId);
layer.close(that.winId);
delAlertDivById(that.uniqueId);
} /**
* 根据myAlert对象组件出jQuery弹窗对象
* @param that myAlert对象
*/
function buildPopup(that) {
var baseHtmlStr = "\
<div class='alert-div'>\
<div class='title'>\
<i class='fa icon'></i>\
<span class='text'></span>\
<i class='fa fa-close icon-close'></i>\
</div>\
<div class='content text-center'>\
<i class='fa fa-5x icon'></i>\
<span class='text1'></span>\
<span class='text2'></span>\
</div>\
<div class='control text-center'>\
<div class='btn btn-base'></div>\
</div>\
</div>\
";
var $baseJq = $(baseHtmlStr); $baseJq.attr("data-guid", that.uniqueId); $baseJq.children(".title").children(".icon").addClass(that.titleIcon);
$baseJq.children(".content").children(".icon").addClass(that.contentIcon);
$baseJq.children(".title").children(".text").text(that.title);
$baseJq.children(".content").children(".text1").text(that.text);
$baseJq.children(".content").children(".text2").text(that.summary);
$baseJq.children(".control").children(".btn-base").text(that.okBtnText); $baseJq.children(".title").children(".icon-close").click(that.canClose ? that.closeBtnFunction : function () { });
$baseJq.children(".control").children(".btn-base").click(that.okBtnFunction); return $baseJq;
} /**
* 根据配置对象对于myAlert对象进行配置
* @param dstObj 目标对象,myAlert对象
* @param cfgObj 配置对象
*/
function defaultConfig(dstObj, cfgObj) {
if (cfgObj == undefined) {
cfgObj = new Object();
} dstObj.uniqueId = cfgObj.uniqueId || getGuid32(); dstObj.title = cfgObj.title || "提示";
dstObj.text = cfgObj.text || "确认?";
dstObj.summary = cfgObj.summary || "确认请按下方按钮";
dstObj.okBtnText = cfgObj.okBtnText || "确认"; dstObj.titleIcon = cfgObj.titleIcon || "fa-info-circle";
dstObj.contentIcon = cfgObj.contentIcon || "fa-exclamation-circle"; if (cfgObj.canClose == undefined) {
dstObj.canClose = true;
}
else {
dstObj.canClose = cfgObj.canClose;
}
dstObj.autoCloseTimer = cfgObj.autoCloseTimer || -1; dstObj.okBtnFunction = cfgObj.okBtnFunction || function () {
closeWork(dstObj);
};
dstObj.closeBtnFunction = cfgObj.closeBtnFunction || function () {
closeWork(dstObj);
};
dstObj.autoCloseTimerId = -1;
dstObj.autoCloseFunction = cfgObj.autoCloseFunction || function () {
closeWork(dstObj);
}; //存储layer返回的弹层id
dstObj.winId = -1;
} /**
* myAlert对象构造函数
* @param cfgObj myAlert配置对象
*/
function myAlert(cfgObj) { defaultConfig(this, cfgObj); //弹出弹层方法
this.show = function () {
if ($(".alert-div[data-guid='" + this.uniqueId + "']").length > 0) {
return;
} var $baseJq = buildPopup(this); $("body").append($baseJq); /**
* 实际弹窗部分
*/
var $popup_dom = $baseJq;
this.winId = layer.open({
id: this.uniqueId,
type: 1,
closeBtn: 0,
title: false,
content: $popup_dom,
area: [$popup_dom.width(), $popup_dom.height()]
}); if (this.canClose) {
if (this.autoCloseTimer > -1) {
var that = this;
this.autoCloseTimerId = setTimeout(function () {
that.autoCloseFunction();
}, this.autoCloseTimer);
}
}
};
}
.layui-layer {
background-color: rgba(255, 255, 255, 0) !important;
}
.alert-div {
position: relative;
width: 740px;
height: 480px;
border: 1px solid #f2af6f;
border-radius: 6px;
font-family: 'Microsoft YaHei UI';
margin: 0px;
padding: 0px;
background-color: white;
overflow-y: hidden;
overflow-x: hidden;
display: none;
}
.alert-div .title {
background: linear-gradient(to bottom, #f2af6f 10%, #f3a02c 90%);
font-size: 48px;
height: 70px;
}
.alert-div .title .icon {
margin: 0px 0px 10px 14px;
}
.alert-div .title .text {
position: absolute;
top: 9px;
left: 67px;
font-size: 34px;
font-weight: bold;
}
.alert-div .title .icon-close {
margin: 8px 12px 10px 14px;
float: right;
}
.alert-div .content {
height: 270px;
padding-top: 50px;
}
.alert-div .content .icon {
display: block;
}
.alert-div .content .text1 {
color: black;
font-size: 38px;
margin-top: 40px;
display: block;
}
.alert-div .content .text2 {
color: #444;
font-size: 24px;
margin-top: 10px;
display: block;
}
.alert-div .control {
height: 140px;
}
.alert-div .control .btn-base {
border: solid 1px #f2af6f;
border-radius: 6px;
font-size: 36px;
font-weight: bold;
padding-left: 40px;
padding-right: 40px;
margin-top: 35px;
background: linear-gradient(to bottom, #f2af6f 10%, #f3a02c 90%);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="Content/bootstrap.css" />
<link rel="stylesheet" href="Content/font-awesome.css" />
<link rel="stylesheet" href="layer/skin/default/layer.css" />
<link rel="stylesheet" href="Content/trml-myAlert.css" />
</head>
<body>
<div class="container" style="margin-top: 20px;">
<input type="button" class="btn btn-primary btn-test1" value="测试1" />
</div>
<script src="Scripts/jquery-3.1.1.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script src="layer/layer.js"></script>
<script src="Scripts/trml-myAlert-jq.js"></script>
<script>
$(".btn-test1").click(function () {
var tstAlert = new myAlert({
uniqueId: "gushihao",
okBtnFunction: function () {
alert("哈哈");
closeWork(tstAlert);
},
autoCloseTimer: 10000,
autoCloseFunction: function () {
alert("我要自动关闭了!");
closeWork(tstAlert);
},
closeBtnFunction: function () {
alert("你点击了关闭按钮");
closeWork(tstAlert);
}
});
tstAlert.show();
});
</script>
</body>
</html>
基于layer简单的弹层封装的更多相关文章
- layer系列之弹层layer.prompt
layer官网:https://www.layui.com/doc/modules/layer.html layer在线调试:http://layer.layui.com/ 如何使用layer.pro ...
- layer弹层基本参数初尝试
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 基于layer封装的异步加载分部视图弹出层
背景:之前一直用的artdialog,但是样式不是很好看,后来偶然看到layer,觉得不错,但是对于.net mvc来说,不能像artdialog一样弹出分部视图是很难受的.所以下面的方法就解决了. ...
- layer:web弹出层解决方案
layer:web弹出层解决方案 一.总结 一句话总结:http://layer.layui.com/ 1.layer中弹出层tips的使用(代码)是怎样的? 使用还是比较简单方便的 //tips层- ...
- 弹层组件-layer
layer是Layui的一个弹层组建,功能强大,总之我很喜欢,下面介绍这个组件的基本用法. 首先如果只需要使用layer而不想使用Layui可以单独下载layer组件包,页面引入jquery1.8以上 ...
- Bootstrap~大叔封装的弹层
回到目录 对于Bootstrap的弹层,插件有很多,今天主要用的是它自带的功能,通过bootstrap提供的模式窗口来实现的,而大叔主要对使用方法进行了封装,开发人员可以自己动态传入弹层的HTML内容 ...
- layer遮罩层 简单的遮罩层
在这里提供一个简单layer遮罩层,想深入了解可以进入 layer官网 多多学习哦. 先看下HTML页面代码 <!DOCTYPE html> <html lang="en& ...
- layer实现关闭弹出层刷新父界面功能详解
本文实例讲述了layer实现关闭弹出层刷新父界面功能.分享给大家供大家参考,具体如下: layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会 ...
- 基于Vue.js PC桌面端弹出框组件|vue自定义弹层组件|vue模态框
vue.js构建的轻量级PC网页端交互式弹层组件VLayer. 前段时间有分享过一个vue移动端弹窗组件,今天给大家分享一个最近开发的vue pc端弹出层组件. VLayer 一款集Alert.Dia ...
随机推荐
- 牛客寒假算法基础训练集中营4 E题 Applese 涂颜色
链接:https://ac.nowcoder.com/acm/contest/330/E 来源:牛客网 题目描述 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列 ...
- CSS预处理器—Sass、LESS和Stylus
http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html 一.什么是CSS预处器 CSS预处理器定义了一种新的语言, ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- [转]Centos7 fastdfs/nginx 安装与配置
https://blog.csdn.net/alex_bean/article/details/78625131 参考文章 分布式文件系统-FastDFS 使用FastDFS搭建图片服务器单实例篇 C ...
- 【演示】在CSS里用calc进行计算
请阅读 在CSS里用calc进行计算 下面的元素的width,padding,margin都使用了CSS calc进行计算. 简单计算: 100% – 100px 这是经过简单计算的元素宽度 复杂 ...
- python基础——高级特性
1.切片 切片: >>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] >>> L[:3] ['Michael ...
- pyqt5界面使用
安装配置了pyuic和pyrcc后再进行下面操作 1.打开: 位置(我的):C:\Users\AppData\Roaming\Python\Python35\site-packages\p ...
- IE8兼容问题
最近做的网站,需要兼容IE8,在这里记录一下,碰到的问题,方便以后查看补充 1.CSS选择器nth-child 不兼容 ul li:nth-child(2){ background-image: ur ...
- HDU1814 Peaceful Commission 2-sat
原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...
- C#基础:委托之Action<T>和Func<T>的用法