-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollDiv.vue
575 lines (509 loc) · 13.3 KB
/
scrollDiv.vue
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<template>
<div class="z-scrollDiv" ref="all_wrap" @mouseenter="barRender">
<div class="z-scroll-main" ref="scroll" :style="{transform:`translate3d(${X_position}px,${Y_position}px,0)`}">
<!-- <div class="z-scroll-main" ref="scroll" :style="{left:`${X_position}px`,top:`${Y_position}px`}"> -->
<slot></slot>
</div>
<!-- 滚动条 -->
<div class="z-scroll-bar y" ref="Y_bar">
<div v-show="bar.Y.show" :style="{height:`${bar.Y.size}px`,transform:`translate3d(0,${bar.Y.number}px,0)`}"></div>
</div>
<div class="z-scroll-bar x" ref="X_bar">
<div v-show="bar.X.show" :style="{width:`${bar.X.size}px`,transform:`translate3d(${bar.X.number}px,0,0)`}"></div>
</div>
<!-- 遮罩 -->
<div class="z-cover" v-show="dragFlag"></div>
</div>
</template>
<script>
export default {
props:{
// 滚轮滚动一次位移的距离
speed:{
type:Number,
},
// 动画函数
animate:{
type:String,
},
// 启用X方向滚动
xbar:{
type:Boolean,
default:false,
}
},
data(){
return {
dragFlag:false, //拖动状态判断
drag_bar:'Y', //触发拖动的bar
// **** 滚动条数据
minSize:20, //px 滚动条最小长度
bar:{
Y:{
size:0, //px Y滚动条高度
show:false,
number:0, //位置
},
X:{
size:0, //px X滚动条宽度
show:false,
number:0, //位置
},
},
// *** 动画效果
// * t: current time(当前时间);
// * b: beginning value(初始值);
// * c: change in value(变化量);
// * d: duration(持续时间)。
Tween:{
Linear: function(t, b, c, d) {
return c * t / d + b;
},
Quad: {
easeIn: function(t, b, c, d) {
return c * (t /= d) * t + b;
},
easeOut: function(t, b, c, d) {
return -c *(t /= d)*(t-2) + b;
},
easeInOut: function(t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t-2) - 1) + b;
}
},
},
}
},
computed:{
// **** 配置
//每次滚轮的触发的移动距离
scrollSpeed(){
if(typeof this.speed == 'number'){
return this.speed;
}else{
return 60;
}
},
// 动画函数
choose_animate(){
if(this.Tween[this.animate]){
return this.Tween[this.animate]
}else{
return this.Tween.Quad.easeOut;
}
},
// **** 计算数据
// Y滚动条可以滚动的距离
Y_barRange(){
let wrap = this.$refs.all_wrap;
if(wrap){
return wrap.clientHeight - this.bar.Y.size;
}
},
// X滚动条可以滚动的距离
X_barRange(){
let wrap = this.$refs.all_wrap;
if(wrap){
return wrap.clientWidth - this.bar.X.size;
}
},
// 内容块位置
// Y内容块
Y_position(){
// 无滚动 不计算
if(!this.bar['Y'].show || !this.$refs.scroll){
return 0;
}
return this.calc_position();
},
// X内容块
X_position(){
// 无滚动 不计算
if(!this.bar['X'].show || !this.$refs.scroll){
return 0;
}
return this.calc_position('X');
},
},
mounted(){
this.init();
},
methods:{
// ******** 初始化
init(){
// 处理兼容
if (!window.requestAnimationFrame) {
requestAnimationFrame = function(fn) {
setTimeout(fn, 17);
};
}
// 数据初始化
this.style_init();
// 渲染滚动条
this.barRender();
// 绑定事件
this.bindEvent();
},
// ******* 数据初始化
// 检测padding 提供给组件
style_init(){
let wrap = this.$refs.all_wrap;
let main = this.$refs.scroll;
// 移植padding
let padd_top = this.getStyle(wrap,'padding-top');
let padd_right = this.getStyle(wrap,'padding-right');
let padd_bottom = this.getStyle(wrap,'padding-bottom');
let padd_left = this.getStyle(wrap,'padding-left');
wrap.style['padding'] = 0;
main.style['padding'] = `${padd_top} ${padd_right} ${padd_bottom} ${padd_left}`;
},
// ******* 事件绑定
bindEvent(){
this.clickEvent();
this.dragEvent();
this.scrollEvent();
},
// 滚轮事件
scrollEvent(){
let main = this.$refs.scroll;
let keyStr = (document.mozFullScreen !== undefined)?"DOMMouseScroll":"mousewheel";
main.addEventListener(keyStr,(e)=>{
this.$emit("scroll"); // 暴露的事件
e = this.eventFilter(e);
if(this.bar.Y.show){
this.scrollCalc('Y',e.direction*this.scrollSpeed,20)
// this.ani_scroll('Y',this.bar.Y.number + e.direction*this.scrollSpeed,20)
e.preventDefault();
e.stopPropagation();
}else if(this.bar.X.show){
this.scrollCalc('X',e.direction*this.scrollSpeed,20)
// this.ani_scroll('X',this.bar.X.number + e.direction*this.scrollSpeed,20)
e.preventDefault();
e.stopPropagation();
}
});
},
eventFilter(e){ //过滤事件对象
e = e || window.event;
if(e.type === "mousewheel"){
e.direction = e.wheelDelta<0?1:-1;
}else if(e.type === "DOMMouseScroll"){
e.direction = e.detail>0?1:-1;
}
return e;
},
// 拖拽事件
dragEvent(){
let wrap = this.$refs.all_wrap;
let main = this.$refs.scroll;
let last = {
X:0,
Y:0
};
wrap.addEventListener('mousemove',(e)=>{
e = e || window.event;
if(this.dragFlag){
let key = this.drag_bar;
last[key] = last[key] || e[`client${key}`];
let cha = e[`client${key}`] - last[key];
last[key] = e[`client${key}`];
this.barNumberRender(key,this.bar[key].number + cha);
}
});
wrap.addEventListener('mouseup',(e)=>{
e = e || window.event;
if(this.dragFlag == true){
last[this.drag_bar] = 0;
this.dragFlag = false;
}
});
},
// 点击事件
clickEvent(){
let wrap = this.$refs.all_wrap;
let y_bar = this.$refs.Y_bar;
let x_bar = this.$refs.X_bar;
y_bar.addEventListener('mousedown',(e)=>{
this.mouseDown(e,y_bar,'Y')
})
x_bar.addEventListener('mousedown',(e)=>{
this.mouseDown(e,x_bar,'X')
})
},
mouseDown(e,el,key = 'Y'){
e = e || window.event;
let target = e[`client${key}`] - this.getOrigin(el,key);
let size = this.bar[key].size;
let number = this.bar[key].number;
// 点击到滚动条
if(target >= number && target <= number + size){
if(this.bar[key].show){
this.drag_bar = key;
this.dragFlag = true;
}
}else{
if(target < number){
size = 0;
}
this.ani_scroll(key,target - size,25)
}
},
// *** 属性渲染器 滚动条位置,长度,宽度渲染
//滚动条渲染
barRender(){
// 无dom不需要进行渲染
if(!this.$refs.scroll){
return;
}
// 更新滚动条长度
this.barSizeRender();
this.barSizeRender("X");
// 更新滚动条位置
this.barNumberRender();
this.barNumberRender('X');
},
// 滚动条长度渲染
// * key 'X,Y' (可省略,默认Y)
// * wrap_size 容器尺寸
// * main_size 内容尺寸
barSizeRender(...arg){
let key = 'Y',wrap_size,main_size;
let word = "Height";
// 处理参数
let hasKey = this.hasKeyJudge(arg);
if(hasKey){
if(key != arg[0]){
key = 'X';
word = "Width"
}
wrap_size = arg[1] || this.$refs.all_wrap[`client${word}`];
main_size = arg[2] || this.$refs.scroll[`scroll${word}`];
}else{
wrap_size = arg[0] || this.$refs.all_wrap[`client${word}`];
main_size = arg[1] || this.$refs.scroll[`scroll${word}`];
}
// 更新长度
let per = wrap_size*100/main_size;
// 控制显示
if(this.xbar == false && key == "X"){ //****遮蔽x滚动条 */
this.bar[key].show = false;
}else{
this.bar[key].show = per<100;
}
// console.log(this.getStyle(this.$refs.scroll,'height'))
// 内容 不足 容器
if(key == 'Y' && per > 100){
this.$refs.scroll.style.height = wrap_size + 'px';
}
let value = wrap_size*per/100;
this.bar[key].size = (value<this.minSize)?this.minSize:value;
},
// 滚动条位置渲染 包含滚动窗口位置渲染
barNumberRender(...arg){
let key = 'Y',value;
// 处理参数
let hasKey = this.hasKeyJudge(arg);
if(hasKey){
if(key != arg[0]){
key = 'X';
}
value = arg[1] || this.bar[key].number;
}else{
value = arg[0] || this.bar[key].number;
}
if(!this.bar[key].show || value < 0){ // 无滚动条 重置位置
value = 0;
}else if(value > this[`${key}_barRange`]){ // 超出范围 重置到极限位置
value = this[`${key}_barRange`];
}
this.bar[key].number = value;
},
// *****特别操作
zero(){
this.bar.Y.number = 0;
this.bar.X.number = 0;
},
// scrollTop(){
// setTimeout(()=>{
// this.ani_scroll("Y",0,5)
// },300)
// },
// **** 最终运动
// 抽离动画
step(obj,func,end){
// 更新当前位置值
let value = this.choose_animate(obj.t, obj.b, obj.c, obj.d);
func && func(value)
obj.t++;
if (obj.t <= obj.d) {
// 继续运动
let run = this.step.bind(this,obj,func,end);
requestAnimationFrame(run);
} else {
// 动画结束
typeof end == 'function' && end.call(this);
}
},
// 滚动条运动
// * key 'X,Y' (可省略,默认为Y)
// * target 目标位置
// * time 运动时间(可省略,默认100)
ani_scroll(...arg){
let key = 'Y',target,time = 100;
// 处理参数
let hasKey = this.hasKeyJudge(arg);
if(hasKey){
if(key != arg[0]){
key = 'X';
}
target = arg[1] === undefined?this.bar[key].number:arg[1];
time = arg[2] || time;
}else{
target = arg[0] === undefined?this.bar[key].number:arg[0];
time = arg[1] || time;
}
let begin_number = this.bar[key].number;
this.step({
t:0,
b:begin_number,
c:target - begin_number,
d:time,
},(value)=>{
this.barNumberRender(key,value)
})
},
// ****其余辅助函数
hasKeyJudge(arr){
return arr.some(val=>isNaN(val - 0));
},
// 根据滚动条位置计算 主体位置
calc_position(key = 'Y'){
let word = key == 'Y'?'Height':'Width';
// 容器高和内容高
let wrap_size = this.$refs.all_wrap[`client${word}`];
let main_size = this.$refs.scroll[`scroll${word}`];
let differ = main_size - wrap_size;
// 位移比例
let rate = differ/this[`${key}_barRange`];
return Math.round(-rate*this.bar[key].number);
},
// 根据主体位移距离 计算滚动条位移
// key:X,Y轴 distance:距离 time:动画时间
scrollCalc(key,distance,time){
let word = key == 'Y'?'Height':'Width';
// 容器高和内容高
let wrap_size = this.$refs.all_wrap[`client${word}`];
let main_size = this.$refs.scroll[`scroll${word}`];
let differ = main_size - wrap_size;
// 位移比例
let rate = differ/this[`${key}_barRange`];
// 通过计算 安排滚动条位移动画
this.ani_scroll(key,this.bar[key].number + distance/rate ,time)
},
// 起始位置
getOrigin(element,key = 'Y'){
let word = key == 'Y'?'top':'left';
return element.getBoundingClientRect()[word];
},
// 获取CSS属性
getStyle(element, attr) {
if(element.currentStyle) {
return element.currentStyle[attr];
} else {
return getComputedStyle(element, false)[attr];
}
},
}
}
</script>
<style lang="less">
@bar_color:#cbd5e5;
@samll_w:5px;
@big_w:10px;
.clear{
&:after{
content:"";
display: block;
clear:both;
}
}
// 滚动框
.z-scrollDiv{
position: relative;
overflow: hidden;
.clear();
.z-scroll-main{
height: 100%;
min-width: 100%;
box-sizing: border-box;
padding:0.1px;
position: relative;
float:left;
.clear();
}
&:hover{
>.z-scroll-bar{
opacity: 1;
}
}
.z-scroll-bar{
opacity: 0;
position: absolute;
transition: 0.2s;
>div{
position: absolute;
top:0;
cursor:pointer;
border-radius: @samll_w/2;
background:@bar_color;
}
// 竖滚动条
&.y{
height: 100%;
right: 3px;
top:0;
width:@samll_w;
>div{
right: 0;
width:@samll_w;
}
&:hover{
width:@big_w;
>div{
border-radius: @big_w/2;
width:@big_w;
}
}
}
// 横滚动条
&.x{
width: 100%;
bottom: 3px;
left:0;
height:@samll_w;
>div{
left: 0;
height:@samll_w;
}
&:hover{
height:@big_w;
>div{
border-radius: @big_w/2;
height:@big_w;
}
}
}
}
// 遮罩
.z-cover{
position: fixed;
width:100%;
height:100%;
top:0;
left:0;
// background:rgba(0,0,0,0.4);
opacity: 0;
z-index:1000;
}
}
</style>