This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
jquery-multi-step-form.js
160 lines (152 loc) · 6.4 KB
/
jquery-multi-step-form.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
(function($) {
var FORM = $.multistepform = function() {
FORM.init.apply( this, arguments );
};
$.extend(FORM, {
current_fs: false,
next_fs: false,
previous_fs: false,
animating: false,
defaults: {
container:'',
duration: 1000,
form_url: '',
form_method: 'POST',
onClose:function(){},
},
init: function(opts) {
FORM.opts = $.extend(true, {}, FORM.defaults, opts);
if(FORM.opts['form_url']) {
var cont = $('<div id="'+FORM.opts['container']+'" class="multistepform" style="display:none; position: absolute; top:0; width: 100%; height: 100%; z-index: 9999;"></div>');
$('body').append(cont);
cont.html('<div id="multistepform"><div class="bg"></div><div class="close"></div><div id="multistepform-container"></div></div>');
$.get(FORM.opts['form_url'], function( data ) {
$('#multistepform-container').html(data);
FORM._show();
});
} else {
var cont = $('#'+FORM.opts['container']).addClass('multistepform').css({
'display':'none',
'position': 'absolute',
'top':0,
'width': '100%',
'height': '100%',
'z-index': 9999
});
var data = cont.html();
cont.html('<div id="multistepform"><div class="bg"></div><div class="close"></div><div id="multistepform-container">'+data+'</div></div>');
FORM._show();
}
},
_show: function() {
FORM.current_fs = $($('#'+FORM.opts['container']).find('.form')[0]);
$('#'+FORM.opts['container']+' .next').click(FORM.next);
$('#'+FORM.opts['container']+' .previous').click(FORM.prev);
$('#'+FORM.opts['container']+' .close').click(FORM.close);
$('#'+FORM.opts['container']).show();
$('body').css({'overflow':'hidden'});
},
_next: function() {
FORM.next_fs = FORM.current_fs.next();
//activate next step on progressbar using the index of next_fs
$("#multistepform-progressbar li").eq($(".form").index(FORM.next_fs)).addClass("active");
//show the next fieldset
FORM.next_fs.show();
//hide the current fieldset with style
FORM.current_fs.animate({opacity: 0}, {
step: function(now, mx) {
//as the opacity of current_fs reduces to 0 - stored in "now"
//1. scale current_fs down to 80%
scale = 1 - (1 - now) * 0.2;
//2. bring next_fs from the right(50%)
left = (now * 50)+"%";
//3. increase opacity of next_fs to 1 as it moves in
opacity = 1 - now;
FORM.current_fs.css({'transform': 'scale('+scale+')'});
FORM.next_fs.css({'left': left, 'opacity': opacity});
},
duration: FORM.opts['duration'],
complete: function(){
if(!FORM.next_fs[0]) {
FORM._close();
}
FORM.current_fs.hide();
FORM.current_fs = FORM.next_fs;
FORM.animating = false;
var curent_forms = FORM.current_fs.find('form');
for(var i=0;i<curent_forms.length;i++) {
(function(f){
$(f).find('.submit').click(function(e){
var form = f;
var form_data = $(form).serialize();
var url = $(form).attr('action');
$.ajax({
type: FORM.opts['container'],
url: url,
data: form_data
});
e.preventDefault();
});
})(curent_forms[i]);
}
},
//this comes from the custom easing plugin
easing: 'easeInOutBack'
});
},
_close: function() {
$('#'+FORM.opts['container']).hide();
$('body').css({'overflow':'auto'});
},
close: function() {
FORM.opts['onClose']();
FORM._close();
},
next: function(){
if(FORM.animating) return false;
FORM.animating = true;
var form = $(FORM.current_fs).children('form');
if(form.length) {
var form_data = $(form[0]).serialize();
$.ajax({
type: FORM.opts['form_method'],
url:$(form[0]).attr('action'),
data: form_data,
success: function(){
FORM._next();
}
});
} else {
FORM._next();
}
},
prev: function() {
if(FORM.animating) return false;
FORM.animating = true;
FORM.previous_fs = FORM.current_fs.prev();
//de-activate current step on progressbar
$("#multistepform-progressbar li").eq($(".form").index(FORM.current_fs)).removeClass("active");
FORM.previous_fs.show();
FORM.current_fs.animate({opacity: 0}, {
step: function(now, mx) {
//as the opacity of current_fs reduces to 0 - stored in "now"
//1. scale previous_fs from 80% to 100%
scale = 0.8 + (1 - now) * 0.2;
//2. take current_fs to the right(50%) - from 0%
left = ((1-now) * 50)+"%";
//3. increase opacity of previous_fs to 1 as it moves in
opacity = 1 - now;
FORM.current_fs.css({'left': left});
FORM.previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
},
duration: FORM.opts['duration'],
complete: function(){
FORM.current_fs.hide();
FORM.current_fs = FORM.previous_fs;
FORM.animating = false;
},
easing: 'easeInOutBack'
});
}
});
})(jQuery);