layui实现类似于bootstrap的模态框功能
以前习惯了bootstrap的模态框,突然换了layui,想的用layui实现类似于bootstrap的模态框功能。
用到了layui的layer模块,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./layui/css/layui.css"> <script type="text/javascript" src="./layui/layui.js"></script>
<script type="text/javascript" src="../JS/jquery-1.8.3.js"></script>
<script> function openModak(){
$("[name='testname']").val("xxxxxxxxxxxxxxx");//向模态框中赋值
layui.use(['layer'],function () {
var layer = layui.layer,$=layui.$;
layer.open({
type:1,//类型
area:['400px','300px'],//定义宽和高
title:'查看详细信息',//题目
shadeClose:false,//点击遮罩层关闭
content: $('#motaikunag')//打开的内容
});
})
}
</script> </head>
<body> <button type="button" onclick="openModak()">开启模态框</button> </body>
</html> <!--模仿bootstrap的模态框-->
<div id="motaikunag" style="display: none;">
<div class="layui-row">
<div class="layui-col-md9">
你的内容 9/12
</div>
<div class="layui-col-md3">
你的内容 3/12
</div>
</div>
<div class="layui-row">
<div class="layui-col-md3">
名字
</div>
<div class="layui-col-md9">
<input type="text" name="testname" value="">
</div>
</div> <br/>
<input type="button" onclick="javascript:alert('点击按钮')" title="点我" value="点我">
</div>
结果:

