-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.hoverscroll.js
488 lines (411 loc) · 14.7 KB
/
jquery.hoverscroll.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* HoverScroll jQuery Plugin
*
* Make an unordered list scrollable by hovering the mouse over it
*
* @author RasCarlito <carl.ogren@gmail.com>
* @version 0.2.5
* @revision 21
*
*
*
* FREE BEER LICENSE VERSION 1.02
*
* The free beer license is a license to give free software to you and free
* beer (in)to the author(s).
*
*
* Released: 03-08-2012 02:30am
*
* Changelog
* ----------------------------------------------------
*
* 0.2.5 - Added 'create' callback that is executed once for each initialized hoverscroll element
*
* 0.2.4 - Added "Right to Left" option, only works with horizontal lists
* - Optimization of arrows opacity control (Thanks to Josef Körner)
*
* 0.2.3 - Added fixed arrows option
* - Binded startMoving and stopMoving functions to the HoverScrolls HTML object for external access
*
* 0.2.2 Bug fixes
* - Backward compatibility with jQuery 1.1.x
* - Added test file to the archive
* - Bug fix: The right arrow appeared when it wasn't necessary (thanks to <admin at unix dot am>)
*
* 0.2.1 Bug fixes
* - Backward compatibility with jQuery 1.2.x (thanks to Andy Mull for compatibility with jQuery >= 1.2.4)
* - Added information to the debug log
*
* 0.2.0 Added some new features
* - Direction indicator arrows
* - Permanent override of default parameters
*
* 0.1.1 Minor bug fix
* - Hover zones did not cover the complete container
*
* note: The css file has not changed therefore it is still versioned 0.1.0
*
* 0.1.0 First release of the plugin. Supports:
* - Horizontal and vertical lists
* - Width and height control
* - Debug log (doesn't show useful information for the moment)
*/
(function($) {
/**
* @method hoverscroll
* @param params {Object} Parameter list
* params = {
* vertical {Boolean}, // Vertical list or not ?
* width {Integer}, // Width of list container
* height {Integer}, // Height of list container
* arrows {Boolean}, // Show direction indicators or not
* arrowsOpacity {Float}, // Arrows maximum opacity
* fixedArrows {Boolean}, // Display arrows permenantly, this overrides arrowsOpacity option
* debug {Boolean} // Debug output in firebug console
* };
*/
$.fn.hoverscroll = function(params) {
if (!params) { params = {}; }
// Extend default parameters
// note: empty object to prevent params object from overriding default params object
params = $.extend({}, $.fn.hoverscroll.params, params);
// Loop through all the elements
this.each(function() {
var $this = $(this);
// prevent hoverscroll from being initialized several times
if (this.__hsInitDone) {
$.log('[HoverScroll] Init prevented because element is already a hoverscroll');
return;
}
if (params.debug) {
$.log('[HoverScroll] Trying to create hoverscroll on element ' + this.tagName + '#' + this.id);
}
// wrap ul list with a div.listcontainer
if (params.fixedArrows) {
$this.wrap('<div class="fixed-listcontainer"></div>')
}
else {
$this.wrap('<div class="listcontainer"></div>');
}
$this.addClass('list');
//.addClass('ui-helper-clearfix');
// store handle to listcontainer
var listctnr = $this.parent();
// wrap listcontainer with a div.hoverscroll
listctnr.wrap('<div class="ui-widget-content hoverscroll' +
(params.rtl && !params.vertical ? " rtl" : "") + '"></div>');
//listctnr.wrap('<div class="hoverscroll"></div>');
// store hoverscroll container
var ctnr = listctnr.parent();
var leftArrow, rightArrow, topArrow, bottomArrow;
// Add arrow containers
if (params.arrows) {
if (!params.vertical) {
if (params.fixedArrows) {
leftArrow = '<div class="fixed-arrow left"></div>';
rightArrow = '<div class="fixed-arrow right"></div>';
listctnr.before(leftArrow).after(rightArrow);
}
else {
leftArrow = '<div class="arrow left"></div>';
rightArrow = '<div class="arrow right"></div>';
listctnr.append(leftArrow).append(rightArrow);
}
}
else {
if (params.fixedArrows) {
topArrow = '<div class="fixed-arrow top"></div>';
bottomArrow = '<div class="fixed-arrow bottom"></div>';
listctnr.before(topArrow).after(bottomArrow);
}
else {
topArrow = '<div class="arrow top"></div>';
bottomArrow = '<div class="arrow bottom"></div>';
listctnr.append(topArrow).append(bottomArrow);
}
}
}
// Apply parameters width and height
ctnr.width(params.width).height(params.height);
if (params.arrows && params.fixedArrows) {
if (params.vertical) {
topArrow = listctnr.prev();
bottomArrow = listctnr.next();
listctnr.width(params.width)
.height(params.height - (topArrow.height() + bottomArrow.height()));
}
else {
leftArrow = listctnr.prev();
rightArrow = listctnr.next();
listctnr.height(params.height)
.width(params.width - (leftArrow.width() + rightArrow.width()));
}
}
else {
listctnr.width(params.width).height(params.height);
}
var size = 0;
if (!params.vertical) {
ctnr.addClass('horizontal');
// Determine content width
$this.children().each(function() {
$(this).addClass('item');
if ($(this).outerWidth) {
size += $(this).outerWidth(true);
}
else {
// jQuery < 1.2.x backward compatibility patch
size += $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'))
+ parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
}
});
// Apply computed width to listcontainer
$this.width(size);
if (params.debug) {
$.log('[HoverScroll] Computed content width : ' + size + 'px');
}
// Retrieve container width instead of using the given params.width to include padding
if (ctnr.outerWidth) {
size = ctnr.outerWidth();
}
else {
// jQuery < 1.2.x backward compatibility patch
size = ctnr.width() + parseInt(ctnr.css('padding-left')) + parseInt(ctnr.css('padding-right'))
+ parseInt(ctnr.css('margin-left')) + parseInt(ctnr.css('margin-right'));
}
if (params.debug) {
$.log('[HoverScroll] Computed container width : ' + size + 'px');
}
}
else {
ctnr.addClass('vertical');
// Determine content height
$this.children().each(function() {
$(this).addClass('item')
if ($(this).outerHeight) {
size += $(this).outerHeight(true);
}
else {
// jQuery < 1.2.x backward compatibility patch
size += $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'))
+ parseInt($(this).css('margin-bottom')) + parseInt($(this).css('margin-bottom'));
}
});
// Apply computed height to listcontainer
$this.height(size);
if (params.debug) {
$.log('[HoverScroll] Computed content height : ' + size + 'px');
}
// Retrieve container height instead of using the given params.height to include padding
if (ctnr.outerHeight) {
size = ctnr.outerHeight();
}
else {
// jQuery < 1.2.x backward compatibility patch
size = ctnr.height() + parseInt(ctnr.css('padding-top')) + parseInt(ctnr.css('padding-bottom'))
+ parseInt(ctnr.css('margin-top')) + parseInt(ctnr.css('margin-bottom'));
}
if (params.debug) {
$.log('[HoverScroll] Computed container height : ' + size + 'px');
}
}
// Define hover zones on container
var zone = {
1: {action: 'move', from: 0, to: 0.06 * size, direction: -1 , speed: 16},
2: {action: 'move', from: 0.06 * size, to: 0.15 * size, direction: -1 , speed: 8},
3: {action: 'move', from: 0.15 * size, to: 0.25 * size, direction: -1 , speed: 4},
4: {action: 'move', from: 0.25 * size, to: 0.4 * size, direction: -1 , speed: 2},
5: {action: 'stop', from: 0.4 * size, to: 0.6 * size},
6: {action: 'move', from: 0.6 * size, to: 0.75 * size, direction: 1 , speed: 2},
7: {action: 'move', from: 0.75 * size, to: 0.85 * size, direction: 1 , speed: 4},
8: {action: 'move', from: 0.85 * size, to: 0.94 * size, direction: 1 , speed: 8},
9: {action: 'move', from: 0.94 * size, to: size, direction: 1 , speed: 16}
}
// Store default state values in container
ctnr[0].isChanging = false;
ctnr[0].direction = 0;
ctnr[0].speed = 1;
/**
* Check mouse position relative to hoverscroll container
* and trigger actions according to the zone table
*
* @param x {Integer} Mouse X event position
* @param y {Integer} Mouse Y event position
*/
function checkMouse(x, y) {
x = x - ctnr.offset().left;
y = y - ctnr.offset().top;
var pos;
if (!params.vertical) {pos = x;}
else {pos = y;}
for (i in zone) {
if (pos >= zone[i].from && pos < zone[i].to) {
if (zone[i].action == 'move') {startMoving(zone[i].direction, zone[i].speed);}
else {stopMoving();}
}
}
}
/**
* Sets the opacity of the left|top and right|bottom
* arrows according to the scroll position.
*/
function setArrowOpacity() {
if (!params.arrows || params.fixedArrows) {return;}
var maxScroll;
var scroll;
if (!params.vertical) {
maxScroll = listctnr[0].scrollWidth - listctnr.width();
scroll = listctnr[0].scrollLeft;
}
else {
maxScroll = listctnr[0].scrollHeight - listctnr.height();
scroll = listctnr[0].scrollTop;
}
var limit = params.arrowsOpacity;
// Optimization of opacity control by Josef Körner
// Initialize opacity; keep it between its extremas (0 and limit) we don't need to check limits after init
var opacity = (scroll / maxScroll) * limit;
if (opacity > limit) { opacity = limit; }
if (isNaN(opacity)) { opacity = 0; }
// Check if the arrows are needed
// Thanks to <admin at unix dot am> for fixing the bug that displayed the right arrow when it was not needed
var done = false;
if (opacity <= 0) {
$('div.arrow.left, div.arrow.top', ctnr).hide();
if(maxScroll > 0) {
$('div.arrow.right, div.arrow.bottom', ctnr).show().css('opacity', limit);
}
done = true;
}
if (opacity >= limit || maxScroll <= 0) {
$('div.arrow.right, div.arrow.bottom', ctnr).hide();
done = true;
}
if (!done) {
$('div.arrow.left, div.arrow.top', ctnr).show().css('opacity', opacity);
$('div.arrow.right, div.arrow.bottom', ctnr).show().css('opacity', (limit - opacity));
}
// End of optimization
}
/**
* Start scrolling the list with a given speed and direction
*
* @param direction {Integer} Direction of the displacement, either -1|1
* @param speed {Integer} Speed of the displacement (20 being very fast)
*/
function startMoving(direction, speed) {
if (ctnr[0].direction != direction) {
if (params.debug) {
$.log('[HoverScroll] Starting to move. direction: ' + direction + ', speed: ' + speed);
}
stopMoving();
ctnr[0].direction = direction;
ctnr[0].isChanging = true;
move();
}
if (ctnr[0].speed != speed) {
if (params.debug) {
$.log('[HoverScroll] Changed speed: ' + speed);
}
ctnr[0].speed = speed;
}
}
/**
* Stop scrolling the list
*/
function stopMoving() {
if (ctnr[0].isChanging) {
if (params.debug) {
$.log('[HoverScroll] Stoped moving');
}
ctnr[0].isChanging = false;
ctnr[0].direction = 0;
ctnr[0].speed = 1;
clearTimeout(ctnr[0].timer);
}
}
/**
* Move the list one step in the given direction and speed
*/
function move() {
if (ctnr[0].isChanging == false) {return;}
setArrowOpacity();
var scrollSide;
if (!params.vertical) {scrollSide = 'scrollLeft';}
else {scrollSide = 'scrollTop';}
listctnr[0][scrollSide] += ctnr[0].direction * ctnr[0].speed;
ctnr[0].timer = setTimeout(function() {move();}, 50);
}
// Initialize "right to left" option if specified
if (params.rtl && !params.vertical) {
listctnr[0].scrollLeft = listctnr[0].scrollWidth - listctnr.width();
}
// Bind actions to the hoverscroll container
ctnr
// Bind checkMouse to the mousemove
.mousemove(function(e) {checkMouse(e.pageX, e.pageY);})
// Bind stopMoving to the mouseleave
// jQuery 1.2.x backward compatibility, thanks to Andy Mull!
// replaced .mouseleave(...) with .bind('mouseleave', ...)
.bind('mouseleave', function() {stopMoving();});
// Bind the startMoving and stopMoving functions
// to the HTML object for external access
this.startMoving = startMoving;
this.stopMoving = stopMoving;
if (params.arrows && !params.fixedArrows) {
// Initialise arrow opacity
setArrowOpacity();
}
else {
// Hide arrows
$('.arrowleft, .arrowright, .arrowtop, .arrowbottom', ctnr).hide();
}
this.__hsInitDone = true;
if ($.isFunction(params.create)) {
params.create.call(this);
}
});
return this;
};
// Backward compatibility with jQuery 1.1.x
if (!$.fn.offset) {
$.fn.offset = function() {
this.left = this.top = 0;
if (this[0] && this[0].offsetParent) {
var obj = this[0];
do {
this.left += obj.offsetLeft;
this.top += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return this;
}
}
/**
* HoverScroll default parameters
*/
$.fn.hoverscroll.params = {
vertical: false, // Display the list vertically or not
width: 400, // Width of the list
height: 50, // Height of the list
arrows: true, // Display arrows to the left and top or the top and bottom
arrowsOpacity: 0.7, // Maximum opacity of the arrows if fixedArrows
fixedArrows: false, // Fix the displayed arrows to the side of the list
rtl: false, // Set display mode to "Right to Left"
debug: false, // Display some debugging information in firebug console
create: function() {}
};
/**
* Log errors to consoles (firebug, opera) if exist, else uses alert()
*/
$.log = function() {
try {console.log.apply(console, arguments);}
catch (e) {
try {opera.postError.apply(opera, arguments);}
catch (e) {
// alert(Array.prototype.join.call(arguments, " "));
}
}
};
})(jQuery);