-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdepage-scroller.js
More file actions
229 lines (178 loc) · 6.66 KB
/
depage-scroller.js
File metadata and controls
229 lines (178 loc) · 6.66 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
/**
* @require framework/shared/jquery-1.8.3.js
* @require framework/shared/jquery.mousewheel.min.js
*
* @file depage-scroller.js
*
* custom scroller to replace default scrollbars for scrolling elements
*
*
* copyright (c) 2011-2012 Frank Hellenkamp [jonas@depage.net]
*
* @author Frank Hellenkamp [jonas@depage.net]
*/
;(function($){
if(!$.depage){
$.depage = {};
};
$.depage.scroller = function(el, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.scroller", base);
// ratio between frame and content
var ratio;
// dragging related variables
var dragging = false;
var dragHandleY = 0;
// {{{ references to new elements
// new layout wrapper (styled like original)
var $scrollOrigin = null;
// new wrapper for scrolling content
var $scrollFrame = null;
// content inside the scroller
var $scrollContent = null;
// scroll bar
var $scrollBar = null
// scroll handle
var $scrollHandle = null;
// }}}
// {{{ init()
base.init = function() {
base.options = $.extend({},$.depage.scroller.defaultOptions, options);
$scrollContent = base.$el;
var className = $scrollContent[0].className;
var id = $scrollContent[0].id;
// wrap into additional depage-scroller divs and move classes tp parent
$scrollFrame = $scrollContent.wrap("<div class=\"" + className + "\" id=\"" + id + "\"><div class=\"depage-scroller-frame\"></div></div>").parent();
$scrollOrigin = $scrollFrame.parent();
// move classes to parent
$scrollContent.removeAttr(className);
$scrollContent.addClass("depage-scroller-content");
$scrollContent.removeAttr("id");
$scrollOrigin.attr("style", $scrollContent.attr("style"));
$scrollContent.removeAttr("style");
// make origin relative or absolute
if ($scrollOrigin.css("position") == "static") {
$scrollOrigin.css("position", "relative");
}
// add scrollbar and -handle
$("<div class=\"scroll-bar\"><div class=\"scroll-handle\"></div></div>").prependTo($scrollOrigin);
$scrollBar = $(".scroll-bar", $scrollOrigin);
$scrollHandle = $(".scroll-handle", $scrollOrigin);
// added drag event to handle
$scrollHandle.mousedown( base.startDrag );
// add scroll events
$scrollFrame.scroll( base.onScroll );
// call scroll event also when window resizes
$(window).resize( base.onScroll );
// call scroll event for initialization
base.onScroll();
// add mousewheel events
$scrollFrame.mousewheel( base.onMousewheel );
};
// }}}
// {{{ onScroll()
base.onScroll = function() {
if (!dragging) {
ratio = $scrollContent.height() / $scrollFrame.height();
var h = $scrollFrame.height() / ratio;
if (h >= $scrollFrame.height()) {
h = $scrollFrame.height();
$scrollBar.hide();
} else {
$scrollBar.show();
}
var t = $scrollFrame.scrollTop() / ratio;
$scrollHandle.css({
height: h,
top: t
});
}
};
// }}}
// {{{ onMousewheel()
base.onMousewheel = function(e, delta) {
if ($scrollFrame.height() < $scrollContent.height()) {
$scrollFrame.scrollTop($scrollFrame.scrollTop() - base.options.distance * delta);
return false;
}
};
// }}}
// {{{ startDrag()
base.startDrag = function(e) {
dragHandleY = e.offsetY || e.pageY - $(e.target).offset().top;
dragging = true;
$scrollOrigin.addClass("dragging");
$(window).bind("mousemove.depageScroller", base.onDrag);
$(window).bind("mouseup.depageScroller", base.stopDrag);
base.disableIframes();
return false;
};
// }}}
// {{{ onDrag()
base.onDrag = function(e) {
if (dragging) {
var scrollY = e.pageY - dragHandleY;
var offset = $scrollOrigin.offset();
var min = offset.top;
var max = offset.top + $scrollOrigin.height() - $scrollHandle.height();
if (scrollY < min) {
scrollY = min;
} else if (scrollY > max) {
scrollY = max;
}
$scrollHandle.offset({
top: scrollY
});
$scrollFrame.scrollTop((scrollY - offset.top) * ratio);
}
};
// }}}
// {{{ stopDrag()
base.stopDrag = function() {
$scrollOrigin.removeClass("dragging");
$(window).unbind(".depageScroller");
base.enableIframes();
dragging = false;
return false;
};
// }}}
// {{{ disableIframes()
base.disableIframes = function() {
$("iframe").each( function() {
var $iframe = $(this);
var offset = $iframe.offset();
var $disabler = $("<div class=\"disable-iframe-events\"></div>").appendTo(document.body);
$disabler.css({
position: "absolute",
zIndex: 10000,
top: offset.top,
left: offset.left,
width: $iframe.width(),
height: $iframe.height()
});
});
}
// }}}
// {{{ enableIframes()
base.enableIframes = function() {
$(".disable-iframe-events").remove();
}
// }}}
// Run initializer
base.init();
};
$.depage.scroller.defaultOptions = {
distance: 25 // number of pixels to scroll on mouse wheel events
};
$.fn.depageScroller = function(options){
return this.each(function(){
(new $.depage.scroller(this, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */