I want to have 2 divs sized to a particular width (i.e. 500px). One above the other aligned horizontally.
The top box should hide its scroll bar, the bottom should show a scroll bar and when the user scrolls I'd like the offset of the top box to change to the value of the bottom box. So that when the bottom DIV scrolls horizontally it appears that the top DIV is also scrolling in unison.
I'm happy to do this in Jquery if it makes the process easier.
$('#bottom').on('scroll', function () {
$('#top').scrollTop($(this).scrollTop());
});
Here we are using .scrollTop()
for all it's worth, getting the scrollTop
value from the element with scroll-bars, and setting the scrollTop
for the other element to sync their scroll positions: http://api.jquery.com/scrollTop
This assumes that your bottom element has an ID of bottom
and your top element has an ID of top
.
You can hide the scroll-bars for the top
element using CSS:
#top {
overflow : hidden;
}
Here is a demo: http://jsfiddle.net/sgcer/1884/
I suppose I've never really had a reason to do this, but it looks pretty cool in action.
I know this is an old thread, but maybe this will help someone. In case if you need to synchronize scrolling in double direction, it's not good enough to just handle both containers' scrolling events and set the scroll value, because then scrolling events are getting into cycle and the scrolling is not smooth (try to scroll vertically by a mouse wheel an example given by Hightom).
Here is an example of how you can check if you are already synchronizing the scroll:
var isSyncingLeftScroll = false; var isSyncingRightScroll = false; var leftDiv = document.getElementById('left'); var rightDiv = document.getElementById('right'); leftDiv.onscroll = function() { if (!isSyncingLeftScroll) { isSyncingRightScroll = true; rightDiv.scrollTop = this.scrollTop; } isSyncingLeftScroll = false; } rightDiv.onscroll = function() { if (!isSyncingRightScroll) { isSyncingLeftScroll = true; leftDiv.scrollTop = this.scrollTop; } isSyncingRightScroll = false; } .container { width: 200px; height: 500px; overflow-y: auto; } .leftContainer { float: left; } .rightContainer { float: right; }
Here is the fiddle.
I've been looking for a double direction solution and thanks to you all, your contibutions helped me doing this :
$('#cells').on('scroll', function () {
$('#hours').scrollTop($(this).scrollTop());
$('#days').scrollLeft($(this).scrollLeft());});
See on JSFiddle : https://jsfiddle.net/sgcer/1274/
Hope it's help someone someday :-)
Alright, so I evaluated all of the options presented here, and they all had limitations of one form or another:
The accepted answer suffers from known issues using the mouse scroll wheel.
The next highest upvote does not have scroll wheel issues, but only works for two known elements. If you need more elements, it's not scalable.
The only solution that showed promise was Lacho's, but there were known element references in the code that were not included in the snippet. On the plus side, it had the structure needed for performance, and it's in TypeScript.
I leveraged that to create a reusable version that can work with an unlimited number of elements, and it doesn't require JQuery.
function scrollSync(selector) {
let active = null;
document.querySelectorAll(selector).forEach(function(element) {
element.addEventListener("mouseenter", function(e) {
active = e.target;
});
element.addEventListener("scroll", function(e) {
if (e.target !== active) return;
document.querySelectorAll(selector).forEach(function(target) {
if (active === target) return;
target.scrollTop = active.scrollTop;
target.scrollLeft = active.scrollLeft;
});
});
});
}
//RWM: Call the function on the elements you need synced.
scrollSync(".scrollSync");
You can check out the JSFiddle for this here: http://jsfiddle.net/gLa2ndur/3. You can see it uses both horizontal and vertical scrolling examples.
The only known limitation is that it might not work on divs of different sizes. I'm sure someone can work on incorporating Andrew's work into this if your use-case finds that necessary (mine does not).
I hope this is helpful to someone!
Another solution that prevents this looping problem and achieves a smooth scroll. This makes sure that only the focused element gets the scroll event.
let activePre: HTMLPreElement = null;
document.querySelectorAll(".scrollable-pre").forEach(function(pre: HTMLPreElement) {
pre.addEventListener("mouseenter", function(e: MouseEvent) {
let pre = e.target as HTMLPreElement;
activePre = pre;
});
pre.addEventListener("scroll", function(e: MouseEvent) {
let pre = e.target as HTMLPreElement;
if (activePre !== pre) {
return;
}
if (pre !== versionBasePre) {
versionBasePre.scrollTop = pre.scrollTop;
versionBasePre.scrollLeft = pre.scrollLeft;
}
if (pre !== versionCurrentPre) {
versionCurrentPre.scrollTop = pre.scrollTop;
versionCurrentPre.scrollLeft = pre.scrollLeft;
}
});
});
Thanks to the answers above, I made a mixed solution that works preferentially for me:
var isLeftScrollTopCalled = false;
$('#leftElement').scroll(function (e) {
if (isRightScrollTopCalled) {
return isRightScrollTopCalled = false;
}
$('#rightElement').scrollTop($(this).scrollTop());
isLeftScrollTopCalled = true;
});
var isRightScrollTopCalled = false;
$('#rightElement').scroll(function (e) {
if (isLeftScrollTopCalled) {
return isLeftScrollTopCalled = false;
}
$('#leftElement').scrollTop($(this).scrollTop());
isRightScrollTopCalled = true;
});
I noticed that his question was pretty old, but I thought I could leave a jQuery implementation that works much better than using timers. Here, I am using two event listeners like the previously used solutions but, this will use proportions/percentage to sync scrolls for two differently sized div boxes. You can apply this same knowledge to get the position the scrollbars to a Vanilla JS solution. This will make the scrolling between the two divs much smoother.
/** Scroll sync between editor and preview **/
// Locks so that only one pane is in control of scroll sync
var eScroll = false;
var pScroll = false;
// Set the listener to scroll
this.edit.on('scroll', function() {
if(eScroll) { // Lock the editor pane
eScroll = false;
return;
}
pScroll = true; // Enable the preview scroll
// Set elements to variables
let current = self.edit.get(0);
let other = self.preview.get(0);
// Calculate the position of scroll position based on percentage
let percentage = current.scrollTop / (current.scrollHeight - current.offsetHeight);
// Set the scroll position on the other pane
other.scrollTop = percentage * (other.scrollHeight - other.offsetHeight);
});
this.preview.on('scroll', function() {
if(pScroll) { // Lock the preview pane
pScroll = false;
return;
}
eScroll = true; // Enable the editor scroll
// Set elements to variables
let current = self.preview.get(0);
let other = self.edit.get(0);
// Calculate the position of scroll position based on percentage
let percentage = current.scrollTop / (current.scrollHeight - current.offsetHeight);
// Set the scroll position on the other pane
other.scrollTop = percentage * (other.scrollHeight - other.offsetHeight);
});
Success story sharing
scrollTop
withscrollLeft
for both instances?scrollLeft
instead ofscrollTop
: jsfiddle.net/sgcer/1overflow
for#bottom
should really beoverflow-y
to avoid getting the x-scrollbar at the bottom of the div with scroll controls.