1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| function Dialog() { // this.option = option this.bindEvent() } Dialog.prototype.template = function (option) { let temp = `<div class="zc-dialog-mask"> <div class="zc-dialog-wrap"> <div class="zc-dialog-head"> ${option.title || '提示'} </div> <div class="zc-dialog-body"> ${option.content || option || '内容'} </div> <div class="zc-dialog-button"></div> <span>X</span> </div> </div>` this.$temp = $(temp) } Dialog.prototype.hide = function () { this.$temp.detach() } Dialog.prototype.bindEvent = function () { $('body').on('click', '.zc-dialog-wrap>span, .zc-dialog-button>.zc-dialog-confirm, .zc-dialog-button>.zc-dialog-cancel', function(){ dialog.hide() if (this.classList[0] === 'zc-dialog-confirm') return true else if (this.classList[0] === 'zc-dialog-cancel') return false }) } Dialog.prototype.setSize = function (option) { if(option.width && option.width > 330){ this.$temp.find('.zc-dialog-wrap').css({width: `${option.width}px`}) } if(option.height && option.height > 150){ this.$temp.find('.zc-dialog-body').css({height: `${option.height}px`}) } } Dialog.prototype.alert = function (option) { this.template(option) this.$temp.find('.zc-dialog-button').addClass('zc-dialog-button-alert').removeClass('zc-dialog-button-confirm').html(`<button class="zc-dialog-confirm" type="button">确认</button>`) this.setSize(option) $('body').append(this.$temp) } Dialog.prototype.confirm = function (option) { this.template(option) this.$temp.find('.zc-dialog-button').addClass('zc-dialog-button-confirm').removeClass('zc-dialog-button-alert').html(`<button class="zc-dialog-confirm" type="button">确认</button> <button class="zc-dialog-cancel" type="button">取消</button>`) this.setSize(option) $('body').append(this.$temp) }
var dialog = new Dialog();
$('.a1').click(function(){ dialog.alert({ title: '非模态窗口', content: '<p>这是一个非模态窗口,并且不带按钮</p>' }) }) $('.a2').click(function(){ dialog.confirm({ title: '非模态窗口', content: '<p>确定要删除吗?</p>' }) })
|