核心提示:代码如下!DOCTYPE htmlhtml lang=enheadmeta charset=UTF-8meta name=viewport content=width=device-width, in...
代码如下
<!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>Document</title> <style> .cancel, .confirm { cursor: pointer; width: 40%; outline: none; border: 0px; text-align: center; background: white; color: blue; border: 1px solid lightseagreen; } .cancel { margin-right: 10%; } .confirm { margin-left: 10%; } </style> </head> <body> <button>弹出中间弹框</button> </body> <script> var oButton = document.getElementsByTagName("button")[0]; oButton.onclick = function () { new layer( { layerCertain: true, position: 'left', top: '200px', left: '350px' }, function () { alert('点击了确认') }, function () { alert('点击了取消') }); } function layer(styleChange, funOne, funTwo) { this.funOne = funOne||null; this.funTwo = funTwo||null; this.style = { title: '系统提示', content: '这是默认的提示', position: 'center', layerCertain: false, } if (styleChange != 'undefined') { this.forChangeStyle(styleChange); } this.init(); } layer.prototype = { init() { this.createDiv(); if (this.style.layerCertain) { this.layerCertain(); } this.closeLayer(); this.layerConfirm(this.funOne); this.layerCancel(this.funTwo); }, createDiv() { var iframe = document.createElement('p'); var style = document.createElement("style"); var classStyle = ''; if (this.style.position == 'center') { classStyle = '.layerPosition{position:fixed;z-index:11;background:lightpink;display:inline-block;height:105px;width:200px;top:0;left:0;bottom:0;right:0;margin:auto'; } else if (this.style.position == 'left') { classStyle = '.layerPosition{position:fixed;z-index:11;background:lightpink;display:inline-block;height:105px;width:200px;top:' + this.style.top + ';left:' + this.style.left + ''; } iframe.className = 'layerPosition'; style.appendChild(document.createTextNode(classStyle)); var head = document.getElementsByTagName("head")[0]; head.appendChild(style); iframe.innerHTML = '<h3>' + this.style.title + '<p style="color:red;cursor:pointer;margin:0;float:right"id="closeLayer">XXX</p></h3><p>' + this.style.content + '</p><p><button class="cancel">取消</button><button class="confirm">确定</button>'; document.body.appendChild(iframe); }, layerCertain() { var oCertain = document.createElement('p'); var style = document.createElement("style"); var classStyle = ''; classStyle = '.layerCertain{position:fixed;z-index:1;top:0;left:0;bottom:0;right:0;background:rgba(0,0,0,0.4)}'; style.appendChild(document.createTextNode(classStyle)); var head = document.getElementsByTagName("head")[0]; head.appendChild(style); oCertain.className = 'layerCertain'; document.body.appendChild(oCertain); }, closeLayer() { this.addHandler(document.getElementById('closeLayer'), 'click', function () { var certain = document.getElementsByClassName('layerCertain')[0] || null, layerPanel = document.getElementsByClassName('layerPosition')[0]; if (certain) { certain.parentElement.removeChild(certain); } layerPanel.parentElement.removeChild(layerPanel); }); }, //这个是回调的重点 layerConfirm(funOne) { var _this = this; this.addHandler(document.getElementsByClassName('confirm')[0], 'click', function () { if (funOne && typeof funOne == 'function') { funOne(); } var certain = document.getElementsByClassName('layerCertain')[0] || null, layerPanel = document.getElementsByClassName('layerPosition')[0]; if (certain) { certain.parentElement.removeChild(certain); } layerPanel.parentElement.removeChild(layerPanel); }) }, //这个是回调的重点 layerCancel(funTwo){ var _this = this; this.addHandler(document.getElementsByClassName('cancel')[0],'click',function(){ if(funTwo&&typeof funTwo == 'function'){ funTwo(); } var certain = document.getElementsByClassName('layerCertain')[0] || null, layerPanel = document.getElementsByClassName('layerPosition')[0]; if (certain) { certain.parentElement.removeChild(certain); } layerPanel.parentElement.removeChild(layerPanel); }) }, addHandler(obj, type, fun) { obj.addEventListener ? obj.addEventListener(type, fun) : obj['on' + type] = fun; }, forChangeStyle(styleChange) { for (var key in styleChange) { this.style[key] = styleChange[key]; } } } </script> </html>