@@ -22,43 +22,28 @@ const ScrollScreen = {
22
22
this . toHeight = - 1 ;
23
23
this . num = 0 ;
24
24
// this.currentNum = 0;
25
- [ 'raf' , 'cancelRequestAnimationFrame' , 'onWheel' , 'startScroll' ]
25
+ [ 'raf' , 'cancelRequestAnimationFrame' , 'onWheel' , 'startScroll' , 'isScroll' ]
26
26
. forEach ( ( method ) => this [ method ] = this [ method ] . bind ( this ) ) ;
27
27
EventListener . addEventListener ( 'wheel.scrollWheel' , this . onWheel ) ;
28
- EventListener . addEventListener ( 'scroll.scrollScreen' , this . scrollEvent ) ;
29
28
// 刚进入时滚动条位置
30
- // requestAnimationFrame(this.startScroll)
31
29
setTimeout ( this . startScroll ) ;
32
30
} ,
33
- scrollEvent ( ) {
34
- const _mapped = mapped . getMapped ( ) ;
35
- const _arr = _mapped . __arr ;
36
- this . scrollTop = currentScrollTop ( ) ;
37
- _arr . forEach ( ( str , i ) => {
38
- const dom = _mapped [ str ] ;
39
- const domOffsetTop = dom . offsetTop ;
40
- const domHeight = dom . getBoundingClientRect ( ) . height ;
41
- if ( this . scrollTop >= domOffsetTop && this . scrollTop < domOffsetTop + domHeight ) {
42
- this . currentNum = i ;
43
- }
44
- } ) ;
45
- } ,
31
+
46
32
startScroll ( ) {
47
33
const _mapped = mapped . getMapped ( ) ;
48
34
const _arr = _mapped . __arr ;
49
35
if ( ! _arr . length ) {
50
36
EventListener . removeEventListener ( 'wheel.scrollWheel' , this . onWheel ) ;
51
37
return ;
52
38
}
53
- this . scrollTop = window . pageYOffset ;
39
+ this . scrollTop = currentScrollTop ( ) ;
54
40
_arr . forEach ( ( str , i ) => {
55
41
const dom = _mapped [ str ] ;
56
42
const domOffsetTop = dom . offsetTop ;
57
43
const domHeight = dom . getBoundingClientRect ( ) . height ;
58
44
if ( this . scrollTop >= domOffsetTop && this . scrollTop < domOffsetTop + domHeight ) {
59
45
this . num = i ;
60
46
this . toHeight = domOffsetTop ;
61
- // this.currentNum = this.num;
62
47
}
63
48
} ) ;
64
49
// 如果 toHeight === -1 且 this.scrollTop 有值时;
@@ -69,7 +54,6 @@ const ScrollScreen = {
69
54
const tooNum = Math . ceil ( ( this . scrollTop - endDom . offsetTop -
70
55
endDom . getBoundingClientRect ( ) . height ) / windowHeight ) ;
71
56
this . num = mapped . getMapped ( ) . __arr . length + tooNum ;
72
- // this.currentNum = this.num;
73
57
}
74
58
return ;
75
59
}
@@ -100,15 +84,35 @@ const ScrollScreen = {
100
84
requestAnimationFrame . cancel ( this . rafID ) ;
101
85
this . rafID = - 1 ;
102
86
} ,
87
+ getComputedStyle ( dom ) {
88
+ return document . defaultView ? document . defaultView . getComputedStyle ( dom ) : { } ;
89
+ } ,
90
+ isScroll ( dom ) {
91
+ const style = this . getComputedStyle ( dom ) ;
92
+ const overflow = style . overflow ;
93
+ const overflowY = style . overflowY ;
94
+ const isScrollOverflow = overflow === 'auto' || overflow === 'scroll' || overflow === 'overlay'
95
+ || overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'overlay' ;
96
+ if ( dom === document . body ) {
97
+ return false ;
98
+ } else if ( dom . scrollHeight > dom . offsetHeight
99
+ && isScrollOverflow
100
+ && dom . scrollTop < dom . scrollHeight ) {
101
+ return true ;
102
+ }
103
+ return this . isScroll ( dom . parentNode ) ;
104
+ } ,
103
105
onWheel ( e ) {
104
106
const _mapped = mapped . getMapped ( ) ;
105
107
if ( ! _mapped . __arr . length ) {
106
108
EventListener . removeEventListener ( 'wheel.scrollWheel' , this . onWheel ) ;
107
109
return ;
108
110
}
111
+ if ( this . isScroll ( e . target ) ) {
112
+ return ;
113
+ }
109
114
const deltaY = e . deltaY ;
110
115
e . preventDefault ( ) ;
111
- // console.log(e.wheelDelta,e.deltaY)
112
116
if ( this . rafID === - 1 && deltaY !== 0 && this . toHeight === - 1 ) {
113
117
// 如果滚动条托动过了,需要获取当前的num;
114
118
const _arr = _mapped . __arr ;
@@ -165,14 +169,13 @@ const ScrollScreen = {
165
169
windowHeight * ( this . num - mapped . getMapped ( ) . __arr . length ) : this . toHeight ;
166
170
this . toHeight = this . toHeight < 0 ? 0 : this . toHeight ;
167
171
this . toHeight = this . toHeight > docHeight - windowHeight ?
168
- docHeight - windowHeight : this . toHeight ;
172
+ ( docHeight - windowHeight ) : this . toHeight ;
169
173
this . rafID = requestAnimationFrame ( this . raf ) ;
170
- // this.currentNum = this.num;
174
+ this . currentNum = this . num ;
171
175
}
172
176
} ,
173
177
unMount ( ) {
174
178
EventListener . removeEventListener ( 'wheel.scrollWheel' , this . onWheel ) ;
175
- EventListener . removeEventListener ( 'scroll.scrollScreen' , this . scrollEvent ) ;
176
179
} ,
177
180
} ;
178
181
export default {
0 commit comments