Skip to content Skip to sidebar Skip to footer

Scroll Other Scrollbars With Scrollbar

i have 3 divs with scrollbars. If i scroll in div 1 i want to scroll div 2 and 3 in the opposite direction. The distance scrolled should be half the distance of div 1. This is what

Solution 1:

No disrespect to @EmreErkan and @Simon for their effort. Here's a no-click version of this.

var $boxes = $("#textBox1,#textBox2,#textBox3"),
    active;

$boxes.scrollTop(150);

// set initial scrollTop values
updateScrollPos();

// bind mouseenter: // 1) find which panel is active // 2) update scrollTop values

$boxes.mouseenter(function () {
    active = this.id;
    updateScrollPos();
});

// bind scroll for all boxes
$boxes.scroll(function (e) {

    $this = $(this);

    // check to see if we are dealing with the active box// if true then set scrolltop of other boxes relative to the active boxif(this.id == active){

        var $others = $boxes.not($this),
            offset = $this.scrollTop()-$this.data("scroll"),
            half_offset = offset / 2;

        $others.each(function(){
            $this = $(this);
            $this.scrollTop($this.data("scroll") - half_offset);
        });
    }

});

// utility function: // assign scrollTop values element's data attributes (data-scroll)

function updateScrollPos() {
    $boxes.each(function(){
        $this = $(this);
        $this.data("scroll",$this.scrollTop());
    });
}

Fiddle

Solution 2:

You can use a variable to determine active textbox with .mousedown() and do the trick if it's active;

var activeScroll = '';

$("#textBox1").on('mousedown focus mouseenter', function () {
    activeScroll = 'scroll1';
}).scroll(function () {
    if (activeScroll == 'scroll1') {
        console.log("scroll 1");
        var offset = $("#textBox1").scrollTop() - scrollPosTBox1;
        var half_offset = offset / 2.0;

        $("#textBox2").scrollTop(scrollPosTBox2 - half_offset);
        $("#textBox3").scrollTop(scrollPosTBox3 - half_offset);
    }
});

You can check your updated jsFiddle here.

Solution 3:

Finally got a dynamic solution for this, was more complex than I thought but I think I got it:

http://jsfiddle.net/XmYh5/14/

var initialTop = 150,
    factor = 2;

    $(".textBox")
        .addClass('disabled')
        .scrollTop(initialTop)
        .on('scroll', function () {
            var $this = $(this);

            if(!$this.is('.disabled')) {
                this.lastOffset = this.lastOffset || initialTop;

                var offset = $this.scrollTop(),
                    step = (offset - this.lastOffset) / factor;

                $this.siblings().each( function() {
                    var $this = $(this),
                        offset = $this.scrollTop() - step;

                    $this.scrollTop(offset);
                    this.lastOffset = offset;
                });

                this.lastOffset = offset;
            }
        })
        .on('mouseenter', function() {
            $(this).removeClass('disabled').siblings().addClass('disabled');
        });

Post a Comment for "Scroll Other Scrollbars With Scrollbar"