!(function ($, window, document, undefined) { var plugin = function (elem, options) { this.$elem = elem; this.$btn = $('#btn1'); this.$omask = $('#mask_shadow'); this.$otitle = this.$elem.find('.title'); this.$title_text = this.$otitle.find('p'); this.$close = this.$otitle.find('span'); this.b_stop = true; // 防止重复点击 this.page_w = $(window).width(); this.page_h = $(window).height(); this.defaults = { ifdrag: false, draglimit: false }; this.opts = $.extend({}, this.defaults, options); }; plugin.prototype = { inital: function () { // 初始化 var self = this; this.$title_text.text(this.$title_text.attr('data-title')); this.$elem.css({left: (this.page_w - this.$elem.width()) / 2}); this.$elem.on('click', function () { return false; }); this.$btn.on('click', function () { self.popbox(); self.b_stop = false; return false; }); this.$close.on('click', function () { self.closepopbox(); return false; }); $(document.body).on('click', function () { self.closepopbox(); }); // 拖拽事件 this.$otitle.on('mousedown', function (ev) { if (self.opts.ifdrag) { self.drag(ev); } return false; }); }, popbox: function () { // 显示弹窗 var self = this; this.$omask.show().animate({opacity: 1});; this.$elem.show().animate({opacity: 1, top: 260}, function () { self.b_stop = true; }); }, closepopbox: function () { // 关闭弹窗 var self = this; if (this.b_stop) { this.$omask.animate({opacity: 0}, function () { $(this).hide(); });; this.$elem.animate({opacity: 0, top: 150}, function () { $(this).hide(); }); } }, drag: function (ev) { // 拖拽事件 var self = this; var oevent = ev || window.event; var disx = oevent.clientx - this.$elem.offset().left; var disy = oevent.clienty - this.$elem.offset().top; var _move = true; $(document).mousemove(function (ev) { if (_move) { var oevent = ev || window.event; var offset_l = oevent.clientx - disx; var offset_t = oevent.clienty - disy; if (self.opts.draglimit) { if (offset_l <= 0) { offset_l = 0; } else if (offset_l >= self.page_w - self.$elem.width()) { offset_l = self.page_w - self.$elem.width(); } if (offset_t <= 0) { offset_t = 0; } else if (offset_t >= self.page_h - self.$elem.height()) { offset_t = self.page_h - self.$elem.height(); } } self.$elem.css({left: offset_l, top: offset_t}); } }).mouseup(function () { _move = false; }); }, constructor: plugin }; $.fn.popup = function (options) { var plugin = new plugin(this, options); return plugin.inital(); }; })(window.jquery, window, document);