Skip to content

Commit 3fbe149

Browse files
authored
CSS Animations (#15)
* Fix #14 : Move styles to block * Move last CSS bit
1 parent 7274d73 commit 3fbe149

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

index.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,27 @@ <h1>jQuery.leanModal2</h1>
4545
<section class="whole smablet-half equalize">
4646
<h2>Defaults</h2>
4747
<textarea class="code rounded" readonly>$('.leanmodal').leanModal({
48-
overlayOpacity: 0.7,
48+
defaultStyles: true, // GLOBAL
49+
fadeTime: 200, // GLOBAL
50+
overlayOpacity: 0.7, // GLOBAL
4951
closeButton: '.js-leanmodal-close',
5052
disableCloseOnOverlayClick: false,
5153
disableCloseOnEscape: false,
52-
fadeTime: 200,
54+
modalCenter: true,
5355
});</textarea>
5456
<h3 class="button clickable rounded leanmodal-defaults" data-modal-id="#defaults-example">Default Example</h3>
5557
</section>
5658

5759
<section class="whole smablet-half equalize">
5860
<h2>Modified</h2>
5961
<textarea class="code rounded" readonly>$('.leanmodal').leanModal({
60-
overlayOpacity: 0.3,
62+
defaultStyles: true, // GLOBAL
63+
fadeTime: 200, // GLOBAL
64+
overlayOpacity: 0.7, // GLOBAL
6165
closeButton: '.close-it',
6266
disableCloseOnOverlayClick: true,
6367
disableCloseOnEscape: true,
64-
fadeTime: 0,
68+
modalCenter: true,
6569
});</textarea>
6670
<h3 class="button clickable rounded leanmodal-modified" data-modal-id="#modified-example">Modified Example</h3>
6771
</section>
@@ -90,11 +94,9 @@ <h3>Hello!</h3>
9094

9195
<script>
9296
$('.leanmodal-modified').leanModal({
93-
overlayOpacity: 0.3,
9497
closeButton: '.close-it',
9598
disableCloseOnOverlayClick: true,
9699
disableCloseOnEscape: true,
97-
fadeTime: 0,
98100
});
99101
</script>
100102

jQuery.leanModal2.js

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// jQuery.leanModal2.js v2.6.2
1+
//// jQuery.leanModal2.js v2.6.3
22
// MIT Licensed by eustasy https://eustasy.org
33
// Based on leanModal v1.1 by Ray Stone - http://finelysliced.com.au
44

@@ -16,32 +16,77 @@
1616
//// Default Options
1717
// Set some Defaults.
1818
var defaults = {
19-
overlayOpacity: 0.7,
19+
defaultStyles: true, // GLOBAL
20+
fadeTime: 200, // GLOBAL
21+
overlayOpacity: 0.7, // GLOBAL
2022
closeButton: '.js-leanmodal-close',
2123
disableCloseOnOverlayClick: false,
2224
disableCloseOnEscape: false,
23-
fadeTime: 200,
2425
modalCenter: true,
2526
}
2627
// Merge in any passed options.
2728
options = $.extend(defaults, options)
2829

30+
//// Default styles
31+
if ( options.defaultStyles ) {
32+
if ( $('#js-leanmodal-styles').length == 0 ) {
33+
$('<style>')
34+
.prop('type', 'text/css')
35+
.prop('id', 'js-leanmodal-styles')
36+
.html('\
37+
@keyframes smoothFadeOut {\
38+
from { opacity: 1; }\
39+
to { opacity: 0; }\
40+
}\
41+
@keyframes smoothFadeIn {\
42+
from { opacity: 0; }\
43+
to { opacity: 1; }\
44+
}\
45+
#js-leanmodal-overlay {\
46+
align-items: center;\
47+
background: rgba(0, 0, 0, ' + options.overlayOpacity + ');\
48+
display: none;\
49+
height: 100%;\
50+
justify-content: center;\
51+
left: 0;\
52+
position: fixed;\
53+
top: 0;\
54+
width: 100%;\
55+
}\
56+
.js-leanmodal-link {\
57+
cursor: pointer;\
58+
}\
59+
.js-leanmodal-inactive {\
60+
animation: smoothFadeOut ' + options.fadeTime + 'ms ease-in-out both;\
61+
}\
62+
.js-leanmodal-active {\
63+
animation: smoothFadeIn ' + options.fadeTime + 'ms ease-in-out both;\
64+
display: block;\
65+
z-index: 1000\
66+
}\
67+
#js-leanmodal-overlay.js-leanmodal-active {\
68+
display: flex;\
69+
z-index: 100;\
70+
}'
71+
).appendTo('head')
72+
}
73+
}
74+
2975
//// Close the Modal
3076
// FUNCTION: Fade out the overlay and a passed identifier.
3177
function leanModal_Close(modal_id) {
32-
$('#js-leanmodal-overlay').fadeOut(options.fadeTime)
33-
$(modal_id).fadeOut(options.fadeTime)
78+
$('#js-leanmodal-overlay').removeClass('js-leanmodal-active').addClass('js-leanmodal-inactive')
79+
$(modal_id).removeClass('js-leanmodal-active').addClass('js-leanmodal-inactive')
3480
$('#js-leanmodal-overlay').unbind('click')
3581
$(document).off('keyup')
3682
$(modal_id).appendTo('body')
37-
$('#js-leanmodal-overlay').remove()
3883
}
3984

