forked from lotsofcode/tinycarousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tinycarousel.js
183 lines (167 loc) · 6.42 KB
/
jquery.tinycarousel.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
/*
* Tiny Carousel 1.9
* http://www.baijs.nl/tinycarousel
*
* Copyright 2010, Maarten Baijs
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-2.0.php
*
* Modified: 25/09/2012 by @lotsofcode
* Dependencies: jQuery 1.7
*/
(function ($)
{
"use strict";
$.tiny = $.tiny || { };
$.tiny.carousel = {
options: {
start: 1, // where should the carousel start?
display: 1, // how many blocks do you want to move at 1 time?
axis: 'x', // vertical or horizontal scroller? ( x || y ).
controls: true, // show left and right navigation buttons.
pager: false, // is there a page number navigation present?
interval: false, // move to another block on intervals.
intervaltime: 3000, // interval time in milliseconds.
rewind: false, // If interval is true and rewind is true it will play in reverse if the last slide is reached.
animation: true, // false is instant, true is animate.
duration: 1000, // how fast must the animation move in ms?
callback: null // function that executes after every move.
}
};
$.fn.tinycarousel_start = function () { $(this).data('tcl').start(); };
$.fn.tinycarousel_stop = function () { $(this).data('tcl').stop(); };
$.fn.tinycarousel_move = function (iNum) { $(this).data('tcl').move(iNum - 1,true); };
$.fn.tinycarousel_destroy = function() { $(this).data('tcl').destroy(); };
function Carousel(root, options)
{
var oSelf = this
, oViewport = $('.viewport:first', root)
, oContent = $('.overview:first', root)
, oPages = oContent.children()
, oBtnNext = $('.next:first', root)
, oBtnPrev = $('.prev:first', root)
, oPager = $('.pager:first', root)
, iPageSize = 0
, iSteps = 0
, iCurrent = 0
, oTimer = undefined
, bPause = false
, bForward = true
, bAxis = options.axis === 'x'
;
function setButtons()
{
if(options.controls)
{
oBtnPrev.toggleClass('disable', iCurrent <= 0 );
oBtnNext.toggleClass('disable', !(iCurrent +1 < iSteps));
}
if(options.pager)
{
var oNumbers = $('.pagenum', oPager);
oNumbers.removeClass('active');
$(oNumbers[iCurrent]).addClass('active');
}
}
function setPager( oEvent )
{
if( $( this ).hasClass( "pagenum" ) )
{
oSelf.move( parseInt( this.rel, 10 ), true );
}
return false;
}
function setTimer()
{
if(options.interval && !bPause)
{
clearTimeout(oTimer);
oTimer = setTimeout(function(){
iCurrent = iCurrent +1 === iSteps ? -1 : iCurrent;
bForward = iCurrent +1 === iSteps ? false : iCurrent === 0 ? true : bForward;
oSelf.move(bForward ? 1 : -1);
}, options.intervaltime);
}
}
function setEvents()
{
oBtnPrev.off('click.tcl');
oBtnNext.off('click.tcl');
if (options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0)
{
oBtnPrev.on('click.tcl', function(e) {
oSelf.move(-1);
e.preventDefault();
});
oBtnNext.on('click.tcl', function(e){
oSelf.move(1);
e.preventDefault();
});
}
if(options.interval)
{
root.hover(oSelf.stop,oSelf.start);
}
if(options.pager && oPager.length > 0)
{
$('a',oPager).click(setPager);
}
}
this.stop = function () { clearTimeout(oTimer); bPause = true; };
this.start = function () { bPause = false; setTimer(); };
this.destroy = function() {
this.stop();
if(options.interval) {
root.unbind('mouseenter mouseleave');
}
if(options.pager && oPager.length > 0) {
$('a',oPager).unbind('click');
}
var position = {};
position[bAxis ? 'left' : 'top'] = 0;
oContent.animate(position, {queue: false, duration: 0});
};
this.move = function (iDirection, bPublic)
{
var tmpCurrent = iCurrent;
tmpCurrent = bPublic ? iDirection : tmpCurrent += iDirection;
if(tmpCurrent > -1 && tmpCurrent < iSteps)
{
iCurrent = tmpCurrent;
var oPosition = {};
oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));
oContent.animate(oPosition,{
queue: false,
duration: options.animation ? options.duration : 0,
complete: function()
{
if(typeof options.callback === 'function')
{
options.callback.call(this, oPages[iCurrent], iCurrent);
}
}
});
setButtons();
setTimer();
}
};
function initialize () {
iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
var iLeftover = Math.ceil(((bAxis ? oViewport.outerWidth() : oViewport.outerHeight()) / (iPageSize * options.display)) -1);
iSteps = Math.max(1, Math.ceil(oPages.length / options.display) - iLeftover);
iCurrent = Math.min(iSteps, Math.max(1, options.start)) -2;
oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
oSelf.move(1);
setEvents();
return oSelf;
}
return initialize();
}
$.fn.tinycarousel = function(params)
{
var options = $.extend({}, $.tiny.carousel.options, params);
this.each(function () { $(this).data('tcl', new Carousel($(this), options)); });
return this;
};
}(jQuery));