/** * @file 基于jquery的弹窗组件 * @author 龙泉 * @version 1.0.0 */ (function(factory) { if (typeof define === 'function' && define.amd) { // amd module define(['jquery'], factory); } else if (typeof module !== "undefined" && module.exports) { // node/commonjs // seajs build factory(require('jquery')); } else { // 浏览器全局模式 factory(jquery); } })(function($) { var closeclass = '.j_dialogclose'; // 默认参数配置 var dialogdef = { id: '', // 如果页面中存在多个样式的弹窗,可用id区别样式 title: 'title', isfixed: true, hideheader: false, hideclose: false, content: null, callback: null, withnopadding: false, // 是否不设置padding withnominwidth: false, // 是否不设置最小宽度 bghide: true, // 点击背景是否隐藏 eschide: true // 按esc按键是否隐藏 }; // 全局变量 var dialogconfig = { windows: $(window), lightbox: '.dialog-background', section: '.dialog-section', imagetag: '.dialog-imageitem', imagedata: {}, width: 0, height: 0, left: 0, top: 0, paddingwidth: 0, paddingheight: 0, resizeparams: ['.dialog-section'] }; // 用户传递过来的参数集 var dialogopts = {}; // 基础实现 var dialog = { /** * 显示dialog弹窗 * @param {object} opts 配置选项 * @return {undefined} */ show: function(opts) { dialogopts = $.extend({}, dialogdef, opts || {}); if (opts.content) { dialog.dialog(opts); } else { dialog.lightbox(opts); } // 初始化时执行的回调函数 typeof dialogopts.oninit === 'function' && dialogopts.oninit(); // 改变浏览器大小时,动态改变内容显示盒子的位置 dialogconfig.windows.on('resize', function() { dialog.settings.apply(window, dialogconfig.resizeparams); }); }, /** * 关闭dialog弹窗 * @param {function} callback 回调函数 * @return {undefined} */ hide: function(callback) { var olightbox = $(dialogconfig.lightbox); var osection = $(dialogconfig.section); if (dialogopts.isfixed) { osection.animate({ margintop: -(dialogconfig.top - 150), opacity: 0 }); } else { osection.animate({ top: (dialogconfig.top + 150), opacity: 0 }); } olightbox.fadeout(function() { olightbox.remove(); osection.remove(); callback && callback(); }); }, dialog: function(opts) { dialog.install(opts); dialogconfig.resizeparams = [dialogconfig.section, true, true]; dialog.settings.apply(window, dialogconfig.resizeparams); }, lightbox: function(opts) { var clickobj_src = opts.clickobj.attr('data-src') || opts.clickobj.attr('data-image'); dialogconfig.now = 0; dialog.getimages_src(dialogopts.imagelist); dialog.loadimage(clickobj_src, true, dialog.settings); dialog.getnow(clickobj_src); }, install: function(opts) { var obody = $('body'); var headerhtml = '
' + dialogopts.title + '
'; var closehtml = '
'; var markid = ''; var addclass = ''; var $background = obody.find(dialogconfig.lightbox); if (!dialogopts.content) { var content = '
' + ' ' + '
' + ' ' + '<' + '>'; } else { var content = dialogopts.content; } if (dialogopts.hideheader) { headerhtml = ''; } if (dialogopts.hideclose) { closehtml = ''; } if (dialogopts.id) { markid = ' id="' + dialogopts.id + '"'; } var plugs_lightbox = '
'; var plugs_lightbox_section = '
' + headerhtml + '
' + content + '
' + closehtml + '
'; // 如果之前有打开弹窗,先将其关闭 if ($background.length) { $background.stop().fadein(); obody.find(dialogconfig.section).remove(); } else { obody.append(plugs_lightbox) } obody.append(plugs_lightbox_section); $(dialogconfig.lightbox).fadein(); $(dialogconfig.section).show(); var ipaddingwidth = $(dialogconfig.section).outerwidth() - $(dialogconfig.section).width(); var ipaddingheight = $(dialogconfig.section).outerheight() - $(dialogconfig.section).height(); dialogconfig.paddingwidth = ipaddingwidth; dialogconfig.paddingheight = ipaddingheight; dialogopts.callback && dialogopts.callback(); }, getnow: function(loadimage_src) { for (var i = 0, len = dialogconfig.images.length; i < len; i++) { if (loadimage_src === dialogconfig.images[i]) { dialogconfig.now = i; } } }, getimages_src: function(images) { var images = (typeof images == 'string') ? $(images) : images; dialogconfig.images = []; for (var i = 0, len = images.length; i < len; i++) { var currentimage = images.eq(i); var currentimage_src = currentimage.attr('data-src') || currentimage.attr('data-image'); var currentimage_src = $.trim(currentimage_src); if (currentimage_src !== '') { dialogconfig.images.push(currentimage_src); } } }, loadimage: function(loadimage_src, ismove, callback) { var image = new image(); image.onload = function() { if ($('.dialog-section').length === 0) { dialog.install(dialogopts); $('.dialog-btnprev').on('click', function() { dialog.switchimage(false, false); }); $('.dialog-btnnext').on('click', function() { dialog.switchimage(true, false); }); } dialog.setbtnsate(); var section = $(dialogconfig.section); var imagetag = $(dialogconfig.imagetag); dialogconfig.imagedata = { width: this.width, height: this.height, src: loadimage_src }; dialogconfig.resizeparams = [section, imagetag, ismove]; callback && callback.apply(window, dialogconfig.resizeparams); } image.src = loadimage_src; }, switchimage: function(d, ismove) { if (d) { dialogconfig.now++; } else { dialogconfig.now--; } if (dialogconfig.now < 0) { dialogconfig.now = dialogconfig.images.length - 1; } if (dialogconfig.now > dialogconfig.images.length - 1) { dialogconfig.now = 0; } var loadimage_src = dialogconfig.images[dialogconfig.now]; dialog.loadimage(loadimage_src, ismove, dialog.settings); }, setbtnsate: function() { if (dialogconfig.images.length < 2) { $('.dialog-btnprev, .dialog-btnnext').hide(); } }, // 设置内容显示盒子的大小,位置 settings: function(section, imagetag, ismove) { var section = (typeof section == 'string') ? $(section) : section; var winheight = $(window).height(); if (!dialogopts.content) { var sectionheight = 116, // 外围容器默认的高度,为了方便,这里暂时使用固定值,后期改版再做调整 configwidth = dialogconfig.imagedata.width, configheight = dialogconfig.imagedata.height; dialogconfig.width = configwidth; dialogconfig.height = configheight; if (sectionheight + dialogconfig.height > winheight) { dialogconfig.height = winheight - sectionheight - 50; dialogconfig.height = dialogconfig.height < 500 ? 500 : dialogconfig.height; dialogconfig.width = math.round(dialogconfig.width * (dialogconfig.height / configheight)); } } else { section.css({ left: '0px' }); // 固定布局时,容器的left应该为0,样式文件不好修改,暂时在这里调整 dialogconfig.width = section.width(); dialogconfig.height = section.height(); } var outerwidth = dialogconfig.width + dialogconfig.paddingwidth; var outerheight = dialogconfig.height + dialogconfig.paddingheight + $('.dialog-header').outerheight(); if (typeof imagetag === 'object') { imagetag.hide().attr('src', dialogconfig.imagedata.src).css({ width: dialogconfig.width, height: dialogconfig.height }).fadein(); } if (dialogopts.isfixed) { dialogconfig.left = math.floor(outerwidth / 2); dialogconfig.top = math.floor(outerheight / 2); section.css({ position: 'fixed', left: '50%' }); if (ismove) { section.css({ marginleft: -dialogconfig.left, margintop: -dialogconfig.top }); } else { section.animate({ marginleft: -dialogconfig.left, margintop: -dialogconfig.top }, { queue: false }); } } else { var scrollleft = dialogconfig.windows.scrollleft(); var scrolltop = dialogconfig.windows.scrolltop(); var windowwidth = $(dialogconfig.lightbox).width(); dialogconfig.left = math.floor((windowwidth - outerwidth) / 2) + scrollleft; dialogconfig.top = math.floor((winheight - outerheight) / 2) + scrolltop; section.css({ position: 'absolute', marginleft: 0, margintop: 0 }); if (ismove) { section.css({ left: dialogconfig.left, top: dialogconfig.top }); } else { section.animate({ left: dialogconfig.left, top: dialogconfig.top }, { queue: false }); } } if (imagetag) { dialog.move(section, ismove); } }, // 显示时的动画效果 move: function(section, ismove) { if (dialogopts.isfixed && ismove) { section.css({ margintop: -(dialogconfig.top - 150) }).animate({ margintop: -dialogconfig.top, opacity: 1 }, function() { section.css('overflow', 'visible'); }); } else if (ismove) { section.css({ top: (dialogconfig.top + 150) }).animate({ top: dialogconfig.top, opacity: 1 }, function() { section.css('overflow', 'visible'); }); } section.animate({ width: dialogconfig.width }, { queue: false }); }, // 取消默认事件 canceldefault: function(e) { e.preventdefault(); e.stoppropagation(); } }; // 弹窗类公共处理函数封装 $.extend({ /** * 发送成功的提示框(置顶) * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @return {undefined} */ sendsuccesstotop: function(msg, duration, callback) { var content = '
' + ' ' + msg + '
'; $('body').append(content); var $tipbox = $('.dialog-success-top'), width = $tipbox.width(); $tipbox.css({ 'margin-left': -(width / 2), 'margin-top': 20, 'opacity': 0 }); $tipbox.animate({ 'opacity': 1, 'margin-top': 0 }, 400, function() { // 自动隐藏 cleartimeout(window.cc_timersendsuccesstotop); window.cc_timersendsuccesstotop = settimeout(function() { $tipbox.fadeout(function() { $tipbox.remove(); typeof callback === 'function' && callback(); }) }, duration || 3000); }); }, /** * 发送警告的提示框(置顶) * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @return {undefined} */ sendwarningtotop: function(msg, duration, callback) { var content = '
' + ' ' + msg + '
'; $('body').append(content); var $tipbox = $('.dialog-warning-top'), width = $tipbox.width(); $tipbox.css({ 'margin-left': -(width / 2), 'margin-top': 20, 'opacity': 0 }); $tipbox.animate({ 'opacity': 1, 'margin-top': 0 }, 400, function() { // 自动隐藏 cleartimeout(window.cc_timersendwarningtotop); window.cc_timersendwarningtotop = settimeout(function() { $tipbox.fadeout(function() { $tipbox.remove(); typeof callback === 'function' && callback(); }); }, duration || 3000); }); }, /** * 发送提示信息 * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @param {string} iconstr icon内容 * @return {undefined} */ sendmsg: function(msg, duration, callback, iconstr) { // 缺省duration参数 if ($.isfunction(duration)) { callback = duration; duration = undefined; } var content = '
' + '
' + (iconstr || '') + msg + '
' + '
'; var _options = { id: 'dialogtipbox', title: ' ', hideheader: true, hideclose: false, content: content, callback: duration === false ? null : function() { // 自动隐藏 cleartimeout(window.timerdialoghide); window.timerdialoghide = settimeout(function() { $(closeclass).trigger('click'); }, duration || 3000); }, onclose: function() { typeof callback === 'function' && callback(); } }; dialog.show(_options); }, /** * 发送成功(弹窗) * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @return {undefined} */ sendsuccess: function(msg, duration, callback) { $.sendmsg(msg, duration, callback, ''); }, /** * 发送警告(弹窗) * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @return {undefined} */ sendwarning: function(msg, duration, callback) { $.sendmsg(msg, duration, callback, ''); }, /** * 发送错误(弹窗) * @param {string} msg 提示语 * @param {number} duration 持续多长时间后关闭 * @param {function} callback 回调函数 * @return {undefined} */ senderror: function(msg, duration, callback) { $.sendmsg(msg, duration, callback, ''); }, /** * 发送确认提示框 * @param {object} options 配置选项 * @return {undefined} */ sendconfirm: function(options) { // 配置选项合并 options = $.extend(true, { id: 'dialogconfirmbox', title: '提示框', hideheader: false, hideclose: false, withcenter: false, // 是否水平居中 withicon: false, // 是否显示icon,可传递withicon的替代类名 autoclose: false, // 是否自动关闭 timeout: 3000, // 多少毫秒之后自动关闭 width: null, // 自定义宽度 noconfirm: false, // 提交按钮是否添加noconfirm属性 msg: '', // 提示语 desc: '', // 描述文本 content: '', // 自定义内容 button: { confirm: '确认', // 确认按钮-标题,null表示不显示,可以通过{text:'按钮文本', href:'按钮链接', target:'链接打开方式',behavior:'是否执行行为'}进行自定义设置 cancel: '取消', // 取消按钮-标题,null表示不显示,可以通过{text:'按钮文本', href:'按钮链接', target:'链接打开方式',behavior:'是否执行行为'}进行自定义设置 cancelfirst: false // 取消狂是否在前面 } }, options); // 是否显示按钮 var confirmvalue = options.button.confirm, cancelvalue = options.button.cancel, isshowbutton = options.button && (confirmvalue || cancelvalue), buttonconfirm = '', buttoncancel = '', buttoncontent = '', appendclass = '', appendstyle = ''; if (isshowbutton) { buttonconfirm = (confirmvalue ? '' + (confirmvalue.text || confirmvalue) + '' : ''); buttoncancel = (cancelvalue ? '' + (cancelvalue.text || cancelvalue) + '' : ''); buttoncontent = '
' + (options.button.cancelfirst ? buttoncancel + buttonconfirm : buttonconfirm + buttoncancel) + '
' } if (options.withcenter) { appendclass += ' withcenter'; } if (options.withicon) { appendclass += ' ' + (typeof options.withicon === 'string' ? options.withicon : 'withicon'); } if (options.width !== null) { appendstyle = ' style="width:' + options.width + (typeof options.width === 'string' ? '' : 'px') + ';"'; } // 弹窗内容 var content = '
' + (options.msg === '' ? '' : '
' + options.msg + '
') + (options.desc === '' ? '' : '
' + options.desc + '
') + (options.content === '' ? '' : '
' + options.content + '
') + (buttoncontent) + '
'; options.content = content; // 自动隐藏选项 if (options.autoclose) { var _callbackcopy = options.callback || $.noop; options.callback = function() { _callbackcopy(); // 自动隐藏 cleartimeout(window.timerdialoghide); window.timerdialoghide = settimeout(function() { $(closeclass).trigger('click'); }, options.timeout); }; } dialog.show(options); } }); // 相关事件绑定 (function() { var $doc = $(document); // 绑定:用于关闭对话弹窗 $doc.on('click', closeclass, function() { var $that = $(this), beforereturn; // 如果返回false,则表示中断关闭弹窗 typeof dialogopts.onbeforeclose === 'function' && (beforereturn = dialogopts.onbeforeclose($that)); if (beforereturn === false) return; cleartimeout(window.timerdialoghide); dialog.hide(function() { if (typeof dialogopts.onclose === 'function') { dialogopts.onclose($that, beforereturn); } }); }); // 绑定:用于执行弹窗的确认操作 $doc.on('click', '.j_dialogconfirm', function() { var $that = $(this), beforereturn; // 如果提交按钮存在noconfirm属性,将不执行响应 if ($that.attr('noconfirm') !== undefined) return; // 如果返回false,则表示中断关闭弹窗 typeof dialogopts.onbeforeconfirm === 'function' && (beforereturn = dialogopts.onbeforeconfirm($that)); if (beforereturn === false) return; cleartimeout(window.timerdialoghide); dialog.hide(function() { if (typeof dialogopts.onconfirm === 'function') { dialogopts.onconfirm($that, beforereturn); } }); }); // 绑定:用于执行弹窗的取消操作 $doc.on('click', '.j_dialogcancel', function() { var $that = $(this), beforereturn; // 如果返回false,则表示中断关闭弹窗 typeof dialogopts.onbeforecancel === 'function' && (beforereturn = dialogopts.onbeforecancel($that)); if (beforereturn === false) return; cleartimeout(window.timerdialoghide); dialog.hide(function() { if (typeof dialogopts.oncancel === 'function') { dialogopts.oncancel($that, beforereturn); } }); }); // 绑定:点击弹窗遮罩层关闭弹窗 $doc.on('click', '.j_bghide', function() { $(closeclass).trigger('click'); }); // 绑定:按esc按键关闭弹窗 $doc.on('keyup', function(ev) { if (ev.keycode == 27 && $('.j_eschide').length) { $(closeclass).trigger('click').removeclass('j_dialogclose'); } }); })(); // 使用$.dialog()进行访问 $.dialog = dialog.show; $.dialogclose = dialog.hide; });