@@ -231,15 +231,19 @@ module.exports = function draw(gd) {
231
231
width : opts . width + constants . scrollBarWidth
232
232
} ) ;
233
233
234
+ if ( gd . firstRender ) {
235
+ // Move scrollbar to starting position
236
+ scrollBar . call (
237
+ Drawing . setRect ,
238
+ opts . width - ( constants . scrollBarWidth + constants . scrollBarMargin ) ,
239
+ constants . scrollBarMargin ,
240
+ constants . scrollBarWidth ,
241
+ constants . scrollBarHeight
242
+ ) ;
243
+ scrollBox . attr ( 'data-scroll' , 0 )
244
+ }
234
245
235
- // Move scrollbar to starting position
236
- scrollBar . call (
237
- Drawing . setRect ,
238
- opts . width - ( constants . scrollBarWidth + constants . scrollBarMargin ) ,
239
- constants . scrollBarMargin ,
240
- constants . scrollBarWidth ,
241
- constants . scrollBarHeight
242
- ) ;
246
+ scrollHandler ( 0 , scrollheight ) ;
243
247
244
248
legend . on ( 'wheel' , null ) ;
245
249
@@ -249,33 +253,24 @@ module.exports = function draw(gd) {
249
253
scrollHandler ( e . deltaY / 20 , scrollheight ) ;
250
254
} )
251
255
252
- scrollBar . on ( 'mousedown' , null ) ;
253
-
254
- scrollBar . on ( 'mousedown' , function ( ) {
255
- var e = d3 . event ;
256
- e . preventDefault ( ) ;
257
-
258
- function mMove ( e ) {
259
- if ( e . buttons === 1 ) {
260
- scrollHandler ( e . movementY , scrollheight ) ;
261
- }
262
- }
263
-
264
- function mUp ( ) {
265
- scrollBar . node ( ) . removeEventListener ( 'mousemove' , mMove ) ;
266
- window . removeEventListener ( 'mouseup' , mUp ) ;
267
- }
268
-
269
- window . addEventListener ( 'mousemove' , mMove ) ;
270
- window . addEventListener ( 'mouseup' , mUp ) ;
271
- } ) ;
256
+ scrollBar . on ( ".drag" , null ) ;
257
+ scrollBox . on ( ".drag" , null ) ;
258
+ var drag = d3 . behavior . drag ( )
259
+ . on ( 'drag' , function ( d ) {
260
+ scrollHandler ( d3 . event . dy , scrollheight ) ;
261
+ } ) ;
262
+
263
+ scrollBar . call ( drag ) ;
264
+ scrollBox . call ( drag ) ;
265
+
272
266
}
267
+
273
268
274
269
function scrollHandler ( delta , scrollheight ) {
275
270
276
271
var scrollBarTrack = scrollheight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
277
272
translateY = scrollBox . attr ( 'data-scroll' ) ,
278
- scrollBoxY = Lib . constrain ( translateY - delta , Math . min ( scrollheight - opts . height , 0 ) , 0 ) ,
273
+ scrollBoxY = Lib . constrain ( translateY - delta , scrollheight - opts . height , 0 ) ,
279
274
scrollBarY = - scrollBoxY / ( opts . height - scrollheight ) * scrollBarTrack + constants . scrollBarMargin ;
280
275
281
276
scrollBox . attr ( 'data-scroll' , scrollBoxY ) ;
0 commit comments