blob: f09b0f3a74ac4b3efb28b6a818447ce4a5e854de [file] [log] [blame]
Scott Maine4d8f1b2012-06-21 18:03:05 -07001/*
2 * jQuery dacSlideshow 1.0
3 *
4 * Sample usage:
5 * HTML -
6 * <div class="slideshow-container">
7 * <a href="" class="slideshow-prev">Prev</a>
8 * <a href="" class="slideshow-next">Next</a>
9 * <ul>
10 * <li class="item"><img src="images/marquee1.jpg"></li>
11 * <li class="item"><img src="images/marquee2.jpg"></li>
12 * <li class="item"><img src="images/marquee3.jpg"></li>
13 * <li class="item"><img src="images/marquee4.jpg"></li>
14 * </ul>
15 * </div>
16 *
17 * <script type="text/javascript">
18 * $('.slideshow-container').dacSlideshow({
19 * auto: true,
20 * btnPrev: '.slideshow-prev',
21 * btnNext: '.slideshow-next'
22 * });
23 * </script>
24 *
25 * Options:
26 * btnPrev: optional identifier for previous button
27 * btnNext: optional identifier for next button
28 * auto: whether or not to auto-proceed
29 * speed: animation speed
30 * autoTime: time between auto-rotation
31 * easing: easing function for transition
32 * start: item to select by default
33 * scroll: direction to scroll in
34 * pagination: whether or not to include dotted pagination
35 *
36 */
37
38 (function($) {
39 $.fn.dacSlideshow = function(o) {
40
41 //Options - see above
42 o = $.extend({
43 btnPrev: null,
44 btnNext: null,
45 auto: true,
46 speed: 500,
47 autoTime: 12000,
48 easing: null,
49 start: 0,
50 scroll: 1,
51 pagination: true
52
53 }, o || {});
54
55 //Set up a carousel for each
56 return this.each(function() {
57
58 var running = false;
59 var animCss = o.vertical ? "top" : "left";
60 var sizeCss = o.vertical ? "height" : "width";
61 var div = $(this);
62 var ul = $("ul", div);
63 var tLi = $("li", ul);
64 var tl = tLi.size();
65 var timer = null;
66
67 var li = $("li", ul);
68 var itemLength = li.size();
69 var curr = o.start;
70
71 li.css({float: o.vertical ? "none" : "left"});
72 ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
73 div.css({position: "relative", "z-index": "2", left: "0px"});
74
75 var liSize = o.vertical ? height(li) : width(li);
76 var ulSize = liSize * itemLength;
77 var divSize = liSize;
78
79 li.css({width: li.width(), height: li.height()});
80 ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));
81
82 div.css(sizeCss, divSize+"px");
83
84 //Pagination
85 if (o.pagination) {
86 var pagination = $("<div class='pagination'></div>");
87 var pag_ul = $("<ul></ul>");
88 if (tl > 1) {
89 for (var i=0;i<tl;i++) {
90 var li = $("<li>"+i+"</li>");
91 pag_ul.append(li);
92 if (i==o.start) li.addClass('active');
93 li.click(function() {
94 go(parseInt($(this).text()));
95 })
96 }
97 pagination.append(pag_ul);
98 div.append(pagination);
99 }
100 }
101
102 //Previous button
103 if(o.btnPrev)
104 $(o.btnPrev).click(function(e) {
105 e.preventDefault();
106 return go(curr-o.scroll);
107 });
108
109 //Next button
110 if(o.btnNext)
111 $(o.btnNext).click(function(e) {
112 e.preventDefault();
113 return go(curr+o.scroll);
114 });
115
116 //Auto rotation
117 if(o.auto) startRotateTimer();
118
119 function startRotateTimer() {
120 clearInterval(timer);
121 timer = setInterval(function() {
122 if (curr == tl-1) {
123 go(0);
124 } else {
125 go(curr+o.scroll);
126 }
127 }, o.autoTime);
128 }
129
130 //Go to an item
131 function go(to) {
132 if(!running) {
133
134 if(to<0) {
135 to = itemLength-1;
136 } else if (to>itemLength-1) {
137 to = 0;
138 }
139 curr = to;
140
141 running = true;
142
143 ul.animate(
144 animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
145 function() {
146 running = false;
147 }
148 );
149
150 $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
151 $( (curr-o.scroll<0 && o.btnPrev)
152 ||
153 (curr+o.scroll > itemLength && o.btnNext)
154 ||
155 []
156 ).addClass("disabled");
157
158
159 var nav_items = $('li', pagination);
160 nav_items.removeClass('active');
161 nav_items.eq(to).addClass('active');
162
163
164 }
165 if(o.auto) startRotateTimer();
166 return false;
167 };
168 });
169 };
170
171 function css(el, prop) {
172 return parseInt($.css(el[0], prop)) || 0;
173 };
174 function width(el) {
175 return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
176 };
177 function height(el) {
178 return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
179 };
180
181 })(jQuery);