以前觉得自己手写一个类似layui的弹出层是挺遥远的事,因为完全没有头绪,即便在layui官网知道layui使用的都是C3动画
之前试过控制width:0;height:0来做动画,结果惨不忍睹,直到几天前灵机一动联想到了transform的scale属性,才稍微触及到了皮毛

为了不添加格外的HTML结构,所以弹出层也是动态生成
layui弹出框和遮罩层是同级结构,而我把弹出框放遮罩层里了,所以关闭时要用animationend来监听,弹出框做完动画后才删除遮罩层
确认框confirm之前也想跟原生confirm一样,通过返回布尔值来进行流程控制,结果为undefined,因为在调用时就已经返回了,而不是点了“确定“”才有返回值,这里和原生的不一样,所以跟layui一样使用callback

HTML

<input type="button" value="模拟layui弹出层" id="btn-alert">

JS

 function MsgAlert() {this.alert = function (msg, time) {var delay = time || 1200,that = this;$('body').append('<div id="msgAlertMask">\n' +'    <div id="msgAlert">\n' +'        <div class="title">\n' +'            <span class="close">×</span>\n' +'        </div>\n' +'        <div class="content">' + msg + '</div>\n' +'    </div>\n' +'</div>');$('#msgAlert').addClass('alert-show');$('#msgAlert').on('click', '.close', function () {that.destroy();});setTimeout(function () {that.destroy();}, delay);};this.confirm = function (msg, callback) {var that = this;$('body').append('<div id="msgAlertMask">\n' +'    <div id="msgAlert">\n' +'        <div class="title">\n' +'            <span class="close">×</span>\n' +'        </div>\n' +'        <div class="content">' + msg + '</div>\n' +'        <div class="btn-box">\n' +'            <input type="button" value="确定" class="ok">\n' +'            <input type="button" value="取消" class="cancel">\n' +'        </div>\n' +'    </div>\n' +'</div>');$('#msgAlert').addClass('alert-show');$('#msgAlert').on('click', '.ok', function () {callback();});$('#msgAlert').on('click', '.close', function () {that.destroy();});$('#msgAlert').on('click','.cancel',function () {that.destroy();})};this.destroy = function () {$('#msgAlert').on('animationend', function () {$('#msgAlertMask').remove();});$('#msgAlert').addClass('alert-hide');}}window.pop = new MsgAlert();$('#btn-alert').click(function () {pop.alert('你说呢');})

CSS

<style>#msgAlertMask {position: fixed;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.3);z-index: 999;}#msgAlert {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);min-width: 300px;background: #fff;-webkit-background-clip: content;border-radius: 2px;box-shadow: 1px 1px 50px rgba(0, 0, 0, .3);}#msgAlert .title {padding: 0 14px 0 20px;height: 42px;line-height: 42px;border-bottom: 1px solid #eee;font-size: 14px;color: #333;overflow: hidden;background-color: #F8F8F8;border-radius: 2px 2px 0 0;text-align: right;}#msgAlert .title .close {font-size: 24px;cursor: pointer;}#msgAlert .content {padding: 30px 24px 40px;}#msgAlert .btn-box {text-align: right;padding: 0 15px 12px;pointer-events: auto;user-select: none;-webkit-user-select: none;}#msgAlert .btn-box input {height: 28px;line-height: 28px;margin: 5px 5px 0;padding: 0 15px;border: 1px solid #dedede;background-color: #fff;color: #333;border-radius: 2px;font-weight: 400;cursor: pointer;text-decoration: none;}#msgAlert .btn-box .ok {border-color: #1E9FFF;background-color: #1E9FFF;color: #fff;}.alert-show {animation: alert-show 0.1s ease-out forwards;}.alert-hide {animation: alert-hide 0.1s ease-out forwards;}@keyframes alert-show {0% {opacity: 0;transform: translate(-50%, -50%) scale(0);}100% {opacity: 1;transform: translate(-50%, -50%) scale(1);}}@keyframes alert-hide {0% {opacity: 1;transform: translate(-50%, -50%) scale(1);}100% {opacity: 0;transform: translate(-50%, -50%) scale(0);}}</style>

总结:

  1. 动画效果还不够好,而且有bug,确认框回调里使用提示框的话会有问题,因为两者用的是同样的id,这个可能需要动态生成id来解决冲突问题
  2. 弹出框鼠标拖拽、窗口resize()事件、初始化时的参数设置等等也没有
  3. 通过transform:translate(-50%,-50%)方式居中好像会出现字和边框模糊的问题,难道layui是为了避免此问题,才动态给弹出框赋top、left?
  4. 还差得太远,继续加油吧

