Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7272 amit.gupta 1
/** 
2
 * jQuery Slidy - A Transition Plugin - http://wbotelhos.com/slidy 
3
 * --------------------------------------------------------------------------------- 
4
 * 
5
 * jQuery Slidy is a plugin that generates a customizable transitions automatically. 
6
 * 
7
 * Licensed under The MIT License 
8
 * 
9
 * @version         0.2.0 
10
 * @since           11.16.2010 
11
 * @author          Washington Botelho dos Santos 
12
 * @documentation   wbotelhos.com/slidy 
13
 * @twitter         twitter.com/wbotelhos 
14
 * @license         opensource.org/licenses/mit-license.php MIT 
15
 * @package         jQuery Plugins 
16
 * 
17
 * Usage with default values: 
18
 * --------------------------------------------------------------------------------- 
19
 * $('#banner').slidy(); 
20
 * 
21
 * <div id="banner"> 
22
 *   <img src="image-1.jpg"/> 
23
 *   <img src="image-2.jpg"/> 
24
 *   <img src="image-3.jpg"/> 
25
 * </div> 
26
 * 
27
 */ 
28
 
29
;(function($) { 
30
 
31
    $.fn.slidy = function(settings) { 
32
 
33
        if (this.length == 0) { 
34
            debug('Selector invalid or missing!'); 
35
            return; 
36
        } else if (this.length > 1) { 
37
            return this.each(function() { 
38
                $.fn.slidy.apply($(this), [settings]); 
39
            }); 
40
        } 
41
 
42
        var opt            = $.extend({}, $.fn.slidy.defaults, settings), 
43
            $this        = $(this), 
44
            id            = this.attr('id'), 
45
            elements    = $this.children(opt.children), 
46
            quantity    = elements.length, 
47
            images        = (opt.children == 'img') ? elements : elements.find('img'), 
48
            timer        = 0, 
49
            isAnimate    = false; 
50
 
51
        if (id === undefined) { 
52
            id = 'slidy-' + $this.index(); 
53
            $this.attr('id', id);  
54
        } 
55
 
56
        $this 
57
        .data('options', opt) 
58
        .css({ 
59
            'cursor':    opt.cursor, 
60
            'height':    opt.height + 'px', 
61
            'overflow':    'hidden', 
62
            'position':    'relative', 
63
            'width':    opt.width + 'px' 
64
        }); 
65
 
66
        elements.each(function(i) { 
67
            $(this) 
68
            .css({ 'position': 'absolute', 'z-index': quantity - i }) 
69
            .attr('id', $this.attr('id') + '-' + (i + 1)); 
70
        }); 
71
 
72
        images.attr({ height: opt.height, width: opt.width }).css('border', '0'); 
73
 
74
        if (opt.children == 'a' && opt.target != '') { 
75
            elements.attr('target',    opt.target); 
76
        } 
77
 
78
        elements.hide().first().show(); 
79
 
80
        if (opt.menu) { 
81
            $menu = $('<ul/>', { id: id + '-slidy-menu', 'class': 'slidy-menu' }).insertAfter($this); 
82
        } 
83
 
84
        var stop = function() { 
85
            clearTimeout(timer); 
86
        }, overBanner = function() { 
87
            stop(); 
88
        }, overMenu = function(thiz) { 
89
            stop(); 
90
 
91
            var $this        = $(this), 
92
                index        = $this.index(), 
93
                $current    = $this.parent().children('.slidy-link-selected'), 
94
                last        = $current.index(); 
95
 
96
            if (index != last) { 
97
                $current.removeClass('slidy-link-selected'); 
98
                $this.addClass('slidy-link-selected'); 
99
 
100
                change(last, index); 
101
            } 
102
        }, outBanner = function(thiz) { 
103
            go($(thiz.target).parent('a').index()); 
104
        }, outMenu = function() { 
105
            var $this        = $(this), 
106
                index        = $this.index(), 
107
                $current    = $this.parent().children('.slidy-link-selected'), 
108
                last        = $current.index(); 
109
 
110
            go(last); 
111
        }, clickMenu = function(thiz) { 
112
            stop(); 
113
 
114
            var $this        = $(this), 
115
                index        = $this.index(), 
116
                $current    = $this.parent().children('.slidy-link-selected'), 
117
                last        = $current.index(); 
118
 
119
            if (index != last) { 
120
                $current.removeClass('slidy-link-selected'); 
121
                $this.addClass('slidy-link-selected');
122
                change(last, index); 
123
            } 
124
 
125
            go(index); 
126
        }; 
127
 
128
        if (opt.menu) { 
129
            var target    = (opt.target != '') ? 'target="' + opt.target + '"' : '', 
130
                menu    = '', 
131
                parent, 
132
                img; 
133
 
134
            images.each(function() { 
135
                img        = $(this); 
136
                parent    = img.parent(opt.children); 
137
 
138
                menu += '<li><a href="javascript:void(0)" ' + target + '>' + img.attr('title') + '</a></li>'; 
139
            }); 
140
 
141
            $menu.html(menu); 
142
 
143
            var    space    = parseInt((opt.width / quantity) + (quantity - 1)), 
144
                diff    = opt.width - (space * quantity), 
145
                links    = $menu.children('li'); 
146
 
147
            if (opt.action == 'mouseenter') { 
148
                links.mouseenter(overMenu).mouseleave(outMenu);     
149
            } else if (opt.action == 'click') { 
150
                links.click(clickMenu); 
151
            } else { 
152
                debug('action attribute must to be "click" or "mouseenter"!'); 
153
                return; 
154
            } 
155
 
156
            links.css('width', space) 
157
                .first().addClass('slidy-link-selected') 
158
            .end() 
159
                .last().css({ 'border-right': '0', 'width': (space + diff) - (quantity) }); 
160
 
161
            if (opt.animation == 'slide' || opt.animation == 'fade') { 
162
                links.mousemove(function() { 
163
                    var $this = $(this); 
164
 
165
                    if (!$this.hasClass('slidy-link-selected')) { 
166
                        $this.mouseenter(); 
167
                    } 
168
                }); 
169
            } 
170
        } 
171
 
172
        go(0); 
173
 
174
        if (opt.pause) { 
175
            $this.mouseenter(overBanner).mouseleave(outBanner); 
176
        } 
177
 
178
        function go(index) { 
179
            var total    = quantity - 1, 
180
                last    = null; 
181
 
182
            if (index > total) { 
183
                index = 0; 
184
                last = total; 
185
            } else if (index <= 0) { 
186
                index = 0; 
187
                last = total; 
188
            } else { 
189
                last = index - 1; 
190
            } 
191
 
192
            change(last, index); 
193
 
194
            timer =    setTimeout(function() { 
195
                    go(++index); 
196
                }, opt.time); 
197
        } 
198
 
199
        function change(last, index) { 
200
            if (!isAnimate) { 
201
                isAnimate = true; 
202
 
203
                if (opt.animation == 'fade') { 
204
                    elements 
205
                        .eq(last).fadeOut(opt.speed) 
206
                    .end() 
207
                        .eq(index).fadeIn(opt.speed, function() { 
208
                            selectMenu(index); 
209
                            isAnimate = false; 
210
                        }); 
211
                } else if (opt.animation == 'slide') { 
212
                    animatedElement = elements.eq(index); 
213
                    elements.css('z-index', 0) 
214
                        .eq(index).css('z-index', quantity).slideLeftShow(opt.speed, function() { 
215
                                elements.eq(last).hide(); 
216
                                selectMenu(index); 
217
                                isAnimate = false; 
218
                        }); 
219
                } else { 
220
                    elements 
221
                    .eq(last).hide() 
222
                    .end() 
223
                    .eq(index).show(0, function() { 
224
                        selectMenu(index); 
225
                        isAnimate = false; 
226
                    }); 
227
                } 
228
            } 
229
        }; 
230
 
231
        function selectMenu(index) { 
232
            if (opt.menu) { 
233
                $this 
234
                    .next('ul.slidy-menu') 
235
                        .children().removeClass('slidy-link-selected') 
236
                            .eq(index).addClass('slidy-link-selected'); 
237
            } 
238
        }; 
239
 
240
        return $this; 
241
    }; 
242
 
243
    function debug(message) { 
244
        if (window.console && window.console.log) { 
245
            window.console.log(message); 
246
        } 
247
    }; 
248
 
249
    $.fn.slidy.defaults = { 
250
        action:        'click', 
251
        animation:    'slide', 
252
        children:    'img', 
253
        cursor:        'default', 
254
        height:        200, 
255
        menu:        false, 
256
        pause:        false, 
257
        speed:        600, 
258
        target:        '', 
259
        time:        3600, 
260
        width:        710 
261
    }; 
262
 
263
    $.fn.slideLeftHide = function(speed, callback) { 
264
        this.animate({ 
265
            width: "hide",  
266
            paddingLeft: "hide",  
267
            paddingRight: "hide",  
268
            marginLeft: "hide",  
269
            marginRight: "hide" 
270
        }, speed, callback); 
271
    } 
272
 
273
    $.fn.slideLeftShow = function(speed, callback) { 
274
        this.animate({ 
275
            width: "show",  
276
            paddingLeft: "show",  
277
            paddingRight: "show",  
278
            marginLeft: "show",  
279
            marginRight: "show" 
280
        }, speed, callback); 
281
    } 
282
})(jQuery);