Skip to content

Instantly share code, notes, and snippets.

@dmarchuk
Last active July 20, 2018 13:44
Show Gist options
  • Save dmarchuk/fe50add8c6c6c2d6f3773656eac4ce8b to your computer and use it in GitHub Desktop.
Save dmarchuk/fe50add8c6c6c2d6f3773656eac4ce8b to your computer and use it in GitHub Desktop.
Synchronizing scroll of two elements
var scrollAreas = $('#content, .meltdown_preview');
var textArea = $('#content');
var previewArea = $('.meltdown_preview');
var timer;
var sync = function () {
var percentage, x;
if ($(this).attr('class') == 'meltdown_preview') {
textArea.unbind("scroll");
percentage = this.scrollTop / (this.scrollHeight - this.offsetHeight);
x = percentage * (textArea.get(0).scrollHeight - textArea.get(0).offsetHeight);
textArea.scrollTop(x);
if (typeof(timer) !== 'undefined')
clearTimeout(timer);
timer = setTimeout(function () {
textArea.scroll(sync)
}, 200)
} else {
previewArea.unbind("scroll");
percentage = this.scrollTop / (this.scrollHeight - this.offsetHeight);
x = percentage * (previewArea.get(0).scrollHeight - previewArea.get(0).offsetHeight);
previewArea.scrollTop(x);
if (typeof(timer) !== 'undefined')
clearTimeout(timer);
timer = setTimeout(function () {
previewArea.scroll(sync)
}, 200)
}
}
scrollAreas.on('scroll', sync);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment