javascript實(shí)現(xiàn)好看的可復(fù)用彈窗插件
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)可復(fù)用彈窗插件的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
下面是詳細(xì)代碼
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: "確認(rèn)", ?? ??? ??? ??? ??? ?cancel: "取消", ?? ??? ??? ??? ??? ?successcallback: function() { ?? ??? ??? ??? ??? ??? ?console.log("確認(rèn)。。。。。。。。。") ?? ??? ??? ??? ??? ?}, ?? ??? ??? ??? ??? ?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 || "確認(rèn)"; ?? ?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); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js彈出框、對(duì)話框、提示框、彈窗實(shí)現(xiàn)方法總結(jié)(推薦)
- js彈窗返回值詳解(window.open方式)
- JS實(shí)現(xiàn)自定義彈窗功能
- js彈窗代碼 可以指定彈出間隔
- JS彈窗 JS彈出DIV并使整個(gè)頁(yè)面背景變暗功能的實(shí)現(xiàn)代碼
- js退出彈窗代碼集合
- js調(diào)用父框架函數(shù)與彈窗調(diào)用父頁(yè)面函數(shù)的簡(jiǎn)單方法
- js點(diǎn)擊彈出div層實(shí)現(xiàn)可拖曳的彈窗效果
- 關(guān)于vue.js彈窗組件的知識(shí)點(diǎn)總結(jié)
- JS使用遮罩實(shí)現(xiàn)點(diǎn)擊某區(qū)域以外時(shí)彈窗的彈出與關(guān)閉功能示例
相關(guān)文章
解決webpack無(wú)法通過(guò)IP地址訪問(wèn)localhost的問(wèn)題
下面小編就為大家分享一篇解決webpack無(wú)法通過(guò)IP地址訪問(wèn)localhost的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02詳解JavaScript基于面向?qū)ο笾^承實(shí)例
這篇文章主要介紹了JavaScript基于面向?qū)ο笾^承實(shí)例,需要的朋友可以參考下2015-12-12js的隱含參數(shù)(arguments,callee,caller)使用方法
本篇文章只要是對(duì)js的隱含參數(shù)(arguments,callee,caller)使用方法進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01利用jsonp與代理服務(wù)器方案解決跨域問(wèn)題
這篇文章主要給大家介紹了關(guān)于利用jsonp與代理服務(wù)器方案解決跨域問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09使用documentElement正確取得當(dāng)前可見(jiàn)區(qū)域的大小
如何取得當(dāng)前瀏覽器里面可見(jiàn)區(qū)域的大?。科渌椒ǘ疾贿m用,只有documentElement才可以,需要的朋友可以參考下2014-07-07鼠標(biāo)懸浮停留三秒后自動(dòng)顯示大圖js代碼
這篇文章主要介紹了鼠標(biāo)懸浮停留三秒后顯示大圖,在網(wǎng)頁(yè)中還是比較實(shí)用的,下面是示例代碼2014-09-09IE7中javascript操作CheckBox的checked=true不打勾的解決方法
在IE7下,生成的Checkbox無(wú)法正確的打上勾。 原因是 chkbox控件還沒(méi)初始化(appendChild),就開(kāi)始操作它的結(jié)果2009-12-12