2727 } ) ) ;
2828 } , true ) ;
2929
30+ function scrollPos ( target ) {
31+ if ( target === document || target === document . documentElement ) {
32+ return { x : window . scrollX , y : window . scrollY } ;
33+ }
34+ return { x : target . scrollLeft || 0 , y : target . scrollTop || 0 } ;
35+ }
36+
3037 var scrollTimer = null ;
31- var scrollStart = { x : window . scrollX , y : window . scrollY } ;
38+ var scrollStart = null ;
39+ var scrollTarget = null ;
3240 document . addEventListener ( 'scroll' , function ( e ) {
33- var fromX = scrollStart . x , fromY = scrollStart . y ;
3441 var target = e . target ;
42+ // If target changed mid-scroll, reset tracking for the new target.
43+ if ( scrollTarget !== target ) {
44+ scrollStart = scrollPos ( target ) ;
45+ scrollTarget = target ;
46+ }
47+ var fromX = scrollStart . x , fromY = scrollStart . y ;
3548 var s = target === document ? 'document' : sel ( target ) ;
3649 if ( scrollTimer ) clearTimeout ( scrollTimer ) ;
3750 scrollTimer = setTimeout ( function ( ) {
38- var toX = window . scrollX , toY = window . scrollY ;
39- if ( Math . abs ( toX - fromX ) > 5 || Math . abs ( toY - fromY ) > 5 ) {
51+ var pos = scrollPos ( target ) ;
52+ if ( Math . abs ( pos . x - fromX ) > 5 || Math . abs ( pos . y - fromY ) > 5 ) {
4053 send ( JSON . stringify ( {
4154 type : 'scroll_settled' ,
4255 from_x : fromX , from_y : fromY ,
43- to_x : toX , to_y : toY ,
56+ to_x : pos . x , to_y : pos . y ,
4457 target_selector : s
4558 } ) ) ;
4659 }
47- scrollStart = { x : toX , y : toY } ;
60+ scrollStart = { x : pos . x , y : pos . y } ;
61+ scrollTarget = null ;
4862 } , 300 ) ;
4963 } , true ) ;
5064} ) ( ) ;
0 commit comments