-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.js
87 lines (68 loc) · 2.01 KB
/
overlay.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
81
82
83
84
85
86
87
(function ($) {
var OverlayPlugin = function (element, options) {
var elem = $(element);
var obj = this;
var backdropEl = null;
var triggerEl = null;
var triggerHandler = null;
var closeEl = null;
var closeHandler = null;
var settings = $.extend({
overlayTriggerId: "#overlayTrigger",
overlayCloseId: "#overlayClose"
}, options || {});
// Public method - can be called from client code
this.destroy = function () {
hide();
if (triggerEl !== null) {
triggerEl.off('click');
}
if (closeEl !== null) {
closeEl.off('click');
}
};
// Private methods - can only be called from within this object
var hide = function () {
$('body').css('overflow', '');
//Hide backdrop
if (backdropEl !== null) {
backdropEl.remove();
backdropEl = null;
}
//Hide overlay
elem.removeClass('overlay');
};
var bindTrigger = function () {
triggerEl = $(settings.overlayTriggerId);
triggerHandler = function () {
//Show backdrop
backdropEl = $('<div/>').addClass('overlay-backdrop');
backdropEl.on('click', function () {
hide();
});
$('body').append(backdropEl);
//Show overlay
elem.addClass('overlay');
$('body').css('overflow', 'hidden');
};
triggerEl.on('click', triggerHandler);
//Add close handler
closeEl = $(settings.overlayCloseId);
closeHandler = function () {
hide();
};
closeEl.on('click', closeHandler);
};
bindTrigger();
};
$.fn.overlay = function (options) {
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('overlay')) return element.data('overlay');
// pass options to plugin constructor
var overlay = new OverlayPlugin(this, options);
// Store plugin object in this element's data
element.data('overlay', overlay);
return overlay;
};
})(jQuery);