leanModal.js
4.78 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
(function($) {
var _stack = 0,
_lastID = 0,
_generateID = function() {
_lastID++;
return 'materialize-lean-overlay-' + _lastID;
};
$.fn.extend({
openModal: function(options) {
$('body').css('overflow', 'hidden');
var defaults = {
opacity: 0.5,
in_duration: 350,
out_duration: 250,
ready: undefined,
complete: undefined,
dismissible: true,
starting_top: '4%'
},
overlayID = _generateID(),
$modal = $(this),
$overlay = $('<div class="lean-overlay"></div>'),
lStack = (++_stack);
// Store a reference of the overlay
$overlay.attr('id', overlayID).css('z-index', 1000 + lStack * 2);
$modal.data('overlay-id', overlayID).css('z-index', 1000 + lStack * 2 + 1);
$("body").append($overlay);
// Override defaults
options = $.extend(defaults, options);
if (options.dismissible) {
$overlay.click(function() {
$modal.closeModal(options);
});
// Return on ESC
$(document).on('keyup.leanModal' + overlayID, function(e) {
if (e.keyCode === 27) { // ESC key
$modal.closeModal(options);
}
});
}
$modal.find(".modal-close").on('click.close', function(e) {
$modal.closeModal(options);
});
$overlay.css({ display : "block", opacity : 0 });
$modal.css({
display : "block",
opacity: 0
});
$overlay.velocity({opacity: options.opacity}, {duration: options.in_duration, queue: false, ease: "easeOutCubic"});
$modal.data('associated-overlay', $overlay[0]);
// Define Bottom Sheet animation
if ($modal.hasClass('bottom-sheet')) {
$modal.velocity({bottom: "0", opacity: 1}, {
duration: options.in_duration,
queue: false,
ease: "easeOutCubic",
// Handle modal ready callback
complete: function() {
if (typeof(options.ready) === "function") {
options.ready();
}
}
});
}
else {
$.Velocity.hook($modal, "scaleX", 0.7);
$modal.css({ top: options.starting_top });
$modal.velocity({top: "10%", opacity: 1, scaleX: '1'}, {
duration: options.in_duration,
queue: false,
ease: "easeOutCubic",
// Handle modal ready callback
complete: function() {
if (typeof(options.ready) === "function") {
options.ready();
}
}
});
}
}
});
$.fn.extend({
closeModal: function(options) {
var defaults = {
out_duration: 250,
complete: undefined
},
$modal = $(this),
overlayID = $modal.data('overlay-id'),
$overlay = $('#' + overlayID);
options = $.extend(defaults, options);
// Disable scrolling
$('body').css('overflow', '');
$modal.find('.modal-close').off('click.close');
$(document).off('keyup.leanModal' + overlayID);
$overlay.velocity( { opacity: 0}, {duration: options.out_duration, queue: false, ease: "easeOutQuart"});
// Define Bottom Sheet animation
if ($modal.hasClass('bottom-sheet')) {
$modal.velocity({bottom: "-100%", opacity: 0}, {
duration: options.out_duration,
queue: false,
ease: "easeOutCubic",
// Handle modal ready callback
complete: function() {
$overlay.css({display:"none"});
// Call complete callback
if (typeof(options.complete) === "function") {
options.complete();
}
$overlay.remove();
_stack--;
}
});
}
else {
$modal.velocity(
{ top: options.starting_top, opacity: 0, scaleX: 0.7}, {
duration: options.out_duration,
complete:
function() {
$(this).css('display', 'none');
// Call complete callback
if (typeof(options.complete) === "function") {
options.complete();
}
$overlay.remove();
_stack--;
}
}
);
}
}
});
$.fn.extend({
leanModal: function(option) {
return this.each(function() {
var defaults = {
starting_top: '4%'
},
// Override defaults
options = $.extend(defaults, option);
// Close Handlers
$(this).click(function(e) {
options.starting_top = ($(this).offset().top - $(window).scrollTop()) /1.15;
var modal_id = $(this).attr("href") || '#' + $(this).data('target');
$(modal_id).openModal(options);
e.preventDefault();
}); // done set on click
}); // done return
}
});
})(jQuery);