-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.dim-background.js
182 lines (161 loc) · 5.01 KB
/
jquery.dim-background.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
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
/**
* Usage: $("<selector>").dimBackground([options] [, callback]);
*
* @author Andy Wermke <[email protected]>
*
*/
(function ($) {
'use strict';
var dimmedNodes = []; /// [ {$curtain: ?, $nodes: jQueryArray} ]
/**
* Dim the whole page except for the selected elements.
* @param options
* Optional. See `$.fn.dimBackground.defaults`.
* @param callback
* Optional. Called when dimming animation has completed.
*/
$.fn.dimBackground = function (options, callback) {
var params = parseParams(options, callback);
options = params.options;
callback = params.callback;
options = $.extend({}, $.fn.dimBackground.defaults, options);
// Initialize curtain
var $curtain = $('<div class="dimbackground-curtain"></div>');
$curtain.css({
position: 'fixed',
left: 0,
top: 0,
width: '100%',
height: '100%',
background: 'black',
opacity: 0,
zIndex: options.curtainZIndex
});
$('body').append($curtain);
// Top elements z-indexing
this.each(function (){
var $this = $(this);
var opts = $.meta ? $.extend( {}, options, $this.data() ) : options;
this._$curtain = $curtain;
this._originalPosition = $this.css('position').toLowerCase();
if (this._originalPosition != "absolute" && this._originalPosition != "fixed") {
$this.css('position', 'relative');
}
this._originalZIndex = $this.css('z-index');
if (this._originalZIndex == "auto" || this._originalZIndex <= opts.curtainZIndex) {
$this.css('z-index', opts.curtainZIndex+1);
}
});
$curtain.stop().animate({opacity: options.darkness}, options.fadeInDuration, callback);
dimmedNodes.push( {$curtain: $curtain, $nodes: this} );
return this;
};
/**
* Undo the dimming.
* @param options
* Optional. See `$.fn.dimBackground.defaults`.
* @param callback
* Optional. Called when "undimming" animation has completed.
*/
$.fn.undim = function (options, callback) {
var params = parseParams(options, callback);
options = params.options;
callback = params.callback;
options = $.extend({}, $.fn.dimBackground.defaults, options);
var $curtain;
var nodeZIndexMap = []; /// [ [node, originalPosition, originalZIndex], ... ]
this.each(function () {
var $this = $(this);
var opts = $.meta ? $.extend( {}, options, $this.data() ) : options;
if (this._$curtain) {
if (!$curtain) {
$curtain = this._$curtain;
}
if (typeof this._originalPosition != "undefined") {
nodeZIndexMap.push( [this, this._originalPosition, this._originalZIndex] );
}
this._$curtain = undefined;
this._originalPosition = undefined;
this._originalZIndex = undefined;
}
});
if ($curtain) {
$curtain.animate({opacity: 0}, options.fadeOutDuration, function () {
for (var i=0; i<nodeZIndexMap.length; i++) {
var node = nodeZIndexMap[i][0],
position = nodeZIndexMap[i][1],
zIndex = nodeZIndexMap[i][2];
$(node).css({
position: position,
zIndex: zIndex
});
}
$curtain.remove();
callback();
});
var match;
for (var i=0; i<dimmedNodes.length; i++) {
var entry = dimmedNodes[i];
if (entry.$curtain == $curtain) {
match = i;
break;
}
}
if (match) {
dimmedNodes = dimmedNodes.slice(0, i).concat( dimmedNodes.slice(i+1) );
}
}
return this;
};
/**
* Undim all dimmed elements.
* @param options
* Optional. See `$.fn.dimBackground.defaults`.
* @param callback
* Optional. Called when all animations have completed.
*/
$.undim = function (options, callback) {
var params = parseParams(options, callback);
options = params.options;
callback = params.callback;
options = $.extend({}, $.fn.dimBackground.defaults, options);
var _dimmedNodes = dimmedNodes.slice();
var completed = 0, total = _dimmedNodes.length;
for (var i=0; i<dimmedNodes.length; i++) {
_dimmedNodes[i].$nodes.undim(options,done);
}
if (total === 0) {
callback();
}
function done () {
completed++;
if (completed == total) {
callback();
}
}
};
// Plugin default options
$.fn.dimBackground.defaults = {
darkness : 0.75, // 0 means no dimming, 1 means completely dark
fadeInDuration : 300, // in ms
fadeOutDuration : 300, // in ms
curtainZIndex : 999
};
/// @return {options:object, callback:function}
function parseParams (options, callback) {
if (typeof options == "function") {
callback = options;
options = {};
}
if (typeof options != "object") {
options = {};
}
if (typeof callback != "function") {
callback = function () {};
}
return {
options : options,
callback : callback
};
}
}( jQuery ));