1 LayUI 概述
在官网首页,可以很方便的下载LayUI
LayUI 是一款经典模块化前端 UI 框架,我们只需要定义简单的HTML、CSS、JS即可实现很复杂的前端效果。
使得前端页面的制作变得更加简单。
2 layUI环境搭建
2.1 下载
在官网即可完成下载https://www.layui.com/
2.2 导入依赖
下载的LayUI解压后,将其中的layui目录导入项目中
<!-- 引入layUI的css --> <link href="layui/css/layui.css" rel="stylesheet"> <!-- 引入layUI的js核心库 --> <script src="layui/layui.js"></script>
2 layUI布局
- layUI的布局容器
- layui-container : 带有左右留白的布局容器
- layui-fluid : 占满屏幕的布局容器
- 栅格系统
- 响应式栅格布局,每行分 12 等分
布局容器
<div class="layui-container" style="height: 300px;background: #123456"> </div> <div class="layui-fluid" style="height: 300px;background: #123456"> </div>
响应式布局:移动设备、平板、桌面端的不同表现
<div class="layui-row"> <div class="layui-col-xs6 layui-col-sm6 layui-col-md4"> 移动:6/12 | 平板:6/12 | 桌面:4/12 </div> <div class="layui-col-xs6 layui-col-sm6 layui-col-md4"> 移动:6/12 | 平板:6/12 | 桌面:4/12 </div> <div class="layui-col-xs4 layui-col-sm12 layui-col-md4"> 移动:4/12 | 平板:12/12 | 桌面:4/12 </div> <div class="layui-col-xs4 layui-col-sm7 layui-col-md8"> 移动:4/12 | 平板:7/12 | 桌面:8/12 </div> <div class="layui-col-xs4 layui-col-sm5 layui-col-md4"> 移动:4/12 | 平板:5/12 | 桌面:4/12 </div> </div>
栅格列间距
<div class="layui-row layui-col-space10"> <div class="layui-col-md4"> <div style="background: #567890">1/3</div> </div> <div class="layui-col-md4"> <div style="background: #567890">1/3</div> </div> <div class="layui-col-md4" > <div style="background: #aabbcc">1/3</div> </div> </div>
列偏移
<div class="layui-row"> <div class="layui-col-md4"> <div style="background: #567890">1/3</div> </div> <div class="layui-col-md4 layui-col-md-offset4"> <div style="background: #aabbcc">1/3</div> </div> </div>
列嵌套
<div class="layui-row layui-col-space5"> <div class="layui-col-md5"> <div class="layui-row"> <div class="layui-col-md3"> 内部列 </div> <div class="layui-col-md9"> 内部列 </div> <div class="layui-col-md12"> 内部列 </div> </div> </div> <div class="layui-col-md7"> <div class="layui-row"> <div class="layui-col-md12"> 内部列 </div> <div class="layui-col-md9"> 内部列 </div> <div class="layui-col-md3"> 内部列 </div> </div> </div> </div>
4 layUI按钮
<!-- 按钮基本用法 --> <button type="button" class="layui-btn">一个标准的按钮</button> <a href="http://www.layui.com" class="layui-btn">一个可跳转的按钮</a> <hr> <!-- 常用的按钮样式 --> <div> <button type="button" class="layui-btn layui-btn-primary">原始按钮</button> <button type="button" class="layui-btn">默认按钮</button> <button type="button" class="layui-btn layui-btn-normal">百搭按钮</button> <button type="button" class="layui-btn layui-btn-warm">暖色按钮</button> <button type="button" class="layui-btn layui-btn-danger">警告按钮</button> <button type="button" class="layui-btn layui-btn-disabled">禁用按钮</button> </div> <hr> <!-- 按钮尺寸 --> <button type="button" class="layui-btn layui-btn-primary layui-btn-lg">大型按钮</button> <button type="button" class="layui-btn layui-btn-primary">默认按钮</button> <button type="button" class="layui-btn layui-btn-danger layui-btn-sm">小型按钮</button> <button type="button" class="layui-btn layui-btn-primary layui-btn-xs">迷你按钮</button> <hr> <!-- 圆角按钮 --> <button type="button" class="layui-btn layui-btn-primary layui-btn-radius">原始按钮</button> <button type="button" class="layui-btn layui-btn-radius">默认按钮</button> <button type="button" class="layui-btn layui-btn-normal layui-btn-radius">百搭按钮</button> <button type="button" class="layui-btn layui-btn-warm layui-btn-radius">暖色按钮</button> <button type="button" class="layui-btn layui-btn-danger layui-btn-radius">警告按钮</button> <button type="button" class="layui-btn layui-btn-disabled layui-btn-radius">禁用按钮</button> <hr> <!-- 带图标按钮 --> <button type="button" class="layui-btn"><i class="layui-icon"></i></button> <button type="button" class="layui-btn"><i class="layui-icon layui-icon-email"></i></button> <button type="button" class="layui-btn"><i class="layui-icon"></i></button> <button type="button" class="layui-btn"><i class="layui-icon"></i></button> <button type="button" class="layui-btn"><i class="layui-icon"></i></button> <button type="button" class="layui-btn"><i class="layui-icon"></i></button> <hr> <!-- 按钮组 --> <div class="layui-btn-group"> <button type="button" class="layui-btn">增加</button> <button type="button" class="layui-btn ">编辑</button> <button type="button" class="layui-btn">删除</button> </div> <hr> <!--带图标的按钮组--> <div class="layui-btn-group"> <button type="button" class="layui-btn layui-btn-normal"> <i class="layui-icon layui-icon-mike"></i> </button> <button type="button" class="layui-btn layui-btn-warm"> <i class="layui-icon layui-icon-logout"></i> </button> <button type="button" class="layui-btn layui-btn-danger"> <i class="layui-icon layui-icon-username"></i> </button> </div>
5 layUI表单
样式:
- 表单样式:layui-form
- 表单项: layui-form-item
- 表单元素块:layui-input-block
行内表单元素: layui-input-inline
属性:
必填属性:lay-verify=“required”
关闭输入框提示:autocomplete=“off”
设置复选框的风格: lay-skin=“primary”
事件过滤器:lay-filter
<form class="layui-form"> <div class="layui-form-item"> <label class="layui-form-label" for="name">输入框</label> <div class="layui-input-block"> <input id="name" type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">标签区域</label> <div class="layui-input-inline"> <input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">联动选择框</label> <div class="layui-input-inline"> <select name="quiz1"> <option value="">请选择省</option> <option value="浙江" selected="">浙江省</option> <option value="你的工号">江西省</option> <option value="你最喜欢的老师">福建省</option> </select> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">复选框</label> <div class="layui-input-block"> <input type="checkbox" name="like[write]" title="写作"> <input type="checkbox" name="like[read]" title="阅读" checked> <input type="checkbox" name="like[game]" title="游戏"> </div> </div> <div class="layui-form-item" pane=""> <label class="layui-form-label">原始复选框</label> <div class="layui-input-block"> <input type="checkbox" name="like1[write]" lay-skin="primary" title="写作" checked=""> <input type="checkbox" name="like1[read]" lay-skin="primary" title="阅读"> <input type="checkbox" name="like1[game]" lay-skin="primary" title="游戏" disabled=""> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">开关-默认关</label> <div class="layui-input-block"> <input type="checkbox" name="close" lay-skin="switch" lay-text="同意|不同意"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">开关-默认开</label> <div class="layui-input-block"> <input type="checkbox" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF"> </div> </div> <div class="layui-form-item layui-form-text"> <label class="layui-form-label">普通文本域</label> <div class="layui-input-block"> <textarea placeholder="请输入内容" class="layui-textarea"></textarea> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button> <button type="reset" class="layui-btn layui-btn-primary">重置</button> </div> </div> </form> <script> layui.use(['form', 'layedit', 'laydate'], function() { var form = layui.form; }) </script>
表单监听提交
<form class="layui-form"> <div class="layui-form-item"> <label class="layui-form-label" for="name">用户名</label> <div class="layui-input-block"> <input id="name" type="text" name="username" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密码</label> <div class="layui-input-inline"> <input type="password" name="password" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">下拉选择框</label> <div class="layui-input-inline"> <select name="city"> <option value="" disabled>请选择省</option> <option value="浙江省" selected="">浙江省</option> <option value="江西省">江西省</option> <option value="福建省">福建省</option> </select> </div> </div> <div class="layui-form-item layui-form-text"> <label class="layui-form-label">普通文本域</label> <div class="layui-input-block"> <textarea placeholder="请输入内容" name="info" class="layui-textarea"></textarea> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button> <button type="reset" class="layui-btn layui-btn-primary">重置</button> </div> </div> </form> <script> layui.use(['form', 'layedit', 'laydate'], function() { var form = layui.form; //监听提交 form.on('submit(demo1)', function(data){ layer.alert(JSON.stringify(data.field), { title: '最终的提交信息' }) return false; }); }) </script>
存值、取值表单
<form class="layui-form" action="" lay-filter="example"> <div class="layui-form-item"> <label class="layui-form-label">输入框</label> <div class="layui-input-block"> <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密码框</label> <div class="layui-input-block"> <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">选择框</label> <div class="layui-input-block"> <select name="interest" lay-filter="aihao"> <option value=""></option> <option value="0">写作</option> <option value="1">阅读</option> <option value="2">游戏</option> <option value="3">音乐</option> <option value="4">旅行</option> </select> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">复选框</label> <div class="layui-input-block"> <input type="checkbox" name="like[write]" title="写作"> <input type="checkbox" name="like[read]" title="阅读"> <input type="checkbox" name="like[daze]" title="发呆"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">开关</label> <div class="layui-input-block"> <input type="checkbox" name="close" lay-skin="switch" lay-text="ON|OFF"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">单选框</label> <div class="layui-input-block"> <input type="radio" name="sex" value="男" title="男" checked=""> <input type="radio" name="sex" value="女" title="女"> </div> </div> <div class="layui-form-item layui-form-text"> <label class="layui-form-label">文本域</label> <div class="layui-input-block"> <textarea placeholder="请输入内容" class="layui-textarea" name="desc"></textarea> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-setval">赋值</button> <button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-getval">取值</button> <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button> </div> </div> </form> <script> layui.use('form',function () { var form = layui.form; //表单赋值 layui.$('#LAY-component-form-setval').on('click', function(){ form.val('example', { "username": "贤心" // "name": "value" ,"password": "123456" ,"interest": 1 ,"like[write]": true //复选框选中状态 ,"close": true //开关状态 ,"sex": "女" ,"desc": "我爱 layui" }); }); //表单取值 layui.$('#LAY-component-form-getval').on('click', function(){ var data = form.val('example'); //异步请求的操作 //将json对象转换成json字符串 alert(JSON.stringify(data)); }); }) </script>
有些时候,你的有些表单元素可能是动态插入的。这时 form 模块 的自动化渲染是会对其失效的。
6 layUI导航
普通导航
样式:
- layui-nav : 导航
- layui-bg-green : 导航背景
- layui-nav-item : 导航菜单
- layui-this : 表示默认选中
- layui-nav-child :导航二级菜单
<!-- 普通导航 --> <ul class="layui-nav" lay-filter=""> <li class="layui-nav-item"><a href="">最新活动</a></li> <li class="layui-nav-item"><a href="">产品</a></li> <li class="layui-nav-item layui-this"><a href="">大数据</a></li> <li class="layui-nav-item"> <a href="javascript:;">解决方案</a> <dl class="layui-nav-child"> <!-- 二级菜单 --> <dd><a href="">移动模块</a></dd> <dd><a href="">后台模版</a></dd> <dd><a href="">电商平台</a></dd> </dl> </li> <li class="layui-nav-item"><a href="">社区</a></li> </ul> <script> layui.use('element',function () { var element = layui.element; }) </script>
带徽章的导航
样式:
- layui-badge:徽章方块效果
- layui-badge-dot:徽章 点效果
- layui-nav-img:导航图片
<ul class="layui-nav layui-bg-green"> <li class="layui-nav-item"> <a href="">控制台<span class="layui-badge">9</span></a> </li> <li class="layui-nav-item"> <a href="">个人中心<span class="layui-badge-dot"></span></a> </li> <li class="layui-nav-item"> <a href=""><img src="//t.cn/RCzsdCq" class="layui-nav-img">我</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">修改信息</a></dd> <dd><a href="javascript:;">安全管理</a></dd> <dd><a href="javascript:;">退了</a></dd> </dl> </li> </ul>
垂直导航
样式:
- layui-nav-tree : 垂直导航
- layui-nav-itemed: 默认展开
- layui-nav-side: 侧边导航
<ul class="layui-nav layui-nav-tree" lay-filter="test"> <!-- 侧边导航: <ul class="layui-nav layui-nav-tree layui-nav-side"> --> <li class="layui-nav-item layui-nav-itemed"> <a href="javascript:;">默认展开</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">选项1</a></dd> <dd><a href="javascript:;">选项2</a></dd> <dd><a href="">跳转</a></dd> </dl> </li> <li class="layui-nav-item"> <a href="javascript:;">解决方案</a> <dl class="layui-nav-child"> <dd><a href="">移动模块</a></dd> <dd><a href="">后台模版</a></dd> <dd><a href="">电商平台</a></dd> </dl> </li> <li class="layui-nav-item"><a href="">产品</a></li> <li class="layui-nav-item"><a href="">大数据</a></li> </ul>
面包屑导航
样式:
- layui-breadcrumb :面包屑导航
- lay-separator :分隔符
<!-- 面包屑导航 layui-breadcrumb --> <span class="layui-breadcrumb" lay-separator="----"> <a href="">首页</a> <a href="">国际新闻</a> <a href="">亚太地区</a> <a><cite>正文</cite></a> </span>
后端程序员的前端捷径-超级容易上手使用的前端框架layUI(下):https://developer.aliyun.com/article/1580665