/
jquery.carousel.js
143 lines (118 loc) · 4.57 KB
/
jquery.carousel.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
(function( $ ){
var settings = {};
var currentPage = 1;
var totalPages = 1;
var methods = {
init : function( options ) {
settings = $.extend( {
'perpage' : 5
,'width' : '800'
,'showPager': true
,'animationSpeed': 1000
,'animationType': 'swing'
,'startPage': 1
,'onPageChangeStart': null
,'onPageChangeEnd': null
}, options);
var self = this;
// paging
return this.each(function() {
var $this = $(this);
// handle building of the pager
var ul = $(".carousel-holder ul", $this);
var lis = $("li", ul);
var liw = $(lis[0]).outerWidth();
ul.width(liw*lis.length); // set the correct width whilst we're here
var ulw = ul.outerWidth();
totalPages = Math.round(ulw/liw/settings.perpage);
if(settings.showPager) {
for(var i = 0; i < totalPages; i++)
{
$(".controls", $this).append("<li><a href=\"#\"></a></li>");
}
$(".controls li:first-child", $this).addClass("selected");
// set up the events for the pager
$(".controls li a", $this).click(function () {
if(!$(".carousel-holder ul", $this).is(":animated"))
{
// adjust the margin
var index = $(this).parent().index();
var newPage = index+1;
$this.carousel("changePage", newPage);
currentPage = newPage;
}
return false;
});
}
// handle left/right arrows
$(".carousel-holder .arrowl", $this).click(function () {
var carousel = $("ul", $(this).parent());
if(!carousel.is(":animated")) {
if(currentPage > 1) {
var newPage = currentPage-1;
$this.carousel("changePage", currentPage-1);
currentPage = newPage;
}
}
return false;
});
$(".carousel-holder .arrowr", $this).click(function () {
var carousel = $("ul", $(this).parent());
if(!carousel.is(":animated")) {
if(currentPage < totalPages) {
var newPage = currentPage+1;
$this.carousel("changePage", newPage);
currentPage = newPage;
}
}
return false;
});
// hide all but the first item!
$(".items > div", $this).hide();
$(".items div:first-child", $this).show();
$(".carousel-holder li", $this).click(function () {
var index = $(this).index()+1;
$("li", $(this).parent()).removeClass("selected");
$(this).addClass("selected");
$(".items div.item", $this).hide();
$(".items div.item[rel='" + index + "']", $this).show();
return false;
});
if(settings.startPage > 1 && settings.startPage <= totalPages) {
$this.carousel("changePage", settings.startPage, false);
}
});
},
changePage: function (page, animate) {
this.each(function () {
var ml = ((page-1)*-1*settings.width) + 'px';
// animate the slide if we want to!
if(animate != false) {
if(jQuery.isFunction(settings.onPageChangeStart)) {
settings.onPageChangeStart.call(this, page);
}
$(".carousel-holder ul", $(this)).animate({'margin-left': ml}, settings.animationSpeed, settings.animationType, function () {
if(jQuery.isFunction(settings.onPageChangeEnd)) {
settings.onPageChangeEnd.call(this, page);
}
});
} else {
$(".carousel-holder ul").css({'margin-left': ml});
}
// update the paging icon!
$(".controls li.selected", $(this)).removeClass("selected");
$(".controls li:nth-child(" + (page) + ")", $(this)).addClass("selected");
});
}
};
$.fn.carousel = function( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.dcSlider' );
}
};
})( jQuery );