-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipcontents.js
260 lines (224 loc) · 6.5 KB
/
flipcontents.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
flipcontents.js
Copyright 2013 Junichi Kakisako.
Licensed under the MIT License:
http://www.opensource.org/licenses/mit-license.php
* Requirement
- jQuery 1.7.1
http://jquery.com/
- flipsnap.js
http://pxgrid.github.com/js-flipsnap/
*/
(function(window, document, undefined) {
var FlipContents = function(opts) {
this.options = $.extend(true, {}, this.default_options, opts);
};
FlipContents.prototype = {
default_options: {
selector: {
contents_base: ".itemContents",
flip_contents: {
flip: ".flickableContents ul",
items: ".flickableContents li"
},
controls: {
base: ".flickableControl",
indicator: {
base: ".indicator",
dots: "span"
},
move: {
prev: ".prev",
next: ".next"
}
},
display: {
title: ".itemTitle .inner"
}
},
num: 3,
indicator_tip_width: 13,
title_attr: "data-title",
fix_padding: null,
init_pos: null,
x_threshold: 15,
y_threshold: 5,
animate_duration: 350
},
options: {},
_flip: null,
_get_base: function() {
return $(this.options.selector.contents_base);
},
_get_flip: function() {
return this._get_base()
.find(this.options.selector.flip_contents.flip);
},
_get_flip_items: function() {
return this._get_base()
.find(this.options.selector.flip_contents.items);
},
_get_controls: function() {
return this._get_base()
.find(this.options.selector.controls.base);
},
_get_indicator: function() {
return this._get_controls()
.find(this.options.selector.controls.indicator.base);
},
_get_indicator_dots: function() {
return this._get_indicator()
.find(this.options.selector.controls.indicator.dots);
},
_get_move_prev: function() {
return this._get_controls()
.find(this.options.selector.controls.move.prev);
},
_get_move_next: function() {
return this._get_controls()
.find(this.options.selector.controls.move.next);
},
_get_display_title: function() {
return this._get_base()
.find(this.options.selector.display.title);
},
_calc_item_width: function() {
return Math.floor(this._get_base().width() / this.options.num);
},
_calc_flip_padding: function() {
if (this.options.fix_padding === null) {
return Math.floor((this._get_base().width() / 2) -
(this._calc_item_width() / 2));
} else {
return this.options.fix_padding;
}
},
flip_factory: function(selector, conf) {
this._flip = Flipsnap(selector, conf);
},
on_resize: function() {
var flip = this._get_flip();
var flip_items = this._get_flip_items();
var indicator = this._get_indicator();
var item_count = flip_items.size();
var item_width = this._calc_item_width();
// update flip items
flip_items.css({
'width': item_width
});
flip.css({
'width': item_width * item_count
}).css({
'padding-left': this._calc_flip_padding()
});
// update indicator
if (indicator) {
indicator.css({
'width': (item_count * this.options.indicator_tip_width) + 'px'
});
}
this._flip.conf.distance = item_width;
this._flip.refresh();
},
update_controls: function() {
// update indicator
var indicator_dots = this._get_indicator_dots();
var current = this._flip.currentPoint;
if (indicator_dots) {
indicator_dots.removeClass("current");
indicator_dots.eq(current).addClass("current");
}
// update move control
var prev = this._get_move_prev();
var next = this._get_move_next();
if (this._flip.hasNext()) {
next.removeClass('disabled');
} else {
next.addClass('disabled');
}
if (this._flip.hasPrev()) {
prev.removeClass('disabled');
} else {
prev.addClass('disabled');
}
},
update_title: function() {
var title_ele = this._get_display_title();
if (title_ele.size() !== 0) {
var current = this._flip.currentPoint;
var flip_items = this._get_flip_items();
var target_item = flip_items.eq(current);
var title = target_item.attr(this.options.title_attr);
var link = target_item.children("a").attr("href");
var html = $('<a>').attr('href', link).html(title);
title_ele.html(html);
}
},
add_event_moveend: function() {
var self = this;
this._flip.element.addEventListener('fsmoveend', function() {
self.update_controls();
self.update_title();
}, false);
},
add_event_resize: function() {
var self = this;
$(window).bind("resize", function() {
self.on_resize();
});
},
add_event_move: function() {
var self = this;
this._get_move_prev().click(function() {
self._flip.toPrev();
return false;
});
this._get_move_next().click(function() {
self._flip.toNext();
return false;
});
},
_init: function() {
var indicator = this._get_indicator();
var flip_items = this._get_flip_items();
// init indicator
if (indicator) {
indicator.empty();
for (var i = 0; i < flip_items.size(); i++) {
indicator.append("<span></span>");
}
}
this.on_resize();
this.update_controls();
this.update_title();
// move to initialize position.
if (this.options.init_pos !== null) {
this._flip.moveToPoint(this.options.init_pos - 1);
}
},
setup: function() {
// create flip object
this.flip_factory(this._get_flip().selector, {
deltaXThreshold: this.options.x_threshold,
deltaYThreshold: this.options.y_threshold,
animateDuration: this.options.animate_duration
});
// setup event
this.add_event_resize();
this.add_event_move();
this.add_event_moveend();
// initialize
this._init();
},
attach_event: function(type, func) {
this._flip.element.addEventListener(type, func);
},
get_current_point: function() {
return this._flip.currentPoint;
},
move_to_point: function(point) {
this._flip.moveToPoint(point);
}
};
window.FlipContents = FlipContents;
})(window, window.document);