-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
unslider.js
141 lines (109 loc) · 3.57 KB
/
unslider.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Unslider 0.1
* by @visualidiot
*
* Licensed under the WTFPL, as always.
*
**
* The quickest way to set it up is like this.
**
*
* HTML: <div id="slider">
* <ul>
* <li></li>
* <li></li>
* <li></li> <!-- you get the idea. -->
* </ul>
* </div>
*
* CSS: Provided in unslider.css
*
* JS: $('#slider').unslider();
*/
(function($, d) {
/**
* Move the Unslider
*/
$.fn.moveUnslider = function(pos, speed, callback) {
return this.stop().animate({marginLeft: parseFloat(pos)}, speed, callback);
};
$.fn.unslider = function(options) {
// Set the options
var o = $.extend({
activeClass: 'active',
arrows: true, // If false, just autoplay
// Speeds + timing
speed: 250,
delay: 2000,
// Callbacks
afterSlide: function() {}
}, options);
// And loop every instance of the Unslider
return this.each(function() {
var me = $(this).addClass('unslider'),
list = me.children('ul'),
items = list.children('li'),
first = items.filter(':first'),
itemCount = items.length + 2, // Don't forget our clones!
width = me.width(),
height = first.height(),
go = function() {
var me = $(this),
dir = me.attr('class').split(' ')[1],
current = items.filter('.' + o.activeClass),
margin = parseFloat(list.css('margin-left')),
actions = {
previous: function() {
var first = current.prev().hasClass('cloned'),
prev = first ? items.eq(-1) : current.prev();
prev.addClass(o.activeClass).siblings().removeClass(o.activeClass);
if(!first) {
list.moveUnslider(margin + width, o.speed, o.afterSlide);
} else {
list.moveUnslider(-(width * (itemCount - 3)), o.speed, o.afterSlide);
}
},
next: function() {
var last = current.next().hasClass('cloned'),
next = last ? items.eq(0) : current.next();
next.addClass(o.activeClass).siblings().removeClass(o.activeClass);
if(last) {
setTimeout(function() {
list.moveUnslider(-width, 0);
}, o.speed);
}
return list.moveUnslider(margin - width, o.speed, o.afterSlide);
}
};
// Run the action, based on the class of the link. Genius.
if(actions[dir]) {
actions[dir]();
}
};
// Check we have two or more items (the itemCount adds two)
if(itemCount > 4) {
// Append the first and last items
first.addClass(o.activeClass).clone().attr('class', 'cloned').appendTo(list);
items.filter(':last').clone().addClass('cloned').prependTo(list);
// Set the width to stop wrapping, and since we have a clone, position it offscreen
list.css({width: width * itemCount, marginLeft: -width});
// Get the arrows, if they want 'em.
if(o.arrows) {
$('<p class="unslider-arrows"><span class="arrow previous" /><span class="arrow next" /></p>').appendTo(me.parent()).find('.arrow').each(function() {
var me = $(this), dir = me.attr('class').split(' ')[1],
msg = 'Click to show the ? slide'.replace('?', dir),
arrows = {previous: '«', next: '»'};
me.attr('title', msg).html(arrows[dir]);
}).click(go);
$(d).keyup(function(e) {
var key = e.which,
keys = {37: 'previous', 39: 'next'};
if(keys[key]) {
$('.unslider-arrows .' + keys[key]).click();
}
});
}
}
});
};
})(jQuery, document);