当前位置 : 主页 > 网络编程 > JavaScript >

alert弹框插件

来源:互联网 收集:自由互联 发布时间:2021-06-28
原生js实现简单的弹框插件 (function (window, document) { 'use strict'; var win = window; var doc = document; var utils; function Utils() { this.overlayClass = 'bean-panel'; this.box = null; this.textTemplate = { text: 'It is OK!',
原生js实现简单的弹框插件
(function (window, document) {
  'use strict';

  var win = window;
  var doc = document;
  var utils;
  function Utils() {
    this.overlayClass = 'bean-panel';
    this.box = null;
    this.textTemplate = {
      text: 'It is OK!',
      confirmButtonText: '确定',
      confirmButtonColor: '#FF5D3D',
      cancelButtonText: '取消',
      cancelButtonColor: '#CECECE',
      showCancelButton: false
    };
  }
  Utils.prototype = {
    // 初始化DOM节点
    init: function () {
      var temp = '' +
     '
 ' +
     '
 
  ' +
     '
  

' + this.textTemplate.text + '

' + ' ' + ' ' + ' ' + ' ' + ' '; return temp; }, getChild: function (opt) { var box = document.createElement('div'); var _this = this; box.className = this.overlayClass; this.textTemplate.text = opt.text || this.textTemplate.text; this.textTemplate.confirmButtonText = opt.confirmButtonText || this.textTemplate.confirmButtonText; this.textTemplate.cancelButtonText = opt.cancelButtonText || this.textTemplate.cancelButtonText; this.textTemplate.cancelButtonColor = opt.cancelButtonColor || this.textTemplate.cancelButtonColor; box.innerHTML = this.init(); this.box = box; document.body.appendChild(this.box); }, alert: function (opt) { var btns = null; var _this = this; this.getChild(opt); btns = doc.getElementsByClassName('btn-footer-ok'); if (opt.showCancelButton) { btns[0].style.display = 'inline-block'; btns[0].style.marginRight = '.2rem'; btns[0].onclick = function () { if (opt.onCancel) { opt.onCancel(); } _this.removeBox(); }; } else { btns[0].style.display = 'none'; btns[1].style.width = '4.8rem'; } btns[1].onclick = function () { if (opt.onConfirm) { opt.onConfirm(); } _this.removeBox(); }; }, extend: function (a, b) { var az = a; Object.keys(b).forEach(function (key) { az[key] = b[key]; }); return az; }, removeBox: function () { var box = document.getElementsByClassName(this.overlayClass); document.body.removeChild(box[box.length - 1]); } }; utils = new Utils(); function BeanAlert(obj, callback) { var pars = obj || utils.textTemplate; if (callback) { pars.onConfirm = callback; } if (this instanceof BeanAlert) { this.params = pars; } else { utils.alert(pars); } } BeanAlert.prototype.show = function () { utils.alert(this.params); }; if (typeof exports === 'object') { module.exports = BeanAlert; } else { win.beanAlert = BeanAlert; } }(window, document));
网友评论