-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbac-1.0.js
225 lines (168 loc) · 6.52 KB
/
mbac-1.0.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Multiple Before and After Comparer v.1
*
* Authored by Jan Duldulao
*
*
* Copyright 2017, Jan Duldulao
* License: GNU General Public License, version 3 (GPL-3.0)
* http://www.opensource.org/licenses/gpl-3.0.html
*
*/
;
(function ($) {
"use strict";
$.fn.mbac = function (o) {
// comparer default settings
var _width = $(this).width();
var defaults = {
width: _width,
height: "auto",
animduration: 450, // length of transition
animspeed: 4000, // delay between transitions
target: "ul.mbac",
z_index: 500,
responsive: false
};
// create settings from defauls and user options
var settings = $.extend({}, defaults, o);
this.each(function () {
var $wrapper = $(this),
$slider = $wrapper.find(settings.target),
$slides = $slider.children('li'),
// marker elements
$m_wrapper = null,
$m_markers = null;
var state = {
slidecount: $slides.length, // total number of slides
animating: false,
paused: false,
currentindex: 0
};
var responsive = {
width: $wrapper.width(),
height: null,
ratio: null
};
// run through options and initialise settings
var init = function () {
// differentiate slider li from content li
$slides.addClass('mbac-slide');
// conf dimensions, responsive or static
if (settings.responsive) {
confResponsive();
} else {
confStatic();
}
// slide components are hidden by default, show them now
$slider.show();
}; // init
var calcSlidesWidth = function (wx) {
var z_index = settings.z_index;
var start_width = wx / 2 / (state.slidecount - 1);
var x = start_width;
var increment = start_width;
for (var i = 0; i < state.slidecount; i++) {
if (i + 1 < state.slidecount) {
$($slides[i])
.css({
"width": x,
"z-index": z_index -= 5,
position: "absolute"
})
.data("wz", {w: x, z: z_index -= 5});
} else {
$($slides[i]).css({
"width": "100%",
"z-index": z_index -= 5
})
.data("wz", {w: "100%", z: z_index -= 5});
}
x = x + increment;
}
};
var conf_action = function () {
$slides.on('mouseover', function (e) {
e.preventDefault();
if (!state.animating) {
open($(this).index());
}
});
$slides.on('mouseleave', function (e) {
e.preventDefault();
if (!state.animating) {
close($(this).index());
}
});
};
var confStatic = function () {
calcSlidesWidth(settings.width);
var h_arr = [];
var h = "100%";
for(var i = $slides.length-1;i>=0;i--) {
if(i == $slides.length-1) {
$($slides[i]).css("position","relative");
}
$($slides[i]).children().width(settings.width);
}
$wrapper.css({
'height': settings.height,
'width': settings.width,
'position': 'relative',
'margin': "auto",
'overflow': 'hidden'
});
conf_action();
};
var confResponsive = function () {
$slider.addClass("responsive");
var h_arr = [], w_arr = [];
$slides.children('img').each(function(){
h_arr.push($(this).height());
w_arr.push($(this).width());
});
var h_final = Math.min.apply(Math,h_arr); // get the min height
var w_final = Math.max.apply(Math,w_arr); // get the min width
calcSlidesWidth(w_final);
$wrapper.css({
'height': h_final,
'width': "100%",
'position': 'relative',
'margin': "auto"
});
conf_action();
};
var open = function (index) {
state.currentindex = index;
var wz = $($slides[state.currentindex]).data("wz");
$($slides[state.currentindex])
.css({
"width" : "100%"
})
.prevAll()
.css({
"width": 0
});
};
var close = function (index) {
var wz = $($slides[state.currentindex]).data("wz");
var i = index;
$($slides[state.currentindex])
.css({
"width": wz.w,
"z-index": wz.z
})
.prevAll().each(function(){
i--;
var prev_wz = $($slides[i]).data("wz");
$(this).css({
"width": prev_wz.w,
"z-index": prev_wz.z
});
});
};
// lets get the party started :)
init();
});
};
})(jQuery);