-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazyslider.js
More file actions
231 lines (192 loc) · 6.32 KB
/
lazyslider.js
File metadata and controls
231 lines (192 loc) · 6.32 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
221
222
223
224
225
226
227
228
229
230
231
/*
* Lazy Slider v0.1
* https://github.com/beatnbite/lazyslider
*
* Copyright 2014, Vyacheslav Petrov
*
* Free for non-commercial use.
* A license fee is required for commercial use.
* Please refer to LICENSE file for details.
*/
(function($) {
/**
* Instantiates and runs the lazy slider on the element matching the specified selector.
*
* @param selector jQuery selector to match the slider element on the page.
* @param options Slider options.
*
* @constructor
*/
var LazySlider = function(selector, options) {
/**
* Slider settings.
*
* @type object
*/
var settings = {
animationSpeed: 700,
animationDelay: 5000,
easingOut: 'swing',
easingIn: 'swing',
captionAnimationSpeed: 250,
captionAnimationDelay: 600,
randomStart: true
};
$.extend(settings, options);
/**
* The slider element.
*
* @type jQuery
*/
var slider = $(selector);
/**
* Information on slides.
*
* @type Array
*/
var slides = [];
/**
* The total number of slides.
*
* @type number
*/
var total = 0;
/**
* Index of the current slide.
*
* @type number
*/
var currentIndex = 0;
/**
* The current slide element.
*
* @type jQuery
*/
var current;
/**
* Start the animation.
*/
this.run = function () {
// Init the slider
init();
// Start the caption animation
var caption = $('.lazyslider-caption', slider);
var panes = $('.lazyslider-panes', slider);
panes.hover(function () {
clearTimeout(panes.t);
caption.stop().animate({bottom: 0}, settings.captionAnimationSpeed);
}, function () {
panes.t = setTimeout((function () {
var height = $('.lazyslider-description', caption).outerHeight();
caption.stop().animate({bottom: -height}, settings.captionAnimationSpeed);
}), settings.captionAnimationDelay);
});
// Update the caption with the first slide description
updateCaption();
// Show the first slide and run the animation
schedule();
};
/**
* Init the slider.
*/
var init = function () {
// Init the slider element
slider.append(
'<div class="lazyslider-panes">'
+ '<div class="lazyslider-first-pane lazyslider-pane"></div>'
+ '<div class="lazyslider-second-pane lazyslider-pane"></div>'
+ '<div class="lazyslider-caption"><div class="lazyslider-background"></div><div class="lazyslider-description"></div></div>'
+ '</div>'
);
slider.addClass('lazyslider');
// Read information on slides
$('.slide', slider).each(function (index) {
var slide = $(this);
var link = slide.data('link');
slides[index] = {
url: slide.data('src'),
width: slide.data('width'),
height: slide.data('height'),
alt: $.trim(slide.text()),
slide: slide
};
if (link) {
slides[index].link = link;
}
total++;
});
if (settings.randomStart) {
currentIndex = Math.floor(Math.random() * total)
}
};
/**
* Update the caption with the description of the current slide.
*/
var updateCaption = function () {
$('.lazyslider-description', slider).text(slides[currentIndex].alt);
};
/**
* Prepare slides for the next animation step and schedule the animation.
*/
var schedule = function () {
prepare();
setTimeout(animate, settings.animationDelay);
};
/**
* Prepares slides for the next animation step.
*/
var prepare = function () {
$('.lazyslider-first-pane a, .lazyslider-first-pane img', slider).remove();
$('.lazyslider-first-pane', slider).append(renderSlide(currentIndex));
$('.lazyslider-first-pane', slider).show();
currentIndex++;
if (currentIndex >= total) {
currentIndex = 0;
}
$('.lazyslider-second-pane', slider).hide();
$('.lazyslider-second-pane a, .lazyslider-second-pane img', slider).remove();
$('.lazyslider-second-pane', slider).append(renderSlide(currentIndex));
};
/**
* Start the next animation step.
*/
var animate = function () {
updateCaption();
$('.lazyslider-first-pane', slider).fadeOut({duration: settings.animationSpeed, easing: settings.easingOut});
$('.lazyslider-second-pane', slider).fadeIn({duration: settings.animationSpeed, easing: settings.easingIn, complete: schedule});
};
/**
* Render a slide.
*
* @param index Index of the slide to render.
*
* @returns string Slide HTML.
*/
var renderSlide = function (index) {
var banner = slides[index];
var imageHtml = '<img src="'
+ banner.url
+ '" width="'
+ banner.width
+ '" height="'
+ banner.height
+ '" alt="'
+ banner.alt
+ '" />';
return ('link' in banner) ? '<a href="' + banner.link + '">' + imageHtml + '</a>' : imageHtml;
};
};
/**
* Prototype jQuery and add the lazySlider method.
*
* @param options Slider settings
*
* @returns jQuery
*/
$.fn.lazySlider = function(options) {
return this.each(function(key, value){
var slider = new LazySlider(this, options);
slider.run();
});
};
})(jQuery);