This repository has been archived by the owner on Mar 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
antiscroll.js
337 lines (285 loc) · 10.9 KB
/
antiscroll.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
// Generated by CoffeeScript 1.3.3
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
(function($) {
/*
scrollbarSize = ->
unless @size
div = $ """
<div style="width:50px;height:50px;overflow:hidden;
position:absolute;top:-200px;left:-200px;"><div style="height:100px;">
</div>
"""
$('body').append div
w1 = $('div', div).width() # fuck
div.css 'overflow-y', 'scroll'
w2 = $('div', div).height() # fuck
$(div).remove()
@size = w1 - w2
return @size
*/
var Antiscroll, scrollbarSize;
scrollbarSize = function() {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
outer.style.height = "150px";
outer.style.overflow = "hidden";
outer.appendChild (inner);
document.body.appendChild (outer);
var w1 = inner.offsetWidth;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
if (w1 == w2) w2 = outer.clientWidth;
document.body.removeChild (outer);
;
return w1 - w2;
};
$.fn.antiscroll = function(o) {
var anti;
anti = null;
this.each(function() {
$(this).find('.antiscroll-scrollbar').remove();
return anti = new Antiscroll(this, o);
});
return anti;
};
return Antiscroll = (function() {
var Scrollbar;
function Antiscroll(el, o) {
this.o = o != null ? o : {};
this.el = $(el);
this.x = this.o.x !== false;
this.y = this.o.y !== false;
this.padding = this.o.padding || 2;
this.inner = this.el.find('.antiscroll-inner');
this.inner.css({
width: this.inner.width() + scrollbarSize(),
height: this.inner.height() + scrollbarSize()
});
this.refresh();
}
Antiscroll.prototype.refresh = function() {
var needHScroll, needVScroll;
needHScroll = this.inner.get(0).scrollWidth > this.el.width();
needVScroll = this.inner.get(0).scrollHeight > this.el.height();
if (!this.horizontal && needHScroll && this.x) {
this.horizontal = new Scrollbar.Horizontal(this);
} else if (this.horizontal && !needHScroll) {
this.horizontal.destroy();
this.horizontal = null;
}
if (!this.vertical && needVScroll && this.y) {
return this.vertical = new Scrollbar.Vertical(this);
} else if (this.vertical && !needVScroll) {
this.vertical.destroy();
return this.vertical["null"];
}
};
Antiscroll.prototype.destroy = function() {
if (this.horizontal) {
this.horizontal.destroy();
}
if (this.vertical) {
this.vertical.destroy();
}
return this;
};
Antiscroll.prototype.rebuild = function() {
this.destroy();
this.inner.attr('style', '');
$.Antiscroll.call(this, this.el, this.o);
return this;
};
Scrollbar = (function() {
function Scrollbar(pane) {
var _this = this;
this.pane = pane;
this.hide = __bind(this.hide, this);
this.pane.el.append(this.el);
this.innerEl = this.pane.inner.get(0);
this.dragging = false;
this.enter = false;
this.shown = false;
/*
types = ['DOMMouseScroll', 'mousewheel']
handler = (e) =>
orgEvent = e or window.event
args = [].slice.call(arguments, 1)
delta = 0
returnValue = true
deltaX = 0
deltaY = 0
event = orgEvent
event.type = 'mousewheel'
# scrollwheel delta
if event.wheelDelta then delta = event.wheelDelta / 120
if event.detail then delta = -event.detail/3
# multidimensional scroll (touchpads) deltas
deltaY = delta
# Gecko
if orgEvent.axis and orgEvent.axis is orgEvent.HORIZONTAL_AXIS
deltaY = 0
deltaX = -1*delta
# Webkit
if orgEvent.wheelDeltaY then deltaY = orgEvent.wheelDeltaY/120
if orgEvent.wheelDeltaX then deltaX = -1*orgEvent.wheelDeltaX/120
# Webkit
if orgEvent.wheelDeltaY then deltaY = orgEvent.wheelDeltaY/120
if orgEvent.wheelDeltaX then deltaX = -1*orgEvent.wheelDeltaX/120
# Add event and delta to the front of the arguments
args.unshift event, delta, deltaX, deltaY
#console.log args
@mousewheel args
if window.addEventListener
for t in types
window.addEventListener t, handler, false
else
window.onmousewheel = handler
*/
this.pane.el.mouseover(function() {
_this.enter = true;
return _this.show();
});
this.pane.el.mouseout(function() {
_this.enter = false;
if (!_this.dragging) {
return _this.hide();
}
});
this.el.mousedown(function(e) {
e.preventDefault();
_this.dragging = true;
_this.startPageY = e.pageY - parseInt(_this.el.css('top'), 10);
_this.startPageX = e.pageX - parseInt(_this.el.css('left'), 10);
document.onselectstart = function() {
return false;
};
pane = _this.pane;
return $(document).mousemove(_this.mousemove).mouseup(function() {
_this.dragging = false;
document.onselectstart = null;
$(document).unbind('mousemove', _this.mousemove);
if (!_this.enter) {
return _this.hide;
}
});
});
this.pane.inner.scroll(function() {
if (!_this.shown) {
_this.show();
if (!_this.dragging && !_this.enter) {
_this.hiding = setTimeout(_this.hide, 1500);
}
}
return _this.update();
});
}
Scrollbar.prototype.destroy = function() {
this.el.remove();
return this;
};
Scrollbar.prototype.show = function(dur) {
if (!this.shown) {
this.update();
this.el.addClass('antiscroll-scrollbar-shown');
if (this.hiding) {
clearTimeout(this.hiding);
this.hiding = null;
}
return this.shown = true;
}
};
Scrollbar.prototype.hide = function() {
if (this.shown) {
this.el.removeClass('antiscroll-scrollbar-shown');
return this.shown = false;
}
};
return Scrollbar;
})();
Scrollbar.Horizontal = (function(_super) {
__extends(Horizontal, _super);
function Horizontal(pane) {
this.mousewheel = __bind(this.mousewheel, this);
this.mousemove = __bind(this.mousemove, this);
this.el = $('<div class="antiscroll-scrollbar antiscroll-scrollbar-horizontal">');
Horizontal.__super__.constructor.call(this, pane);
}
Horizontal.prototype.update = function() {
var innerEl, paneWidth, trackWidth;
paneWidth = this.pane.el.width();
trackWidth = paneWidth - this.pane.padding * 2;
innerEl = this.pane.inner.get(0);
return this.el.css({
width: trackWidth * paneWidth / innerEl.scrollWidth,
left: trackWidth * innerEl.scrollLeft / innerEl.scrollWidth
});
};
Horizontal.prototype.mousemove = function(e) {
var barWidth, innerEl, pos, trackWidth, y;
trackWidth = this.pane.el.width() - this.pane.padding * 2;
pos = e.pageX - this.startPageX;
barWidth = this.el.width();
innerEl = this.pane.inner.get(0);
y = Math.min(Math.max(pos, 0), trackWidth - barWidth);
return innerEl.scrollLeft = (innerEl.scrollWidth - this.pane.el.width()) * y / (trackWidth - barWidth);
};
Horizontal.prototype.mousewheel = function(e, delta, x, y) {
if ((x < 0 && this.pane.inner.get(0).scrollLeft) || (x > 0 && this.innerEl.scrollLeft + this.pane.el.width() === this.innerEl.scrollWidth)) {
e.preventDefault();
return false;
}
};
return Horizontal;
})(Scrollbar);
Scrollbar.Vertical = (function(_super) {
__extends(Vertical, _super);
function Vertical(pane) {
this.mousemove = __bind(this.mousemove, this);
this.mousewheel = __bind(this.mousewheel, this);
this.el = $('<div class="antiscroll-scrollbar antiscroll-scrollbar-vertical">');
Vertical.__super__.constructor.call(this, pane);
}
Vertical.prototype.update = function() {
var innerEl, paneHeight, trackHeight;
paneHeight = this.pane.el.height();
trackHeight = paneHeight - this.pane.padding * 2;
innerEl = this.innerEl;
return this.el.css({
height: trackHeight * paneHeight / innerEl.scrollHeight,
top: trackHeight * innerEl.scrollTop / innerEl.scrollHeight
});
};
Vertical.prototype.mousewheel = function(e, delta, x, y) {
if ((y > 0 && 0 === this.innerEl.scrollTop) || (y < 0 && this.innerEl.scrollTop + this.pane.el.height() === this.innerEl.scrollHeight)) {
e.preventDefault();
return false;
}
};
Vertical.prototype.mousemove = function(ev) {
var paneHeight = this.pane.el.height()
, trackHeight = paneHeight - this.pane.padding * 2
, pos = ev.pageY - this.startPageY
, barHeight = this.el.height()
, innerEl = this.innerEl
// minimum top is 0, maximum is the track height
var y = Math.min(Math.max(pos, 0), trackHeight - barHeight)
innerEl.scrollTop = (innerEl.scrollHeight - paneHeight)
* y / (trackHeight - barHeight);
return 0;
};
return Vertical;
})(Scrollbar);
return Antiscroll;
}).call(this);
})($);
}).call(this);