简体   繁体   中英

vertical scroll two tables at the same time

I have two tables that must scroll together:

$('.vscroll').on('scroll', function (e) {
    divTable1.scrollTop = e.scrollTop;
    divTable2.scrollTop = e.scrollTop;

There's a little lag issue though. Table1 scrolls milliseconds before Table2.

I know scrollTop fires the scroll event, but is there a way to delay the scrolling of Table1 until Table2's scrollTop is also set?

Try using a setTimeout to trigger the scrolls, and then return false to cancel the original scrollevent:

var ignoreEvent = false;

$(".vscroll").on('scroll', function (e) {
    if (!ignoreEvent) {
        setTimeout(function() {
            ignoreEvent = true;
            table1.scrollTop = e.scrollTop;
            table2.scrolLTop = e.scrollTop;
        }, 100);
    }

    ignoreEvent = false;
    return false; // cancels the original scroll event.
}

I'm using div s instead of table s but you get the idea

$("div").on("scroll",function(){
    $("div:not(this)").scrollTop($(this).scrollTop());
});

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM