Last active
July 20, 2018 13:44
-
-
Save dmarchuk/fe50add8c6c6c2d6f3773656eac4ce8b to your computer and use it in GitHub Desktop.
Synchronizing scroll of two elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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