-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jQuery.leanModal2.js
167 lines (150 loc) · 4.86 KB
/
jQuery.leanModal2.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
//// jQuery.leanModal2.js v2.6.3
// MIT Licensed by eustasy https://eustasy.org
// Based on leanModal v1.1 by Ray Stone - http://finelysliced.com.au
/*global jQuery*/
// ANONFUNC Wrap in an anonymous function.
(function($){
//// Extend jQuery
// EXTENDFUNC
$.fn.extend({
// EXTENDFUNC_LEANMODAL
leanModal: function(options) {
//// Default Options
// Set some Defaults.
var defaults = {
defaultStyles: true, // GLOBAL
fadeTime: 200, // GLOBAL
overlayOpacity: 0.7, // GLOBAL
closeButton: '.js-leanmodal-close',
disableCloseOnOverlayClick: false,
disableCloseOnEscape: false,
modalCenter: true,
}
// Merge in any passed options.
options = $.extend(defaults, options)
//// Default styles
if ( options.defaultStyles ) {
if ( $('#js-leanmodal-styles').length == 0 ) {
$('<style>')
.prop('type', 'text/css')
.prop('id', 'js-leanmodal-styles')
.html('\
@keyframes smoothFadeOut {\
from { opacity: 1; }\
to { opacity: 0; }\
}\
@keyframes smoothFadeIn {\
from { opacity: 0; }\
to { opacity: 1; }\
}\
#js-leanmodal-overlay {\
align-items: center;\
background: rgba(0, 0, 0, ' + options.overlayOpacity + ');\
display: none;\
height: 100%;\
justify-content: center;\
left: 0;\
position: fixed;\
top: 0;\
width: 100%;\
}\
.js-leanmodal-link {\
cursor: pointer;\
}\
.js-leanmodal-inactive {\
animation: smoothFadeOut ' + options.fadeTime + 'ms ease-in-out both;\
}\
.js-leanmodal-active {\
animation: smoothFadeIn ' + options.fadeTime + 'ms ease-in-out both;\
display: block;\
z-index: 1000\
}\
#js-leanmodal-overlay.js-leanmodal-active {\
display: flex;\
z-index: 100;\
}'
).appendTo('head')
}
}
//// Close the Modal
// FUNCTION: Fade out the overlay and a passed identifier.
function leanModal_Close(modal_id) {
$('#js-leanmodal-overlay').removeClass('js-leanmodal-active').addClass('js-leanmodal-inactive')
$(modal_id).removeClass('js-leanmodal-active').addClass('js-leanmodal-inactive')
$('#js-leanmodal-overlay').unbind('click')
$(document).off('keyup')
$(modal_id).appendTo('body')
}
//// Everything is Linked
// FOREACHLINK For each targeted link.
return this.each(function() {
// Force this to look like a link.
$(this).addClass('js-leanmodal-link')
//// Command Override
// Override the existing click command,
// and insert this new one.
// CLICKOVERRIDE
$(this).unbind('click').click(function(e) {
//// Select the Modal Identifier
// IFHREF Use data-open-modal if available
var modal_id
if ( $(this).attr('data-modal-id') ) {
modal_id = $(this).attr('data-modal-id')
// IFHREF Fall back to href
} else if ( $(this).attr('href') ) {
modal_id = $(this).attr('href')
// IFHREF Fail entirely.
} else {
return false
} // IFHREF
//// Close with closeButton
// If `closeButton` is set,
// use it to call the close command.
if ( options.closeButton ) {
$(options.closeButton).click(function() {
leanModal_Close(modal_id)
})
}
//// Escape with `Esc`
// Close the modal when someone presses `Esc`,
// except when `disableCloseOnEscape` is set to `true`
if ( !options.disableCloseOnEscape ) {
$(document).on('keyup', function(evt) {
if ( evt.keyCode == 27 ) {
leanModal_Close(modal_id)
}
})
}
//// There can be only one.
// Overlay. If there isn't an overlay, add one.
if ( $('#js-leanmodal-overlay').length == 0 ) {
var overlay = $('<div id="js-leanmodal-overlay"></div>')
$('body').append(overlay)
}
//// Envelope in Darkness
// Close the modal when someone clicks on the overlay,
// except when `disableCloseOnOverlayClick` is set to `true`
if ( !options.disableCloseOnOverlayClick ) {
$('#js-leanmodal-overlay').click(function(e) {
if ( e.target == this ) {
leanModal_Close(modal_id)
}
})
}
//// Modal Positioning
// Position the modal centrally inside the overlay using flexbox.
if ( options.modalCenter ) {
$(modal_id).appendTo('#js-leanmodal-overlay')
}
//// Curtain Up
// Fade in the modal and overlay.
$('#js-leanmodal-overlay').removeClass('js-leanmodal-inactive').addClass('js-leanmodal-active')
$(modal_id).removeClass('js-leanmodal-inactive').addClass('js-leanmodal-active')
//// Default Prevention
// Prevent whatever the default was (probably scrolling).
e.preventDefault()
}) // CLICKOVERRIDE
}) // FOREACHLINK
}, // EXTENDFUNC_LEANMODAL
}) // EXTENDFUNC
})(jQuery) // ANONFUNC This passes in `jQuery` as `$`