-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslider.js
More file actions
220 lines (181 loc) · 6.75 KB
/
slider.js
File metadata and controls
220 lines (181 loc) · 6.75 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
slider
---------------------------------------------------------------------
Author: Lane Olson
Sources: jQuery Plugin Patterns:
http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
Version: 1.0
Date: April 30, 2012
Description: Displays nested lists of links as a sliding menu
---------------------------------------------------------------------
**/
;(function ($) {
// initialize namespace if it doesn't exist
if (!$.responsive) {
$.responsive = {};
};
$.responsive.slider = function ( el, options ) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data( "responsive.slider" , base );
var currentNav = $('ul:first', base.el);
var transitionsSupported = false;
var isCreated = false;
var isAnimating = false;
base.init = function () {
base.options = $.extend({}, $.responsive.slider.defaultOptions, options);
base.$el.bind("createslider", function() {
base.create();
});
base.$el.bind("destroyslider", function() {
base.destroy();
});
transitionsSupported = base.transitionSupport();
if(base.options.initPlugin)
base.create();
};
// item - list item containing the sub menu
base.goToNext = function(item) {
var currUl = currentNav;
var nextUl = item.siblings("ul:first");
var divContainer = false;
if(!isAnimating)
{
isAnimating = true;
if(nextUl.length < 1)
{
divContainer = item.siblings("div");
nextUl = $("ul:first", divContainer);
}
nextUl.prepend('<li><a class="'+base.options.classPrefix+'back'+'" href="#">'+base.options.backWording+' '+item.html()+'</a></li>');
$('a.'+base.options.classPrefix+'back'+' strong', nextUl).html(base.options.prevArrow);
if(!transitionsSupported)
{
nextUl.css("left", nextUl.position().left+"px");
nextUl.animate({ left: "0" }, base.options.transitionTime, "linear", function()
{
base.switchClassesNext(currUl, nextUl);
isAnimating = false;
});
} else {
base.switchClassesNext(currUl, nextUl);
isAnimating = false;
}
currentNav = nextUl;
}
};
base.goToParent = function() {
var currUl = currentNav;
var prevUl = currUl.parents("ul:first"); // get first parent <ul>
var moveLeft; // value to animate left to
if(!isAnimating) {
isAnimating = true;
if(!transitionsSupported) {
if(prevUl.position().left < 0)
moveLeft = "0";
else
moveLeft = "100%";
prevUl.css("left", prevUl.position().left+"px");
prevUl.animate({ left: moveLeft }, base.options.transitionTime, "linear", function()
{
base.switchClassesPrevious(currUl, prevUl);
isAnimating = false;
});
} else {
base.switchClassesPrevious(currUl, prevUl);
isAnimating = false;
}
currentNav = prevUl;
}
};
base.switchClassesNext = function(current, next) {
current.removeClass(base.options.classPrefix+'current');
current.addClass(base.options.classPrefix+'left');
next.addClass(base.options.classPrefix+'current');
next.removeClass(base.options.classPrefix+'right');
base.$el.height(next.height());
next.css("left", "");
};
base.switchClassesPrevious = function(current, previous) {
current.removeClass(base.options.classPrefix+'current');
current.addClass(base.options.classPrefix+'right');
previous.addClass(base.options.classPrefix+'current').css("left", "");
previous.removeClass(base.options.classPrefix+'left').css("left", "");
current.find("li:first").remove();
base.$el.height(previous.height());
};
base.transitionSupport = function() {
var d = document.createElement("detect"),
CSSprefix = "Webkit,Moz,O,ms,Khtml".split(","),
All = ("transition " + CSSprefix.join("Transition,") + "Transition").split(",");
for (var n = 0, np = All.length; n < np; n++) {
if (d.style[All[n]] === "") {
return true;
}
}
return false;
};
base.create = function(){
if(!isCreated) {
base.$el.css('overflow', 'hidden');
$('ul', base.el).css({
'position': 'absolute',
'width': '100%',
'top': '0px'
});
base.$el.find("ul:first").addClass(base.options.classPrefix+'current');
base.$el.css({
height: base.$el.find("ul:first").height()
});
$("li", base.el).each(function() {
if($(this).children("ul").length > 0 || $(this).children('div').children('ul').length > 0)
{
$(this).children("ul","div").addClass(base.options.classPrefix+'right');
$(this).children("a").append('<strong>'+base.options.moreArrow+'</strong>').addClass(base.options.classPrefix+'more');
}
});
base.$el.on("click", "a."+base.options.classPrefix+'more', function() {
base.goToNext($(this));
return false;
});
base.$el.on("click", "a."+base.options.classPrefix+'back', function () {
base.goToParent();
return false;
});
isCreated = true;
}
};
base.destroy = function() {
base.$el.removeAttr('style');
base.$el.off("click", "**");
$('.'+base.options.classPrefix+'current', base.el).removeClass(base.options.classPrefix+'current');
$('.'+base.options.classPrefix+'right', base.el).removeClass(base.options.classPrefix+'right');
$('.'+base.options.classPrefix+'left', base.el).removeClass(base.options.classPrefix+'left');
$('.'+base.options.classPrefix+'more'+' strong', base.el).remove();
$('.'+base.options.classPrefix+'more', base.el).removeClass(base.options.classPrefix+'more');
$('ul', base.el).removeAttr('style');
$('.'+base.options.classPrefix+'back').parent().remove();
isCreated = false;
};
base.init();
};
$.responsive.slider.defaultOptions = {
classPrefix: 'slider-',
transitionTime: 200,
backWording: "Back to",
moreArrow: "▶",
prevArrow: "◀",
initPlugin: false
// TODO: Add option for max levels
};
$.fn.slider = function( options ) {
return this.each(function () {
(new $.responsive.slider(this, options));
});
};
})( jQuery );