forked from mattmcmanus/jquery.fadeAble
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.fadeAble.js
executable file
·103 lines (88 loc) · 3.04 KB
/
jquery.fadeAble.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
;(function($) {
$.fn.fadeAble = function(options) {
var defaults = {
prevId: 'prevButton',
prevText: 'Previous',
nextId: 'nextButton',
nextText: 'Next',
controlsShow: true,
controlsFade: true,
speed: 800,
autoPlay: false,
pause: 2000,
loop: false,
images: [],
current: 0
};
// Set the options.
var options = $.extend(defaults, options);
// Go through the matched elements and return the jQuery object.
this.each(function(){
var container = this;
var defaultLink = $(container).children('a');
defaultLink.addClass("fadeAble")
var defaultImage = $(defaultLink).children('img');
// Let's make sure things don't start jumping around all crazy like
$(container).css({
position: 'relative',
height: $(defaultImage).height(),
width: $(defaultImage).width()
});
// Add the additional images inside your container
$(options.images).each(function(){
$(container).append('<a href="'+this.href+'" class="fadeAble" style="display:none" title="'+this.title+'"><img src="'+this.src+'" /></a>');
});
// Make sure things line up nicely
$(container).children('a').css({
position: 'absolute',
top: 0,
left: 0
});
//prepend $(img:first).src to array
options.images.unshift({
"src": $(defaultImage).attr('src'),
"href": $(defaultLink).attr('src'),
"title": $(defaultLink).attr('title')
});
if (options.controlsShow) {
$(this).append(
'<span class="button" style="z-index:2" id="'+options.prevId+'">' +
'<a href=\"javascript:void(0);\">'+options.prevText +'</a></span>' +
'<span class="button" style="z-index:2" id="'+options.nextId+'">' +
'<a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>'
);
$("#"+options.nextId).click(function(){ fade("next", container); });
$("#"+options.prevId).click(function(){ fade("prev", container); });
}
if (options.loop) {
options.timeout = setTimeout(function(){
fade("next", container);
}, options.pause);
}
});
function fade(direction, container) {
if (options.timeout) {
clearTimeout(options.timeout);
}
//Figure out what the next index should be
var nextIndex;
if (direction == 'prev') {
nextIndex = ( options.current == 0 ) ? options.images.length-1 : options.current-1;
} else {
nextIndex = ( options.current == options.images.length-1 ) ? 0 : options.current+1;
}
// Animate the transition
var imgs = $(container).children('a.fadeAble');
imgs.hide();
imgs.eq(options.current).show().css('z-index', '0');
imgs.eq(nextIndex).css('z-index', '1').fadeIn(options.speed);
options.current = nextIndex;
// Reset the time
if (options.loop) {
options.timeout = setTimeout(function(){
fade("next", container);
}, options.pause);
}
}
};
})(jQuery);