-
Notifications
You must be signed in to change notification settings - Fork 42
/
directorySlider.js
112 lines (95 loc) · 3.08 KB
/
directorySlider.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
/**
* directorySlider v0.9 - Loads all images in a specified directory and creates a slide show
* Author: Justin W. Hall
* http://www.justinwhall.com/directory-jquery-slider/
*
* License: GPL v3
*/
(function($){
var directorySlider = function(element, options)
{
var elem = $(element),
obj = this,
elemId = elem[0].id;
// Merge config settings
var config = $.extend({
animation: 'slide',
filebase: 'slide_',
extension: 'jpg',
speed: 1000,
timeout: 4000,
directory: null,
numslides: null,
height: null,
width: null
}, options || {});
// set slideshow dimensions if set
if (config.height) {
$(elem).css('height', config.height);
}
if (config.width) {
$(elem).css('width', config.width);
}
$(elem).css('overflow', 'hidden');
// Get slides
var slides = [],
slideNumber = 1;
while(slideNumber <= config.numslides){
slides.push('<img src="' + config.directory + config.filebase + slideNumber + '.' + config.extension + '" />');
slideNumber++;
}
// append slideshow
// apply slide wrap 1st
var slideWrap = $('<div class="' + elemId + '-slide-wrap" ></div>');
slideWrap.appendTo(elem);
// append slide and position absolutley
$.each(slides, function(index, val) {
$(val).css({
position: 'absolute',
top: 0,
left: 0,
width: config.width // ADDED THIS SO WE DON'T NEED TO HAVE ALL IMAGES WITH SAME HEIGHT & WIDTH
}).appendTo(slideWrap);
});
setInterval(function(){
var firstSlide = elem.find('img:first-child'),
lastSlide = elem.find('img:last-child');
// Apply animation
switch(config.animation){
case 'fade':
$(lastSlide).animate({
opacity: 0},
config.speed, function() {
$(this).insertBefore(firstSlide).css('opacity', 1);
});
break;
case 'uncover':
lastSlide.animate({
marginLeft: -$(this).width()},
config.speed, function() {
$(this).insertBefore(firstSlide).css('marginLeft', 0);
});
break;
default:
$(lastSlide).animate({
opacity: 0},
config.speed, function() {
$(this).insertBefore(firstSlide).css('opacity', 1);
});
}
}, config.timeout);
};
$.fn.directorySlider = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('directoryslider')) return;
// pass options to plugin constructor
var directoryslider = new directorySlider(this, options);
// Store plugin object in this element's data
element.data('directoryslider', directoryslider);
});
};
})(jQuery);