-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathslider.js
184 lines (149 loc) · 7.07 KB
/
slider.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
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
(function ($) {
'use strict';
$.fn.fullWidth = function (options) {
var defaults = {
maxHeight : 450,
minHeight : 375,
delay : 5000,
transition : 1000,
maxFont : 36,
minFont : 20
}, settings = $.extend(defaults, options),
cssTrans = (function() {
var s = document.createElement('p').style;
return 'transition' in s ||
'WebkitTransition' in s ||
'MozTransition' in s ||
'msTransition' in s ||
'OTransition' in s;
}());
return this.each( function() {
var $full = $(this),
$inner = $full.find('.inner'),
$slides = $inner.find('.slide'),
$images = $slides.find('img'),
$nav = $full.find('.slide-nav'),
$controls = $full.find('.controls a'),
$navCircles = '',
smallest = 9999,
status = {current : 0, previous : 0, max : $slides.length - 1},
timers = {slides : '', resize : ''},
move = function(direction, current){
if($inner.is(':animated')) return;
stop();
status.previous = status.current;
if(direction === 'right'){
status.current = status.current + 1 > status.max ? 0 : status.current+1;
}else if(direction === 'left'){
status.current = status.current - 1 < 0 ? status.max : status.current-1;
}else{
status.current = current || 0;
}
$navCircles.removeClass('current').eq(status.current).addClass('current');
$full.trigger( 'fws.start', { 'status' : status, 'direction' : direction } );
if(cssTrans){
$inner.css({ 'margin-left' : '-' + 100 * status.current + '%' });
setTimeout(start, settings.transition);
}else{
$inner.animate({ 'margin-left' : '-' + 100 * status.current + '%' }, settings.transition, start );
}
},
start = function(){
$full.trigger( 'fws.finish', { 'status' : status } );
timers.slides = setTimeout(function(){ move('right'); }, settings.delay);
},
stop = function(){
clearTimeout(timers.slides);
},
resize = function(){
var $wWidth = $(window).width(),
newHeight = parseInt($wWidth/3, 10),
imageCSS = $wWidth <= smallest ? ['100%', 'auto', '9999'] : ['', '', ''],
start = $inner.height(),
divCSS = $wWidth <= 480 ? ['0', '100%', 'none'] : ['', '', ''],
size = $wWidth/41;
/*
size equals window width divided by 41. 41 was chosen as it creates a nicely proportionate font size.
However, you can experiment with this if you want a generally larger or smaller size to be set.
For example: a width of 960px/41 = 23.4px, with 20 instead of 41 you'd get a font size of 48px.
It really depends on what kind of content you're displaying and how much text you've got on each slide.
*/
size = size > settings.maxFont ? settings.maxFont :
(size < settings.minFont ? settings.minFont : size);
$inner.css('height', function(){
return newHeight > settings.maxHeight ? settings.maxHeight :
(newHeight < settings.minHeight ? settings.minHeight : newHeight);
});
$images.css({
'margin-top' : function(){
var curr = $(this).height();
return '-' + (start > curr ? 0 : curr-start) / 2 + 'px';
},
'height' : imageCSS[0],
'width' : imageCSS[1],
'maxWidth' : imageCSS[2]
});
$slides.find('div').css({
'font-size' : size,
'top' : function(){
var diff = start - $(this).height();
return $wWidth <= 480 ? diff : diff/2;
},
'padding' : divCSS[0],
'width' : divCSS[1]
}).find('br').css('display', divCSS[2]);
},
attachEvents = function(){
$(window).resize(function(){
clearTimeout(timers.resize);
timers.resize = setTimeout(resize, 100);
}).trigger('resize');
$controls.on('click', function(){
move(this.className);
return false;
});
$full.on('mouseenter mouseleave', function(e){
if( $controls.is(':animated') ) return;
if( e.type === 'mouseenter' ){
$controls.fadeIn();
}else{
$controls.fadeOut();
}
});
$navCircles.on('click', function(){
move('direct', $(this).index());
});
$(document).on('keydown', function(e){
if(!(e.which === 37 || e.which === 39)) return;
move(e.which === 37 ? 'left' : 'right');
});
};
(function(){
$images.each(function(){
var w = $(this).attr('width');
smallest = w < smallest ? w : smallest;
});
$slides.css('width', parseFloat(100/$slides.length, 10)+'%')
.each(function(i){
$(this).addClass('slide-'+(i+1));
$nav.append('<span>•</span>');
}).find('div').wrapInner('<p />');
$inner.css({
height: settings.minHeight,
transitionDuration: settings.transition+'ms',
width: ($slides.length*100)+'%'
});
$navCircles = $nav.find('span');
$navCircles.first().addClass('current');
$nav.css('width', function(){
return $navCircles.length*26;
});
$(window).load(function() {
attachEvents();
$full.trigger( 'fws.start', { 'status' : status, 'direction' : 'direct' } );
$inner.fadeTo(1000, 1, start);
});
}());
});
};
}(jQuery));