This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
/
animatedModal.js
126 lines (100 loc) · 3.98 KB
/
animatedModal.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
/*=========================================
* animatedModal.js: Version 1.0
* author: João Pereira
* website: https://joaopereira.pt
* email: joaopereirawd@gmail.com
* Licensed MIT
=========================================*/
(function ($) {
$.fn.animatedModal = function(options) {
var modal = $(this);
//Defaults
var settings = $.extend({
modalTarget: modal.attr('href').replace('#',''),
position:'fixed',
width:'100%',
height:'100%',
top:'0px',
left:'0px',
zIndexIn: '9999',
zIndexOut: '-9999',
color: '#39BEB9',
opacityIn:'1',
opacityOut:'0',
animatedIn:'zoomIn',
animatedOut:'zoomOut',
animationDuration:'.6s',
overflow:'auto',
// Callbacks
beforeOpen: function() {},
afterOpen: function() {},
beforeClose: function() {},
afterClose: function() {}
}, options);
var closeBt = $('.close-'+settings.modalTarget);
//console.log(closeBt)
var href = $(modal).attr('href'),
id = $('body').find('#'+settings.modalTarget),
idConc = '#'+id.attr('id');
//console.log(idConc);
// Default Classes
id.addClass('animated');
id.addClass(settings.modalTarget+'-off');
//Init styles
var initStyles = {
'position':settings.position,
'width':settings.width,
'height':settings.height,
'top':settings.top,
'left':settings.left,
'background-color':settings.color,
'overflow-y':settings.overflow,
'z-index':settings.zIndexOut,
'opacity':settings.opacityOut,
'-webkit-animation-duration':settings.animationDuration,
'-moz-animation-duration':settings.animationDuration,
'-ms-animation-duration':settings.animationDuration,
'animation-duration':settings.animationDuration
};
//Apply stles
id.css(initStyles);
modal.click(function(event) {
event.preventDefault();
$('body, html').css({'overflow':'hidden'});
if (href == idConc) {
if (id.hasClass(settings.modalTarget+'-off')) {
id.removeClass(settings.animatedOut);
id.removeClass(settings.modalTarget+'-off');
id.addClass(settings.modalTarget+'-on');
}
if (id.hasClass(settings.modalTarget+'-on')) {
settings.beforeOpen();
id.css({'opacity':settings.opacityIn,'z-index':settings.zIndexIn});
id.addClass(settings.animatedIn);
id.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', afterOpen);
};
}
});
closeBt.click(function(event) {
event.preventDefault();
$('body, html').css({'overflow':'auto'});
settings.beforeClose(); //beforeClose
if (id.hasClass(settings.modalTarget+'-on')) {
id.removeClass(settings.modalTarget+'-on');
id.addClass(settings.modalTarget+'-off');
}
if (id.hasClass(settings.modalTarget+'-off')) {
id.removeClass(settings.animatedIn);
id.addClass(settings.animatedOut);
id.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', afterClose);
};
});
function afterClose () {
id.css({'z-index':settings.zIndexOut});
settings.afterClose(); //afterClose
}
function afterOpen () {
settings.afterOpen(); //afterOpen
}
}; // End animatedModal.js
}(jQuery));