Skip to content

Commit 5819e40

Browse files
committed
Fix when scroll animate duration greater than debounce will cause scrolling flicking
1 parent 5c861fd commit 5819e40

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

public/js/syncscroll.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ var viewScrollThrottle = 10;
115115
var buildMapThrottle = 100;
116116

117117
var viewScrolling = false;
118-
var viewScrollingDebounce = 200;
119-
120118
var editScrolling = false;
121-
var editScrollingDebounce = 200;
122119

123120
ui.area.codemirrorScroll.on('scroll', _.throttle(syncScrollToView, editScrollThrottle));
124121
ui.area.view.on('scroll', _.throttle(syncScrollToEdit, viewScrollThrottle));
@@ -214,12 +211,8 @@ function buildMapInner(callback) {
214211
if (loaded && callback) callback();
215212
}
216213

217-
// sync view scroll progress to edit
218-
var viewScrollingTimeout = _.debounce(viewScrollingTimeoutInner, viewScrollingDebounce);
219-
220-
function viewScrollingTimeoutInner() {
221-
viewScrolling = false;
222-
}
214+
// sync view scroll progress to edit
215+
var viewScrollingTimer = null;
223216

224217
function syncScrollToEdit(e) {
225218
if (currentMode != modeType.both || !syncscroll) return;
@@ -280,21 +273,23 @@ function syncScrollToEdit(e) {
280273

281274
var posDiff = Math.abs(scrollInfo.top - posTo);
282275
var duration = posDiff / 50;
276+
duration = duration >= 100 ? duration : 100;
283277
ui.area.codemirrorScroll.stop(true, true).animate({
284278
scrollTop: posTo
285-
}, duration >= 100 ? duration : 100, "linear");
279+
}, duration, "linear");
286280

287281
viewScrolling = true;
288-
viewScrollingTimeout();
282+
clearTimeout(viewScrollingTimer);
283+
viewScrollingTimer = setTimeout(viewScrollingTimeoutInner, duration * 1.2);
289284
}
290285

291-
// sync edit scroll progress to view
292-
var editScrollingTimeout = _.debounce(editScrollingTimeoutInner, editScrollingDebounce);
293-
294-
function editScrollingTimeoutInner() {
295-
editScrolling = false;
286+
function viewScrollingTimeoutInner() {
287+
viewScrolling = false;
296288
}
297289

290+
// sync edit scroll progress to view
291+
var editScrollingTimer = null;
292+
298293
function syncScrollToView(event, _lineNo) {
299294
if (currentMode != modeType.both || !syncscroll) return;
300295
if (preventSyncScrollToView) {
@@ -336,10 +331,16 @@ function syncScrollToView(event, _lineNo) {
336331

337332
var posDiff = Math.abs(ui.area.view.scrollTop() - posTo);
338333
var duration = posDiff / 50;
334+
duration = duration >= 100 ? duration : 100;
339335
ui.area.view.stop(true, true).animate({
340336
scrollTop: posTo
341-
}, duration >= 100 ? duration : 100, "linear");
337+
}, duration, "linear");
342338

343339
editScrolling = true;
344-
editScrollingTimeout();
340+
clearTimeout(editScrollingTimer);
341+
editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.2);
342+
}
343+
344+
function editScrollingTimeoutInner() {
345+
editScrolling = false;
345346
}

0 commit comments

Comments
 (0)