-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollBar (2).js
231 lines (186 loc) · 5.5 KB
/
scrollBar (2).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
define(function(require, exports, module) {
var cache = [];
function ScrollBar(options) {
if (options.id) {
cache.push(options.id);
}
var initOptions = {
wheel : true, //是否滚动
wheelSpeed : 20, //滚动速度
axis : 'y' //横竖滚动条位置 (x,y)
};
this.options = $.extend(initOptions, options);
this.options.id = $(this.options.id)
if (this.options.id.length === 0) {
throw new TypeError('This page not found element');
}
if(cache.indexOf(this.options.id)!==-1){
this.position();
this.update();
return;
}
this.randerDom();
this.bindDom();
this.addWheel();
this.update();
this.position();
this.scrollResize();
}
ScrollBar.prototype.randerDom = function() {
this.content = this.options.id;
this.content.css({'position' : 'absolute', top : 0, left : 0});
this.contentParent = $('<div class="contentParent"></div>');
this.content.wrap(this.contentParent);
this.contentParent = this.content.parent();
this.bar = $('<div class="bar"><div class="point"></div></div>').appendTo(this.contentParent);
this.point = this.bar.find('.point');
// 默认样式 重置请用!import
var contentParentCss = {
'height' : '100%',
'overflow' : 'hidden',
'position' : 'relative'
};
this.contentParent.css(contentParentCss);
var barCss = {
'height' : '100%',
'position' : 'absolute',
'top' : '0',
'right' : '0',
'zIndex' : '998'
};
var pointCss = {
'position' : 'absolute',
'top' : '0',
'left' : '0',
'zIndex' : '999'
};
if (this.options.axis == 'x') {
barCss.height = '6px';
barCss.width = '100%';
delete barCss.top;
barCss.bottom = '0';
pointCss.height = '6px';
}
this.point.css(pointCss);
this.bar.css(barCss);
this.t = parseInt(this.point.position().top);
this.l = parseInt(this.point.position().left);
};
ScrollBar.prototype.bindDom = function() {
var that = this;
this.point.on('mousedown', function(ev) {
var disY = ev.clientY - that.point.position().top;
var disX = ev.clientX - that.point.position().left;
$(document).on('mousemove', function(ev) {
if (that.options.axis == 'y') {
that.t = ev.clientY - disY;
that.position(that.t);
} else {
that.l = ev.clientX - disX;
that.position(that.l);
}
});
$(document).on('mouseup', function() {
$(document).off('mousemove');
$(document).off('mouseup');
});
ev.preventDefault();
});
};
// 滚动 //支持回调 滚动到底部加载数据
ScrollBar.prototype.addWheel = function(loadFn) {
var that = this;
this.down = false;
var speed = parseInt(that.options.wheelSpeed);
this.contentParent.parent().on('mousewheel DOMMouseScroll', function(ev) {
if (ev.originalEvent.detail > 0 || ev.originalEvent.wheelDelta < 0) {
that.down = false;
} else {
that.down = true;
}
if (that.options.wheel) {
if (that.options.axis == 'y') {
if (!that.down) {
that.t += speed;
} else {
that.t -= speed;
}
that.position(that.t);
that.downNum=that.bar.height()-that.point.height();
if(that.downNum===that.t){
//滚动到底部加载数据回调
loadFn && loadFn();
}
} else {
if (!that.down) {
that.l += speed;
} else {
that.l -= speed;
}
}
}
ev.preventDefault();
});
};
// 重置尺寸
ScrollBar.prototype.update = function(direction) {
var scaleY = this.content.parent().height() / this.content.height();
var scaleX = this.content.parent().width() / this.content.width();
if (this.options.axis == 'y') {
if (scaleY < 1) {
this.content.css('position', 'absolute');
this.bar.css('display', 'block');
this.point.css('height', this.bar.height() * this.content.parent().height() / this.content.height());
} else {
this.content.css('position', 'static');
this.point.css('height', this.bar.height());
this.bar.css('display', 'none');
}
} else {
if (scaleX < 1) {
this.content.css('position', 'absolute');
this.bar.css('display', 'block');
this.point.css('width', this.bar.width() * this.contentParent.width() / this.content.width());
} else {
this.content.css('position', 'static');
this.point.css('width', this.bar.width());
this.bar.css('display', 'none');
}
}
//加载后bar位置
if (direction != 'undefind') {
if (direction == 'top') {
this.t = 0;
} else if (direction == 'bottom') {
this.t = this.bar.height() - this.point.height();
}else{
this.t = direction; //接受数值
}
this.position(this.t);
}
};
// 上下
ScrollBar.prototype.position = function() {
if (this.options.axis == 'y') {
this.t < 0 && (this.t = 0);
this.t > this.bar.height() - this.point.height() && (this.t = this.bar.height() - this.point.height());
this.point.css('top', this.t);
var scale = this.t / (this.bar.height() - this.point.height());
this.content.css('top', -scale * (this.content.height() - this.contentParent.height()));
} else {
this.l < 0 && (this.l = 0);
this.l > this.bar.width() - this.point.width() && (this.l = this.bar.width() - this.point.width());
this.point.css('left', this.l);
var scale = this.l / (this.bar.width() - this.point.width());
this.content.css('left', -scale * (this.content.width() - this.contentParent.width()));
}
};
ScrollBar.prototype.scrollResize = function() {
var that = this;
$(window).on('resize.scroll',function() {
that.position();
that.update();
}).trigger('resize.scroll');
};
module.exports = ScrollBar;
});