Is it possible to synchronize two scrolls?
jQuery jScrollPane Synchronize Scroll
2.7k views Asked by velozyrapthor At
5
There are 5 answers
1
On
Add this function to your code:
jQuery.fn.synchronizeScroll = function() {
var elements = this;
if (elements.length <= 1) return;
elements.scroll(
function() {
var left = $(this).scrollLeft();
var top = $(this).scrollTop();
elements.each(
function() {
if ($(this).scrollLeft() != left) $(this).scrollLeft(left);
if ($(this).scrollTop() != top) $(this).scrollTop(top);
}
);
});
}
Then, you can just synchronize all the scrollbars within an element like so:
$(“jqueryselectorgoeshere”).synchronizeScroll();
0
On
The answer of velozyrapthor is correct and working. The only thing I added to my code is the 'click on the track' event. When you click on the track it jumps to position.
Because my solution was involving a horizontal scroll bar, I changed the events to the horizontal ones.
this is my code:
$c1=$('#c1').jScrollPane();
$c2=$('#c2').jScrollPane();
//sync the two boxes to scroll together
//bind the scroll to c1
$("#c1").bind(
'jsp-scroll-x',
function(event, scrollPositionX, isAtTop, isAtBottom)
{
if($(this).find(".jspDrag").hasClass("jspActive"))
{
$c2.data('jsp').scrollToX(scrollPositionX)
}
}
);
//bind the jump when clicking on the track
$("#c1").bind('mousedown',function()
{
$c2.data('jsp').scrollToX($c1.data('jsp').getContentPositionX());
});
//bind the scroll to c2
$("#c2").bind(
'jsp-scroll-x',
function(event, scrollPositionX, isAtTop, isAtBottom)
{
if($(this).find(".jspDrag").hasClass("jspActive"))
{
$c1.data('jsp').scrollToX(scrollPositionX)
}
}
);
//bind the jump when clicking on the track
$("#c2").bind('mousedown',function()
{
$c1.data('jsp').scrollToX($c2.data('jsp').getContentPositionX());
});
0
On
/* This is my solution. thank both*/
$c1= $("#c1").jScrollPane();
$c2= $("#c2").jScrollPane();
$("#c1").bind(
'jsp-scroll-y',
function(event, scrollPositionY, isAtTop, isAtBottom)
{
if($(this).find(".jspDrag").hasClass("jspActive")){
$c2.data('jsp').scrollToY(scrollPositionY)
console.log('Handle jsp-scroll-y', this,
'scrollPositionY=', scrollPositionY,
'isAtTop=', isAtTop,
'isAtBottom=', isAtBottom);
}
}
)
$("#c2").bind(
'jsp-scroll-y',
function(event, scrollPositionY, isAtTop, isAtBottom)
{
if($(this).find(".jspDrag").hasClass("jspActive")){
$c1.data('jsp').scrollToY(scrollPositionY)
console.log('Handle jsp-scroll-y', this,
'scrollPositionY=', scrollPositionY,
'isAtTop=', isAtTop,
'isAtBottom=', isAtBottom);
}
}
)
0
On
Here's my solution that will make a sticky column, and a sticky row. Set overflow: hidden on #rowHeader, #columnHeader
$("#dataTable").bind('jsp-scroll-y', function(event, scrollPositionY) {
$("#rowHeader").scrollTop(scrollPositionY);
}).bind('jsp-scroll-x',function(event, scrollPositionX) {
$("#columnHeader").scrollLeft(scrollPositionX);
}).jScrollPane();
It should be pretty easy to do so by binding to the
jsp-scroll-yevent and then calling thescrollToYAPI method.Or, since jScrollPane also dispatches plain
scrollevents you could adapt Peter Of The Corn's solution by usinggetContentPositionYinstead ofscrollTop()andscrollToYinstead ofscrollTop(value)(and likewise for the left/ top properties)