window.popBox = window.popBox || {}; window.popBox.showMsg = (function () { var mongolia, //蒙层 promptBox, //提示框 closeSpan, //关闭按钮 titleSpan, //提示标题 contextSpan, //提示信息 okBtn, //确定按钮 cancelBtn, //取消按钮 isRegEvent, //是否注册事件 option; //传入的参数 /** * 初始化蒙层 */ function initMongolia() { if (!mongolia) { //没有蒙层则初始化 //蒙层:覆盖整个窗口,半透明的黑色 mongolia = document.createElement("div"); mongolia.style.position = "fixed"; mongolia.style.width = mongolia.style.height = "100%"; mongolia.style.left = mongolia.style.top = 0; mongolia.style.background = "rgba(0,0,0,.5)"; document.body.appendChild(mongolia); } mongolia.style.display = "block"; //展示蒙层 } /** * 初始化提示框 */ function initPromptBox() { //提示框:宽高300,位置居中 if (!promptBox) { promptBox = document.createElement("div"); //promptBox.style.width = promptBox.style.height = "300px"; promptBox.style.width = option.width || "600px"; promptBox.style.height = option.height || "400px"; promptBox.style.background = "#fff"; promptBox.style.fontSize = "16px"; promptBox.style.position = "absolute"; //promptBox.style.top = promptBox.style.left = "40%"; promptBox.style.top = "100px"; //promptBox.style.marginLeft = promptBox.style.marginTop = "-150px"; promptBox.style["data-popBox-id"] = "promptBox"; promptBox.style.borderRadius = "8px 8px"; initPromptContext(); mongolia.appendChild(promptBox); titleSpan = document.querySelector("[data-popBox-id='title']"); //提示标题 contextSpan = document.querySelector("[data-popBox-id='message']"); //提示信息 closeSpan = document.querySelector("[data-popBox-id='close']"); //关闭按钮 okBtn = document.querySelector("[data-popBox-id='ok']"); //确定按钮 cancelBtn = document.querySelector("[data-popBox-id='cancel']"); //取消按钮 titleSpan.style.fontSize = "20px"; titleSpan.style.color = "#ffffff"; var clientHeight = document.documentElement.clientHeight; //屏幕的可见高度 var clientWidth = document.documentElement.clientWidth; //屏幕的可见宽度 var top = (clientHeight - promptBox.getBoundingClientRect().height) / 2 + "px"; var left = (clientWidth - promptBox.getBoundingClientRect().width) / 2 + "px"; tjdLog("top = " + top + ", left = " + left); promptBox.style.top = top; promptBox.style.left = left; tjdLog("width = " + promptBox.style.width); tjdLog("height = " + promptBox.style.height); tjdLog("top = " + promptBox.style.top); tjdLog("left = " + promptBox.style.left); } okBtn.innerText = option.okText || "确定"; cancelBtn.innerText = option.cancelText || "取消"; titleSpan.innerText = option.title || "提示"; contextSpan.innerText = option.context || ""; } /** * 初始化提示框中的内容 */ function initPromptContext() { //内容包含:标题,关闭按钮,提示信息,确定按钮,取消按钮 //创建标题,关闭按钮 var div = document.createElement("div"); div.innerHTML = ` X`; div.style.height = "50px"; div.style.padding = "10px 10px"; div.style.background = "#193c7b"; div.style.boxSizing = "border-box"; div.style.borderRadius = "8px 8px 0 0"; promptBox.appendChild(div); //创建提示信息 div = document.createElement("div"); e = ''; inputlist = eval(option.input); height = 0; msgVal = eval(option.msgVal); for(i=0;i

'; }else e += '

'; height += 56; } div.innerHTML = e; div.style.height = height + "px"; //"240px"; div.style.padding = "10px 10px"; div.style.boxSizing = "border-box"; div.id = "popBoxContent"; selects = eval(option.selects); if (typeof selects !== "undefined" && selects !== null){ k=i+1; for(i=0;i'; div.style.height = "50px"; div.style.padding = "10px 20px"; div.style.boxSizing = "border-box"; promptBox.appendChild(div); } //注册事件 function regEvent() { if (!isRegEvent) { //未注册事件 //1.点击关闭,点击蒙层,点击取消按钮 closeSpan.onclick = function () { mongolia.style.display = "none"; //隐藏蒙层 }; okBtn.onclick = function () { option && option.okFunction && (ret = option.okFunction()); if(ret == 1) mongolia.style.display = "none"; //隐藏蒙层 } cancelBtn.onclick = function () { option && option.cancelFunction && option.cancelFunction(); mongolia.style.display = "none"; //隐藏蒙层 } //2.拖动提示框事件 window.onmousedown = function (e) { var target = getTarget(e.target); //是否包含目标元素 if (target) { var style = window.getComputedStyle(target); var left = parseInt(style.left); var top = parseInt(style.top); var disX = parseInt(e.pageX) - left; var disY = parseInt(e.pageY) - top; window.onmousemove = function (e) { var newLeft = parseInt(e.pageX) - disX; var newTop = parseInt(e.pageY) - disY; promptBox.style.left = newLeft + "px"; promptBox.style.top = newTop + "px"; }; window.onmouseup = window.onmouseleave = function () { window.onmousemove = null; } } }; function getTarget(target) { while (target) { //if (target.tagName === "DIV" && target.style["data-popBox-id"] === "promptBox") { if (target.tagName === "DIV" && target.style["data-popBox-id"] === "title") { return target; } else { target = target.parentElement; } } return false; } } } /** * @param {object} opts * opts.title : 提示标题 * opts.context : 提示信息 * opts.cancelText:取消按钮内容 * opts.okText:确定按钮内容 * opts.cancelText:取消按钮内容 * opts.okFunction:确定按钮的回调函数 * opts.cancelFunction:取消按钮的回调函数 */ function showMsg(opts) { if (typeof opts === "string") { option = { context: opts } } else { option = opts || {}; } if (promptBox){ mongolia.removeChild(promptBox); promptBox = null; } tjdLog("input = " + option.input + "length = " + eval(option.input).length); if (typeof option.selects !== "undefined" && option.selects !== null){ tjdLog("selects = " + option.selects + "length = " + eval(option.selects).length); for(i=0;i