-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
80 lines (78 loc) · 2.29 KB
/
modal.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
(function () {
function Modal(element, options) {
this.options = $.extend({}, Modal.DEFAULT, options);
this.$element = $(element);
this.$bg = this.$element.find('.modal-bg');
this.$content = this.$element.find('.modal-content');
this.$close = this.$element.find('.modal-close');
this.$body = $('body');
this.contentMarginTop = this.$content.css('margin-top');
this.contentStyle = this.$content.attr('style');
this.init();
}
Modal.DEFAULT = {
bgCancel: true,
bgOpacity: .6,
contentDuration: 250,
bgDuration: 150,
slideDistance: 150
};
Modal.prototype.init = function () {
if (this.$body.hasClass('modal-open')) return false;
else this.show();
this.$close.click($.proxy(function () {
if (this.$body.hasClass('modal-open')) this.hide();
else return false;
}, this));
if (this.options.bgCancel) {
this.$bg.click($.proxy(function () {
this.hide();
}, this));
}
};
Modal.prototype.show = function () {
var contentCss = {
'margin-top': '-' + this.options.slideDistance + 'px'
};
if (this.$body.hasClass('modal-open')) return false;
this.$bg.css('opacity', 0);
this.$content.css(contentCss);
this.$body.addClass('modal-open');
this.$element.removeClass('modal-hidden');
this.$bg.fadeTo(this.options.bgDuration, this.options.bgOpacity, $.proxy(function () {
var self = this;
this.$content.animate({
'margin-top': this.contentMarginTop
}, this.options.contentDuration, function () {
self.recoverStyle();
});
}, this));
};
Modal.prototype.hide = function () {
var self = this,
contentAni = {
'margin-top': '-' + this.options.slideDistance + 'px'
};
this.$content.animate(contentAni, this.options.contentDuration, function () {
self.$bg.animate({
opacity: 0
}, self.options.bgDuration, function () {
self.recoverStyle();
self.$element.addClass('modal-hidden');
self.$body.removeClass('modal-open');
});
});
};
Modal.prototype.recoverStyle = function () {
if (this.contentStyle) this.$content.attr('style', this.contentStyle);
else this.$content.removeAttr('style');
};
function Plugin(options) {
return this.each(function () {
var $this = $(this);
if ($this.data('modal')) $this.data('modal').show();
else $this.data('modal', new Modal(this, options));
});
}
$.fn.modal = Plugin;
})();