-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.overlay.js
More file actions
192 lines (160 loc) · 4.03 KB
/
jquery.overlay.js
File metadata and controls
192 lines (160 loc) · 4.03 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* jquery.overlay.js, jQuery plugin to create overlay over any element
*
* Copyright 2011, Egil Hanger (egilkh@gmail.com)
*
* Licensed under the MIT license.
*
*/
(function($){
var methods = {
init : function (options, c) {
var settings = $.extend({ },
{
'backgroundColor' : '#000',
'opacity' : 0.2,
'zIndex' : 100,
'autoClick' : true,
'fadeSpeed' : 400,
'click' : function (e) {},
'onShow' : function (e) {}, // Event at the same time as it will show
'onHide' : function (e) {}, // Event at the same time as it will hide
'onShown' : function (e) {}, // Event (chained) after fadeIn
'onHidden' : function (e) {} // Event (chained) after fadeOut
}, options);
return this.each(function () {
var $this = $(this);
var data = $this.data('overlay');
var ol = data ? data.ol : null;
if (!data) {
ol = $('<div />')
.hide()
.css({
backgroundColor : settings['backgroundColor'],
opacity : settings['opacity'],
zIndex : settings['zIndex']
})
.bind('click.overlay', {o : $this}, methods.click);
$('body').append(ol);
}
$this.data('overlay', {
'ol' : ol,
'settings' : settings
});
$this.overlay('show', c);
});
},
show : function (c) {
return this.each(function (e) {
var $this = $(this);
var data = $this.data('overlay');
if (!data) {
// do init
$this.overlay('init');
return;
}
var ol = data.ol;
var w = 0;
var h = 0;
if ($this[0].nodeName && $this[0].nodeName === "#document") {
var w = '100%';
var h = '100%';
var p = 'fixed';
if (!self.innerWidth) { // Special case for IE8
w = document.body.clientWidth;
h = document.body.clientHeight;
p = 'absolute';
}
ol.css({
left : 0,
top : 0,
width : w,
height : h,
position : p
});
} else {
// this might be wrong for some browsers?
ol.css( {
left: $this.offset().left,
top : $this.offset().top,
width : $this.outerWidth(),
height : $this.outerHeight(),
position : 'absolute'
});
}
setTimeout(function() {
ol.fadeIn(data.settings['fadeSpeed'], function (e) {
setTimeout(function() {
data.settings['onShown'].call($this);
}, 25);
if (c) {
setTimeout(function() {
c.call($this);
}, 25);
}
});
}, 25);
setTimeout(function() {
data.settings.onShow.call($this);
}, 25);
});
},
hide : function (c) {
return this.each(function (e) {
var $this = $(this);
var data = $this.data('overlay');
if (data && data.ol) {
setTimeout(function() {
data.ol.fadeOut(data.settings['fadeSpeed'], function (e) {
setTimeout(function() {
data.settings['onHidden'].call($this);
}, 25);
if (c) {
setTimeout(function() {
c.call($this);
}, 25);
}
});
}, 25);
setTimeout(function() {
data.settings.onHide.call($this);
}, 25);
}
});
},
destroy : function ( ) {
return this.each(function (e) {
var $this = $(this);
var data = $this.data('overlay');
if (data) {
data.unbind('.overlay');
data.ol.remove();
$this.removeData('data');
}
});
},
click : function (e) {
var $this = e.data.o; // jQuery object we called .overlay() on.
var data = $this.data('overlay');
if (data) {
if (data.settings['autoClick']) {
$this.overlay('hide');
}
data.settings['click'].call($this, e);
}
}
};
$.fn.overlay = function(method) { // method wrapper
if ( methods[method] ) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof(method) === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.Overlay');
}
};
// shorthand for $(document) overlay
$.overlay = function (options) {
$(document).overlay(options);
};
})(jQuery);