-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery-ui.hoverscroll.js
334 lines (276 loc) · 10.2 KB
/
jquery-ui.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
/**
* HoverScroll jQuery UI widget
*
* Make an unordered list scrollable by hovering the mouse over it.
*
* @author RasCarlito <[email protected]>
* @version 0.3a
* @revision 22
*
* 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).
*/
(function($, undefined) {
var createCount = 0;
$.widget("ui.hoverscroll", {
options: {
vertical: false, // Display the list vertically or not
width: 400, // Width of the list
height: 50, // Height of the list
maxSpeed: 20, // Maximum speed displacement
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"
refreshRate: 50, // Animation timer refresh rate
create: function(event, ui) {},
start: function(event, ui) {},
move: function(event, ui) {},
stop: function(event, ui) {},
resize: function(event, ui) {},
debug: false // Display some debugging information in firebug console (do not activate without firebug)
},
_create: function() {
var self = this, o = this.options;
createCount++;
if (o.debug) {
log("[HoverScroll] Creating hoverscroll on element " +
this.element[0].tagName + "#" + this.element[0].id);
}
// wrap ul list with a div.listcontainer
if (o.fixedArrows) {this.element.wrap('<div class="fixed-listcontainer"></div>');}
else {this.element.wrap('<div class="listcontainer"></div>');}
this.element.addClass("list");
// store handle to listcontainer
var listctnr = this.element.parent();
// wrap listcontainer with a div.hoverscroll
listctnr.wrap('<div class="ui-widget-content hoverscroll' +
(o.rtl && !o.vertical ? " rtl" : "") + '"></div>');
// store handle hoverscroll container
var ctnr = listctnr.parent();
// Add arrow containers
if (o.arrows) {
if (o.vertical) {
if (o.fixedArrows) {
listctnr.before('<div class="fixed-arrow top"></div>')
.after('<div class="fixed-arrow bottom"></div>');
}
else {
listctnr.append('<div class="arrow top"></div>')
.append('<div class="arrow bottom"></div>');
}
}
else {
if (o.fixedArrows) {
listctnr.before('<div class="fixed-arrow left"></div>')
.after('<div class="fixed-arrow right"></div>');
}
else {
listctnr.append('<div class="arrow left"></div>')
.append('<div class="arrow right"></div>');
}
}
}
// Apply width and height parameters
ctnr.width(o.width).height(o.height);
// Calculate and apply width and height parameters
// on the list container for fixed arrows
if (o.arrows && o.fixedArrows) {
if (o.vertical) {
listctnr.width(o.width)
.height(o.height - (listctnr.prev().height() + listctnr.next().height()));
}
else {
listctnr.height(o.height)
.width(o.width - (listctnr.prev().width() + listctnr.next().width()));
}
}
else {
listctnr.width(o.width).height(o.height);
}
var size = 0;
if (o.vertical) {
ctnr.addClass("vertical");
// Determine content height
this.element.children().each(function() {
$(this).addClass("item");
size += $(this).outerHeight(true);
});
// Apply computed height to listcontainer
this.element.height(size);
if (o.debug) {
log("[HoverScroll] Computed content height : " + size + "px");
}
// Retrieve container height instead of using the given params.height to include padding
size = ctnr.outerHeight();
if (o.debug) {
log('[HoverScroll] Computed container height : ' + size + 'px');
}
}
else {
ctnr.addClass('horizontal');
// Determine content width
this.element.children().each(function() {
$(this).addClass("item");
size += $(this).outerWidth(true);
});
// Apply computed width to listcontainer
this.element.width(size);
if (o.debug) {
log('[HoverScroll] Computed content width : ' + size + 'px');
}
// Retrieve container width instead of using the given params.width to include padding
size = ctnr.outerWidth();
if (o.debug) {
log('[HoverScroll] Computed container width : ' + size + 'px');
}
}
// Initialize "right to left" option if specified
if (o.rtl && !o.vertical) {
listctnr[0].scrollLeft = listctnr[0].scrollWidth - listctnr.width();
if (o.debug) {
log("[HoverScroll] Start position for Right to Left mode set to " + listctnr[0].scrollLeft + "px");
}
}
// Bind actions to the hoverscroll container
ctnr
// Bind checkMouse to the mousemove
.mousemove(function(e) {self._checkMouse(e.pageX, e.pageY);})
// Bind stopMoving to the mouseleave
.mouseleave(function() {self.stop();});
this.ctnr = ctnr;
this.listctnr = listctnr;
// Determine width:speed relationship constant
this._getSpeedConstant();
this.isMoving = false;
if (o.arrows && !o.fixedArrows) {
// Initialise arrow opacity
this._setArrowOpacity();
}
else {
// Hide arrows
$('.arrowleft, .arrowright, .arrowtop, .arrowbottom', ctnr).hide();
}
},
_getSpeedConstant: function() {
if (!this.options.vertical) {
this.speedConstant = (this.ctnr.width() / 2) / (this.ctnr.width() * Math.pow(this.options.maxSpeed, 1/3));
}
else {
this.speedConstant = (this.ctnr.height() / 2) / (this.ctnr.height() * Math.pow(this.options.maxSpeed, 1/3));
}
if (this.options.debug) {
log("[HoverScroll] Container size:Speed relationship set to " + this.speedConstant);
}
},
_checkMouse: function(x, y) {
var ctnrSize, ctnrOffset, cursorPos;
if (this.options.vertical) {
ctnrSize = this.ctnr.height();
ctnrOffset = this.ctnr.offset().top;
cursorPos = y;
}
else {
ctnrSize = this.ctnr.width();
ctnrOffset = this.ctnr.offset().left;
cursorPos = x;
}
cursorPos = (cursorPos - ctnrOffset) - (ctnrSize / 2);
if (this.cursorPos == cursorPos) {return;}
this.cursorPos = cursorPos;
y = Math.round(Math.pow(cursorPos / (this.speedConstant * ctnrSize), 3));
if (this.speed == y) {return;}
if ((y < 1 && y > -1)) {
this.stop();
return;
}
this.start(y);
},
_setArrowOpacity: function() {
var o = this.options, done = false, maxScroll, scroll, limit, opacity;
if (!o.arrows || o.fixedArrows) {return;}
if (o.vertical) {
maxScroll = this.listctnr[0].scrollHeight - this.listctnr.height();
scroll = this.listctnr[0].scrollTop;
}
else {
maxScroll = this.listctnr[0].scrollWidth - this.listctnr.width();
scroll = this.listctnr[0].scrollLeft;
}
limit = o.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
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
if (opacity <= 0) {
$('div.arrow.left, div.arrow.top', this.ctnr).hide();
if(maxScroll > 0) {
$('div.arrow.right, div.arrow.bottom', this.ctnr).show().css('opacity', limit);
}
done = true;
}
if (opacity >= limit || maxScroll <= 0) {
$('div.arrow.right, div.arrow.bottom', this.ctnr).hide();
done = true;
}
if (!done) {
$('div.arrow.left, div.arrow.top', this.ctnr).show().css('opacity', opacity);
$('div.arrow.right, div.arrow.bottom', this.ctnr).show().css('opacity', (limit - opacity));
}
},
_move: function() {
if (!this.isMoving) {return;}
this._setArrowOpacity();
var self = this, scrollSide;
if (!this.options.vertical) {scrollSide = "scrollLeft";}
else {scrollSide = "scrollTop";}
this.listctnr[0][scrollSide] += this.speed;
this.timer = setTimeout(function() {
self._move();
}, this.options.refreshRate);
if ($.isFunction(this.options.move)) {
this.options.move();
}
},
start: function(speed) {
if (typeof speed != "undefined" && this.speed != speed) {
if (this.options.debug) {
log('[HoverScroll] Starting to move. Speed: ' + speed);
}
// this.stop();
clearTimeout(this.timer);
this.speed = speed;
this.isMoving = true;
this._move();
if ($.isFunction(this.options.start)) {
this.options.start();
}
}
},
stop: function() {
if (this.isMoving) {
this.isMoving = false;
this.speed = 0;
clearTimeout(this.timer);
if ($.isFunction(this.options.stop)) {
this.options.stop();
}
}
}
});
/**
* log errors to consoles (firebug, opera) if exist, else uses alert()
*/
function log() {
try {console.log.apply(console, arguments);}
catch (e) {
try {opera.postError.apply(opera, arguments);}
catch (e) {}
}
};
})(jQuery);