PageSourceSearch

https://blechacz.net/html/javascript/carousel.js

js blechacz.net collected 2026-10-02 12:25:00 UTC 14,501 bytes, 348 lines download raw bytes

1// JavaScript Document
2/**
3 * jCarouselLite - jQuery plugin to navigate images/any content in a carousel style widget.
4 * @requires jQuery v1.2 or above
5 *
6 * http://gmarwaha.com/jquery/jcarousellite/
7 *
8 * Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
12 *
13 * Version: 1.0.1
14 * Note: Requires jquery 1.2 or above from version 1.0.1
15 */
16
17/**
18 * Creates a carousel-style navigation widget for images/any-content from a simple HTML markup.
19 *
20 * The HTML markup that is used to build the carousel can be as simple as...
21 *
22 *  <div class="carousel">
23 *      <ul>
24 *          <li><img src="image/1.jpg" alt="1"></li>
25 *          <li><img src="image/2.jpg" alt="2"></li>
26 *          <li><img src="image/3.jpg" alt="3"></li>
27 *      </ul>
28 *  </div>
29 *
30 * As you can see, this snippet is nothing but a simple div containing an unordered list of images.
31 * You don't need any special "class" attribute, or a special "css" file for this plugin.
32 * I am using a class attribute just for the sake of explanation here.
33 *
34 * To navigate the elements of the carousel, you need some kind of navigation buttons.
35 * For example, you will need a "previous" button to go backward, and a "next" button to go forward.
36 * This need not be part of the carousel "div" itself. It can be any element in your page.
37 * Lets assume that the following elements in your document can be used as next, and prev buttons...
38 *
39 * <button class="prev">&lt;&lt;</button>
40 * <button class="next">&gt;&gt;</button>
41 *
42 * Now, all you need to do is call the carousel component on the div element that represents it, and pass in the
43 * navigation buttons as options.
44 *
45 * $(".carousel").jCarouselLite({
46 *      btnNext: ".next",
47 *      btnPrev: ".prev"
48 * });
49 *
50 * That's it, you would have now converted your raw div, into a magnificient carousel.
51 *
52 * There are quite a few other options that you can use to customize it though.
53 * Each will be explained with an example below.
54 *
55 * @param an options object - You can specify all the options shown below as an options object param.
56 *
57 * @option btnPrev, btnNext : string - no defaults
58 * @example
59 * $(".carousel").jCarouselLite({
60 *      btnNext: ".next",
61 *      btnPrev: ".prev"
62 * });
63 * @desc Creates a basic carousel. Clicking "btnPrev" navigates backwards and "btnNext" navigates forward.
64 *
65 * @option btnGo - array - no defaults
66 * @example
67 * $(".carousel").jCarouselLite({
68 *      btnNext: ".next",
69 *      btnPrev: ".prev",
70 *      btnGo: [".0", ".1", ".2"]
71 * });
72 * @desc If you don't want next and previous buttons for navigation, instead you prefer custom navigation based on
73 * the item number within the carousel, you can use this option. Just supply an array of selectors for each element
74 * in the carousel. The index of the array represents the index of the element. What i mean is, if the
75 * first element in the array is ".0", it means that when the element represented by ".0" is clicked, the carousel
76 * will slide to the first element and so on and so forth. This feature is very powerful. For example, i made a tabbed
77 * interface out of it by making my navigation elements styled like tabs in css. As the carousel is capable of holding
78 * any content, not just images, you can have a very simple tabbed navigation in minutes without using any other plugin.
79 * The best part is that, the tab will "slide" based on the provided effect. :-)
80 *
81 * @option mouseWheel : boolean - default is false
82 * @example
83 * $(".carousel").jCarouselLite({
84 *      mouseWheel: true
85 * });
86 * @desc The carousel can also be navigated using the mouse wheel interface of a scroll mouse instead of using buttons.
87 * To get this feature working, you have to do 2 things. First, you have to include the mouse-wheel plugin from brandon.
88 * Second, you will have to set the option "mouseWheel" to true. That's it, now you will be able to navigate your carousel
89 * using the mouse wheel. Using buttons and mouseWheel or not mutually exclusive. You can still have buttons for navigation
90 * as well. They complement each other. To use both together, just supply the options required for both as shown below.
91 * @example
92 * $(".carousel").jCarouselLite({
93 *      btnNext: ".next",
94 *      btnPrev: ".prev",
95 *      mouseWheel: true
96 * });
97 *
98 * @option auto : number - default is null, meaning autoscroll is disabled by default
99 * @example
100 * $(".carousel").jCarouselLite({
101 *      auto: 800,
102 *      speed: 500
103 * });
104 * @desc You can make your carousel auto-navigate itself by specfying a millisecond value in this option.
105 * The value you specify is the amount of time between 2 slides. The default is null, and that disables auto scrolling.
106 * Specify this value and magically your carousel will start auto scrolling.
107 *
108 * @option speed : number - 200 is default
109 * @example
110 * $(".carousel").jCarouselLite({
111 *      btnNext: ".next",
112 *      btnPrev: ".prev",
113 *      speed: 800
114 * });
115 * @desc Specifying a speed will slow-down or speed-up the sliding speed of your carousel. Try it out with
116 * different speeds like 800, 600, 1500 etc. Providing 0, will remove the slide effect.
117 *
118 * @option easing : string - no easing effects by default.
119 * @example
120 * $(".carousel").jCarouselLite({
121 *      btnNext: ".next",
122 *      btnPrev: ".prev",
123 *      easing: "bounceout"
124 * });
125 * @desc You can specify any easing effect. Note: You need easing plugin for that. Once specified,
126 * the carousel will slide based on the provided easing effect.
127 *
128 * @option vertical : boolean - default is false
129 * @example
130 * $(".carousel").jCarouselLite({
131 *      btnNext: ".next",
132 *      btnPrev: ".prev",
133 *      vertical: true
134 * });
135 * @desc Determines the direction of the carousel. true, means the carousel will display vertically. The next and
136 * prev buttons will slide the items vertically as well. The default is false, which means that the carousel will
137 * display horizontally. The next and prev items will slide the items from left-right in this case.
138 *
139 * @option circular : boolean - default is true
140 * @example
141 * $(".carousel").jCarouselLite({
142 *      btnNext: ".next",
143 *      btnPrev: ".prev",
144 *      circular: false
145 * });
146 * @desc Setting it to true enables circular navigation. This means, if you click "next" after you reach the last
147 * element, you will automatically slide to the first element and vice versa. If you set circular to false, then
148 * if you click on the "next" button after you reach the last element, you will stay in the last element itself
149 * and similarly for "previous" button and first element.
150 *
151 * @option visible : number - default is 3
152 * @example
153 * $(".carousel").jCarouselLite({
154 *      btnNext: ".next",
155 *      btnPrev: ".prev",
156 *      visible: 4
157 * });
158 * @desc This specifies the number of items visible at all times within the carousel. The default is 3.
159 * You are even free to experiment with real numbers. Eg: "3.5" will have 3 items fully visible and the
160 * last item half visible. This gives you the effect of showing the user that there are more images to the right.
161 *
162 * @option start : number - default is 0
163 * @example
164 * $(".carousel").jCarouselLite({
165 *      btnNext: ".next",
166 *      btnPrev: ".prev",
167 *      start: 2
168 * });
169 * @desc You can specify from which item the carousel should start. Remember, the first item in the carousel
170 * has a start of 0, and so on.
171 *
172 * @option scrool : number - default is 1
173 * @example
174 * $(".carousel").jCarouselLite({
175 *      btnNext: ".next",
176 *      btnPrev: ".prev",
177 *      scroll: 2
178 * });
179 * @desc The number of items that should scroll/slide when you click the next/prev navigation buttons. By
180 * default, only one item is scrolled, but you may set it to any number. Eg: setting it to "2" will scroll
181 * 2 items when you click the next or previous buttons.
182 *
183 * @option beforeStart, afterEnd : function - callbacks
184 * @example
185 * $(".carousel").jCarouselLite({
186 *      btnNext: ".next",
187 *      btnPrev: ".prev",
188 *      beforeStart: function(a) {
189 *          alert("Before animation starts:" + a);
190 *      },
191 *      afterEnd: function(a) {
192 *          alert("After animation ends:" + a);
193 *      }
194 * });
195 * @desc If you wanted to do some logic in your page before the slide starts and after the slide ends, you can
196 * register these 2 callbacks. The functions will be passed an argument that represents an array of elements that
197 * are visible at the time of callback.
198 *
199 *
200 * @cat Plugins/Image Gallery
201 * @author Ganeshji Marwaha/[email protected]
202 */
203
204(function($) {                                          // Compliant with jquery.noConflict()
205$.fn.jCarouselLite = function(o) {
206    o = $.extend({
207        btnPrev: null,
208        btnNext: null,
209        btnGo: null,
210        mouseWheel: false,
211        auto: null,
212
213        speed: 200,
214        easing: null,
215
216        vertical: false,
217        circular: true,
218        visible: 3,
219        start: 0,
220        scroll: 1,
221
222        beforeStart: null,
223        afterEnd: null
224    }, o || {});
225
226    return this.each(function() {                           // Returns the element collection. Chainable.
227
228        var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
229        var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;
230
231        if(o.circular) {
232            ul.prepend(tLi.slice(tl-v-1+1).clone())
233              .append(tLi.slice(0,v).clone());
234            o.start += v;
235        }
236
237        var li = $("li", ul), itemLength = li.size(), curr = o.start;
238        div.css("visibility", "visible");
239
240        li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
241        ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
242        div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});
243
244        var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
245        var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
246        var divSize = liSize * v;                           // size of entire div(total length for just the visible items)
247
248		/**
249		 * poprawka SMI nie ustawiaj wysokości
250		 */
251//        li.css({width: li.width(), height: li.height()});
252		li.css({width: li.width()});
253        ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));
254
255        div.css(sizeCss, divSize+"px");
255                     // Width of the DIV. length of visible images
256
257        if(o.btnPrev)
258            $(o.btnPrev).click(function() {
259                return go(curr-o.scroll);
260            });
261
262        if(o.btnNext)
263            $(o.btnNext).click(function() {
264                return go(curr+o.scroll);
265            });
266
267        if(o.btnGo)
268            $.each(o.btnGo, function(i, val) {
269                $(val).click(function() {
270					$('.years a').removeClass('active');
271					$(val).addClass('active');
272                    return go(o.circular ? o.visible+i : i);
273                });
274            });
275
276        if(o.mouseWheel && div.mousewheel)
277            div.mousewheel(function(e, d) {
278                return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
279            });
280
281        if(o.auto)
282            setInterval(function() {
283                go(curr+o.scroll);
284            }, o.auto+o.speed);
285
286        function vis() {
287            return li.slice(curr).slice(0,v);
288        };
289
290        function go(to) {
291            if(!running) {
292
293                if(o.beforeStart)
294                    o.beforeStart.call(this, vis());
295
296                if(o.circular) {            // If circular we are in first or last, then goto the other end
297                    if(to<=o.start-v-1) {           // If first, then goto last
298                        ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
299                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
300                        curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
301                    } else if(to>=itemLength-v+1) { // If last, then goto first
302                        ul.css(animCss, -( (v) * liSize ) + "px" );
303                        // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
304                        curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
305                    } else curr = to;
306                } else {                    // If non-circular and to points to first or last, we just return.
307                    if(to<0 || to>itemLength-v) return;
308                    else curr = to;
309                }                           // If neither overrides it, the curr will still be "to" and we can proceed.
310
311                running = true;
312
313                ul.animate(
314                    animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
315                    function() {
316                        if(o.afterEnd)
317                            o.afterEnd.call(this, vis());
318                        running = false;
319                    }
320                );
321                // Disable buttons when the carousel reaches the last/first, and enable when not
322                if(!o.circular) {
323                    $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
324                    $( (curr-o.scroll<0 && o.btnPrev)
325                        ||
326                       (curr+o.scroll > itemLength-v && o.btnNext)
327                        ||
328                       []
329                     ).addClass("disabled");
330                }
331
332            }
333            return false;
334        };
335    });
336};
337
338function css(el, prop) {
339    return parseInt($.css(el[0], prop)) || 0;
340};
341function width(el) {
342    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
343};
344function height(el) {
345    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
346};
347
348})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.