forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow.js
97 lines (66 loc) · 2.22 KB
/
slideshow.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
/*
Move the #slideshow element to the top of #main.
Write code to cycle through the list items inside the element;
fade one in, display it for a few seconds, then fade it out and
fade in the next one.
When you get to the end of the list, start again at the beginning.
Add a counter under the slideshow (2 of 3, etc.) $.fn.prevAll
Add buttons under the counter that let you take manual control
of the slideshow and stop the automatic animation.
*/
$('h1').html('new html').attr()
$(document).ready(function() {
var timeout, manualMode = false,
$slideshow = $('#slideshow').prependTo('#main'),
$counter = $('<div/>').insertAfter($slideshow),
$controls = $('<div/>').insertAfter($slideshow),
$prevBtn = $('<input/>', {
type : 'button',
value : 'previous'
}).appendTo($controls),
$nextBtn = $('<input/>', {
type : 'button',
value : 'next'
}).appendTo($controls),
$items = $slideshow.find('li').hide(),
total = $items.length,
updateCounter = function(num) {
$counter.text(num + ' of ' + total);
},
getItem = function($item, trav) {
var $returnItem = $item[trav]();
return $returnItem.length ?
$returnItem :
$items[(trav == 'next') ? 'first' : 'last']();
},
showItem = function($currentItem, $itemToShow) {
var $itemToShow =
$itemToShow || getItem($currentItem,'next');
$currentItem.fadeOut(500, function() {
$itemToShow.fadeIn(500, fadeCallback);
});
},
fadeCallback = function() {
if (manualMode) { return; }
var $this = $(this),
$next = getItem($this, 'next'),
num = $this.prevAll().length + 1;
// update the counter
updateCounter(num);
// set the timeout for showing
// the next item in 5 seconds
timeout = setTimeout(function() {
showItem($this, $next);
}, 5000);
},
handleBtnClick = function(e) {
clearTimeout(timeout);
manualMode = true;
var $currentItem = $items.filter(':visible'),
$itemToShow = getItem($currentItem, e.data.direction);
showItem($currentItem, $itemToShow);
};
$prevBtn.bind('click', { direction : 'prev' }, handleBtnClick);
$nextBtn.bind('click', { direction : 'next' }, handleBtnClick);
$items.eq(0).fadeIn(500, fadeCallback);
});