type - 基本层类型:
类型:Number,默认:0
layer提供了5种层类型。可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层)。 若你采用layer.open({type: 1})方式调用,则type为必填项(信息框除外)
content - 内容
类型:String/DOM/Array,默认:''
content可传入的值是灵活多变的,不仅可以传入普通的html内容,还可以指定DOM,更可以随着type的不同而不同。譬如:
/!*
如果是页面层
*/
layer.open({
type: 1,
content: '传入任意的文本或html' //这里content是一个普通的String
});
layer.open({
type: 1,
content: $('#id') //这里content是一个DOM,注意:最好该元素要存放在body最外层,否则可能被其它的相对元素所影响
});
//Ajax获取
$.post('url', {}, function(str){
layer.open({
type: 1,
content: str //注意,如果str是object,那么需要字符拼接。
});
});
/!*
如果是iframe层
*/
layer.open({
type: 2,
content: 'http://sentsin.com' //这里content是一个URL,如果你不想让iframe出现滚动条,你还可以content: ['http://sentsin.com', 'no']
});
/!*
如果是用layer.open执行tips层
*/
layer.open({
type: 4,
content: ['内容', '#id'] //数组第二项即吸附元素选择器或者DOM
});
补充:如果想更好的设置宽度,可以:
layui.use(['layer'],function () {
var layer = layui.layer;
var width=($(window).width()*0.80);
var height=($(window).height()*0.60);
layer.open({
title:'添加字典',
area: [width+'px', height +'px'],//大小
fix: false, //不固定
maxmin: true,//是否显示最大最小化按钮
shadeClose: false,
shade:0.4,
type:1,
content:$('#addModal')
});
})
area:
类型:String/Array,默认:'auto'
在默认状态下,layer是宽高都自适应的,但当你只想定义宽度时,你可以area: '500px',高度仍然是自适应的。当你宽高都要定义时,你可以area: ['500px', '300px']
为了使模态框的高度自适应而且页面过高出现滚动条,可以采用如下配置:
//2.打开模态框
var width=($(window).width()*0.60);
var top =($(window).height()*0.10);
var index = layer.open({
title:'配置权限',
area: width+'px',//大小定宽,高度自适应
fixed: false, //不固定
maxmin: false,//显示最大最小化按钮
zIndex:1500,//层
offset:top+'px',//定义坐标
scrollbar:true,//显示滚动条
shadeClose: false,//点击外部不关闭
shade:0.4,//阴影
type:1,
content:$('#setPermissionModal')
});
//向页面隐藏index
$("#hidden_setPermission_index").val(index);
为了实现更好的模态框效果,我们可以设置样式:
HTML:
<!--2.修改字典模态框-->
<%--隐藏打开的index--%>
<input type="hidden" id="hidden_update_index">
<div class="x-body" style="display: none" id="updateModal">
<form class="layui-form" id="updateDictionaryForm">
<!--0.隐藏上级字典编号-->
<div class="layui-form-item">
<label for="dictionaryname" class="layui-form-label">
上级字典
</label>
<div class="layui-input-inline">
<input type="text" id="update_updictionaryname" disabled class="layui-input" value="">
<%--隐藏需要修改的字典编号--%>
<input type="hidden" name="dictionaryid" id="update_dictionaryid">
</div>
</div> <!--1.字典名称-->
<div class="layui-form-item">
<label for="dictionaryname" class="layui-form-label">
字典名称
</label>
<div class="layui-input-inline">
<input type="text" id="update_dictionaryname" name="dictionaryname" required="" lay-verify="required"
autocomplete="off" class="layui-input">
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*</span>必须填写
</div>
</div>
<!--2 字典描述-->
<div class="layui-form-item layui-form-text">
<label for="dictionaryname" class="layui-form-label">
字典描述
</label>
<div class="layui-input-inline">
<input type="text" name="description" lay-verify="required" id="update_description"
autocomplete="off" class="layui-input">
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*</span>必须填写
</div>
</div>
<%--3.字典启用状态--%>
<div class="">
<label for="dictionaryname" class="layui-form-label">
字典状态
</label>
<div class="">
<input type="radio" name="isuse" class="update_isuse" value="1" title="启用">
<input type="radio" name="isuse" class="update_isuse" value="0" title="禁用" >
</div>
</div>
<%--提交按钮--%>
<div class="layui-form-item">
<button class="layui-btn" lay-submit lay-filter="updateDictionary" type="button">修改</button>
</div>
</form>
</div>
JS:
/*************S 修改字典相关操作************/
/**
* 打开修改字典信息模态框
* @param obj 将修改按钮自己传下来
*/
function openUpdateDict(obj){
var tr= $(obj).parent().parent();//获取到tr元素
var update_dictionaryid = tr.children("td:eq(0)").children("input:hidden").val();//字典变
var update_dictionaryname = tr.children("td:eq(2)").text();//字典名称
var update_updictionaryname = tr.children("td:eq(3)").text();//上级字典名称
var update_description = tr.children("td:eq(4)").text();//字典描述
var update_isuse = tr.children("td:eq(5)").children("input:hidden").val();//字典状态
$("#update_dictionaryid").val(update_dictionaryid);
$("#update_dictionaryname").val(update_dictionaryname);
$("#update_updictionaryname").val(update_updictionaryname);
$("#update_description").val(update_description);
/* $(".update_isuse").each(function () {
if($(this).val()==update_isuse){
alert($(this).val())
$(this).prop("chcked","checked");
}
});*/
$(".update_isuse:radio").removeAttr("checked");//删除checked属性
$(".update_isuse:radio[value='"+update_isuse+"']").attr("checked", true);
form.render('radio'); //重新渲染radio单选框
var width=($(window).width()*0.80);
var height=($(window).height()*0.70);
var index = layer.open({
title:'修改字典信息',
area: [width+'px', height +'px'],//大小
fix: true, //不固定
maxmin: true,
zIndex:500,
shadeClose: false,
shade:0.4,
type:1,
content:$('#updateModal')
});
//向页面隐藏index
$("#hidden_update_index").val(index);
} //监听修改表单的提交事件
layui.use(['layer','form'],function () {
var layer = layui.layer,form = layui.form;
form.on('submit(updateDictionary)', function(data){
$.ajax({
url:contextPath+"/dictionary/updateDict.do",
data:data.field,
type:'post',
datatype:'text',
success:function (response) {
layer.msg(response,{time:1500},function () {
if("修改成功"==response){
layer.close($("#hidden_update_index").val()); //关闭当前窗口
window.location.reload();//刷新页面
}
});
}
});
});
})
/*************E 修改字典相关操作************/
关于更多的参数解释参考:http://www.layui.com/doc/modules/layer.html
layui实现类似于bootstrap的模态框功能的更多相关文章
- WebUploader 上传插件结合bootstrap的模态框使用时选择上传文件按钮无效问题的解决方法
由于种种原因(工作忙,要锻炼健身,要看书,要学习其他兴趣爱好,谈恋爱等),博客已经好久没有更新,为这个内心一直感觉很愧疚,今天开始决定继续更新博客,每周至少一篇,最多不限篇幅. 今天说一下,下午在工作 ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- bootstrap的模态框的使用
bootstrap的模态框 如果只想单独使用模态框功能,可以单独引入modal.js,和bootstrap的css,在bootstrap的包中,可引入bootstrap.js. 用法 通过data属性 ...
- Bootstrap 3 模态框播放视频
Bootstrap 3 模态框播放视频 I'm trying to use the Modal feature from Bootstrap 3 to show my Youtube video. I ...
- Bootstrap 实例 - 模态框(Modal)插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- bootstrap的模态框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Bootstrap -- 插件: 模态框、滚动监听、标签页
Bootstrap -- 插件: 模态框.滚动监听.标签页 1. 模态框(Modal): 覆盖在父窗体上的子窗体. 使用模态框: <!DOCTYPE html> <html> ...
- Bootstrap中模态框多层嵌套时滚动条问题
在使用Bootstrap中模态框过程中,如果出现多层嵌套的时候,如打开模态框A,然后在A中打开模态框B,在关闭B之后,如果A的内容比较多,滚动条会消失,而变为Body的滚动条,这是由于模态框自带的遮罩 ...
- 黄聪:bootstrap中模态框modal在苹果手机上会失效
bootstrap中模态框在苹果手机上会失效 可将代码修改为<a data-toggle="modal" data-target="#wrap" hre ...
随机推荐
- 详解Python闭包,装饰器及类装饰器
在项目开发中,总会遇到在原代码的基础上添加额外的功能模块,原有的代码也许是很久以前所写,为了添加新功能的代码块,您一般还得重新熟悉源代码,稍微搞清楚一点它的逻辑,这无疑是一件特别头疼的事情.今天我们介 ...
- TP中循环遍历
循环遍历(重点) 在ThinkPHP中系统提供了2个标签来实现数组在模版中的遍历: volist标签.foreach标签. Volist语法格式: Foreach语法格式: 从上述的语法格式发现vol ...
- 第112天:javascript中函数预解析和执行阶段
关于javascript中的函数: 1.预解析:把所有的函数定义提前,所有的变量声明提前,变量的赋值不提前 2.执行 :从上到下执行,但有例外(setTimeout,setInterval,aja ...
- jmeter同步定时器
同步定时器是jmeter中一个比较重要的定时器,同步定时器,相当于一个储蓄池,累积一定的请求,当在规定的时间内达到一定的线程数量,这些线程会在同一个时间点一起并发,可以用来做大数据量的并发请求. 验证 ...
- 题解 P1423 【小玉在游泳】
这道题可以用简单的蒟蒻do-while循环,方式:直到型 因为是萌新/蒟蒻,所以并不知道这道题的时间/空间复杂度是多大 脚踩std(^▽^)摩擦 #include <iostream> # ...
- 洛谷 P1495 曹冲养猪
这是一道标准的孙子定理的题,题意浅显,思路明确 然后我就交了整整16遍啊,欺负人啊,题解暴力就能过,我就TLE ..悲惨的提交记录 下面是题面 题目描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干 ...
- Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS Memory Limit: 131072K To ...
- 框架----Django框架知识点整理
一.cbv cbv(class-base-view) 基于类的视图 fbv(func-base-view) 基于函数的视图 a.基本演示 urlpatterns = [ url(r'^login.ht ...
- codeforces 691F 暴力
传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...
- nginx日志增加cookie信息
一.获取全部cookie信息 这个比较方便,直接在nginx.conf文件中添加$http_cookie log_format main '[$time_local] - $remote_addr:$ ...