4085
//// Everything is Linked
4186
// FOREACHLINK For each targeted link.
4287
return this.each(function() {
4388
// Force this to look like a link.
44-
$(this).css({ 'cursor': 'pointer' })
89+
$(this).addClass('js-leanmodal-link')
4590

4691
//// Command Override
4792
// Override the existing click command,
@@ -85,12 +130,7 @@
85130
//// There can be only one.
86131
// Overlay. If there isn't an overlay, add one.
87132
if ( $('#js-leanmodal-overlay').length == 0 ) {
88-
var style =
89-
'background: rgba(0, 0, 0, ' +
90-
options.overlayOpacity +
91-
'); display: none; height: 100%; left: 0; position: fixed; top: 0; width: 100%; z-index: 100; ' +
92-
'align-items: center; justify-content: center;'
93-
var overlay = $('<div id="js-leanmodal-overlay" style="' + style + '"></div>')
133+
var overlay = $('<div id="js-leanmodal-overlay"></div>')
94134
$('body').append(overlay)
95135
}
96136

@@ -106,24 +146,15 @@
106146
}
107147

108148
//// Modal Positioning
109-
// Position the modal centrally using JavaScript, because CSS sucks.
110-
// Actually it doesn't, but it is hard to globally position.
111-
//var modal_height = $(modal_id).innerHeight()
112-
//var modal_width = $(modal_id).innerWidth()
149+
// Position the modal centrally inside the overlay using flexbox.
113150
if ( options.modalCenter ) {
114-
$(modal_id).css({
115-
'display': 'block',
116-
'opacity': 0,
117-
'z-index': 11000,
118-
})
119151
$(modal_id).appendTo('#js-leanmodal-overlay')
120152
}
121153

122154
//// Curtain Up
123155
// Fade in the modal and overlay.
124-
$('#js-leanmodal-overlay').css({ 'display': 'flex', opacity: 0 })
125-
$('#js-leanmodal-overlay').fadeTo(options.fadeTime, 1)
126-
$(modal_id).fadeTo(options.fadeTime, 1)
156+
$('#js-leanmodal-overlay').removeClass('js-leanmodal-inactive').addClass('js-leanmodal-active')
157+
$(modal_id).removeClass('js-leanmodal-inactive').addClass('js-leanmodal-active')
127158

128159
//// Default Prevention
129160
// Prevent whatever the default was (probably scrolling).

jQuery.leanModal2.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)