转载于:https://www.cnblogs.com/Grani/p/11029416.html

模拟layui弹出层相关推荐

  1. layui弹出层:使用icon图标小结

    文章目录 layui弹出层:icon图标小结 表格示下: 调用代码 · 效果图 · 示下: layui弹出层:icon图标小结 layui的弹框插件layer中,有很多不同场景需要的弹框icon图标, ...

  2. layui弹出层:皮肤扩展(文档解读)

    layui弹出层:皮肤扩展(文档解读) layui弹出层:样式自定义 官方文档:https://layer.layui.com/ 皮肤扩展 · 官方教程: 官方文档:https://layer.lay ...

  3. layui弹出层:倒计时后自动关闭(含代码、案例)

    layui弹出层:倒计时后自动关闭(含代码.案例) 问题描述: 如何设定倒计时? 如何在自动倒计时结束后关闭弹框: 如何自定义"按钮文字"文案: 如何在关闭后,执行函数事件: 如何 ...

  4. layui弹出层之layer.open弹出iframe窗口·

    layui弹出层之layer.open弹出iframe窗口· layer.open弹出iframe窗口 [1]该功能为案例,弹出iframe窗口显示上传数据 由于layui的弹出层每次执行layer. ...

  5. 如何更改layui弹出层样式

    首先引入插件: js: <script src="./layui/src/layui.js"></script> 然后设置layui弹出层内容: layer ...

  6. Layui 弹出层插件

    Layui 弹出层插件 开发工具与关键技术: Visual Studio 2015 – Layui 作者:廖亚星 撰写时间:2019年 6 月4日 这段时间在做项目页面的搭建,在页面的操作中,会出现很 ...

  7. layui 弹出层 shade

    layui 弹出层 遮罩在弹出层上面 var index = layer.open({type: 1,skin: 'demo-class',title: "正在生成会议投图任务", ...

  8. layui弹出层中添加下拉框

    layui弹出层中添加下拉框 实习不到两个月,踩到的坑 使用layui写了一个项目,负责其中的一小块,其中有一个页面需要在弹出层中写一个下拉框 代码如下: <div class="la ...

  9. layui弹出层,点击关闭按钮确无法关闭

    layui弹出层,前2次可正常关闭,但是从第3次开始无论点击什么,都只是出现一个遮罩,无法点击其他, 但是esc可正常关闭弹出层 不清楚原因 shade: 0 将遮罩关闭,就正常了

最新文章

  1. [abap] 通过动态参数获取字段数据
  2. Linux 下 Redis 6 的安装使用(Ubuntu 18.04)
  3. java小编程----盛最多水的容器
  4. 勒索病毒GANDCRAB新变种GANDCRAB V5.2新变种来袭 你中招了吗?
  5. 前端学习(701):循环小总结
  6. Django Python MySQL Linux 开发环境搭建
  7. volatile的应用
  8. 普通路由器改4g路由器_4G宽带随心用,办公娱乐更自由,蒲公英X4C路由器体验|路由器|蒲公英|宽带|wifi|sim...
  9. python安装方法_Python如何批量更新已安装的库,为你介绍六种方法!
  10. [BZOJ2502]清理雪道
  11. oracle设置默认值为当前时间_把锁屏密码设置成当前时间,随时间永远变动!
  12. 基于python sklearn的 RandomForest随机森林 类实现
  13. 同余方程-NOIP2012TGD2T1
  14. selenium phantomjs
  15. 北航计算机考研计算机组成原理,北航计算机组成原理讲义.pdf
  16. CUDA编程-02: 初识CUDA编程
  17. openGauss数据库基本操作
  18. 信息学奥赛一本通|1179:奖学金
  19. 北京地铁,把什么丢了?
  20. 后端给base64码,在前端显示成图片

热门文章

  1. 基于吉日嘎拉的通用权限管理WebForm版扩展:字典选项管理和缓存管理
  2. android zip解压缩
  3. java 关于分页的实现
  4. 编译原理实验(算符优先文法)
  5. Android studio中正确引入so文件的方法
  6. 【转】android的消息处理机制(图+源码分析)——Looper,Handler,Message
  7. ListItem.Update与ListItem.SystemUpdate
  8. 缓存初解(四)---Ibatis的缓存配置+Ehcache
  9. Binary Tree Level Order Traversal II leetcode java
  10. webshpere MQ linux 上安装