javascript实现好看的可复用弹窗插件
更新时间:2022年5月8日 23:27 点击:685 作者:mondayes
本文实例为大家分享了javascript实现可复用弹窗插件的具体代码,供大家参考,具体内容如下
效果图
下面是详细代码
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>index</title> <link rel="stylesheet" href="./componet.css" > </head> <body> <button id="button"> 弹窗 </button> <script src="./componet.js"></script> <script> var btn = document.querySelector("#button"); btn.addEventListener("click", function() { new AlertBox({ message: "哈哈哈哈哈哈", success: "确认", cancel: "取消", successcallback: function() { console.log("确认。。。。。。。。。") }, cancelcallback: function() { console.log("取消。。。。。。。。。。。") } }); }) </script> </body> </html>
componet.css
.alert-Box{ width: 250px; height: 150px; position: absolute; top: 50%; left: 50%; margin: -75px 0 0 -125px; box-shadow: 0px 0px 10px 7px #ced6e0; border-radius: 5px; background-color: #ffffff; } .alert-Box-message{ height:108px; text-align: center; line-height: 108px; font-size: 14px; } .alert-Box-buttonwrap{ height: 40px; display: flex; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; overflow: hidden; } .alert-Box-button{ height: inherit; text-align: center; line-height: 40px; flex:1; cursor: pointer; font-size: 14px; background-color: white; border-top: solid 1px #ced6e0; transition: background-color 0.5s; } .alert-Box-button:hover{ background-color: #dee1e6; color: rgb(88, 88, 88); } .alert-Box-button:nth-child(1){ border-right: solid 1px #ced6e0; } .alert-Box-show{ -webkit-animation: alert-show 0.3s; -webkit-animation-fill-mode: forwards; animation: alert-show 0.3s; animation-fill-mode: forwards; } .alert-Box-hidden{ -webkit-animation: alert-hidden 0.3s; -webkit-animation-fill-mode: forwards; animation: alert-hidden 0.3s; animation-fill-mode: forwards; } @keyframes alert-show{ from{transform: scale(0);} to{transform: scale(1);} } @-webkit-keyframes alert-show{ from{transform: scale(0);} to{transform: scale(1);} } @keyframes alert-hidden{ from{transform: scale(1);} to{transform: scale(0);} } @-webkit-keyframes alert-hidden{ from{transform: scale(1);} to{transform: scale(0);} }
componet.js
function AlertBox(options) { this.message = options.message; this.callback = options.callback; this.success = options.success; this.cancel = options.cancel; this.successcallback = options.successcallback; this.cancelcallback = options.cancelcallback; this.createBox(); this.buttonaddEvent(); } AlertBox.prototype.createBox = function() { let body = document.getElementsByTagName("body")[0]; this.fragment = document.createDocumentFragment(); this.Box = crE("div"); this.Box.classList.add("alert-Box", "alert-Box-show"); let message = crE("div"); message.textContent = this.message; message.classList.add("alert-Box-message"); this.Box.appendChild(message); let buttonwrap = crE("div"); buttonwrap.classList.add("alert-Box-buttonwrap"); this.successBtn = crE("div"); this.successBtn.classList.add("alert-Box-button"); this.successBtn.textContent = this.success || "确认"; buttonwrap.appendChild(this.successBtn); if (this.cancel) { this.cancelBtn = crE("div"); this.cancelBtn.classList.add("alert-Box-button"); this.cancelBtn.textContent = this.cancel; buttonwrap.appendChild(this.cancelBtn); } this.Box.appendChild(buttonwrap); this.fragment.appendChild(this.Box); body.appendChild(this.fragment); } AlertBox.prototype.buttonaddEvent = function() { this.successBtn.addEventListener("click", () => { let fn = this.successcallback; fn(); this.Box.classList.add("alert-Box-hidden"); setTimeout(() => { remE(this.Box); }, 310) }); if (this.cancel) { this.cancelBtn.addEventListener("click", () => { let fn = this.cancelcallback; fn(); this.Box.classList.add("alert-Box-hidden"); setTimeout(() => { remE(this.Box); }, 310) }); } } function crE(element) { return document.createElement(element); } function remE(element) { document.body.removeChild(element); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
原文出处:https://blog.csdn.net/tdl2793958525/article/details/10363706
上一篇: vue计算属性及函数的选择
下一篇: js+canvas实现飞机大战
相关文章
- 本篇文章主要分享了通过window.navigator来判断浏览器及其版本信息的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧...2017-01-23
- 这篇文章主要介绍了js如何实现浏览器打印功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-15
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- 作为前端,一直以来都知道HTTP劫持与XSS跨站脚本、CSRF跨站请求伪造。防御这些劫持最好的方法是从后端入手,前端能做的太少。而且由于源码的暴露,攻击者很容易绕过防御手段。但这不代表我们去了解这块的相关知识是没意义的,本文的许多方法,用在其他方面也是大有作用。...2021-05-24
- 这篇文章主要给大家介绍了关于Nest.js参数校验和自定义返回数据格式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-28
- 那么今天我就用JavaScript代码来实现这个效果吧,那么首先介绍一下整个的思路,首先我们先将确定输入密码的位数,我的需求是5位,那么就用一个div标签包住5个input标签...2016-01-02
- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- 这篇文章主要为大家详细介绍了js+css实现回到顶部按钮back to top回到顶部按钮,感兴趣的小伙伴们可以参考一下...2016-03-03
- 这篇文章主要给大家介绍了一个关于JS正则匹配的踩坑记录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-13
- 这篇文章主要介绍了如何使用JavaScript实现“无缝滚动 自动播放”轮播图效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-20
- 这篇文章主要介绍了js实现调用网络摄像头及常见错误处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-07
- 这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
- 这篇文章主要为大家详细介绍了js实现列表按字母排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-11
- 这篇文章主要介绍了js组件SlotMachine实现图片切换效果制作抽奖系统的相关资料,需要的朋友可以参考下...2016-04-19
- 这篇文章主要介绍了基于JavaScript实现文字超出部分隐藏 的相关资料,需要的朋友可以参考下...2016-03-01
- Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
- 有时候我们需要屏蔽客户端的F12,以防菜鸟也可以随意修改我们的代码,也处于源码的保护等操作,这里就为大家分享一下常见的代码...2020-10-03
- 为了网站的安全性,很多朋友都把密码设的比较复杂,但是如何密码不能明显示,不知道输的是对是错,为了安全起见可以把密码显示的,那么基于js代码如何实现的呢?下面通过本文给大家介绍JavaScript实现表单密码的隐藏和显示,需要的朋友参考下...2016-03-03
- 这篇文章主要介绍了JS实现响应鼠标点击动画渐变弹出层效果代码,具有非常自然流畅的动画过度效果,涉及JavaScript针对鼠标事件的响应及页面元素样式的动态操作相关技巧,需要的朋友可以参考下...2016-03-28
- 这篇文章主要介绍了JS创建Tag标签的方法,结合具体实例形式分析了javascript动态操作页面HTML元素实现tag标签功能的步骤与相关操作技巧,需要的朋友可以参考下...2017-06-15