实现同步代码,这是一个横向滚动条的,纵向的直接修改监听的滚动条scrollLeft为scrollTop
setScrollRule() {
let that = this;
this.one = this.$refs.one.$refs.multipleTable.$refs.bodyWrapper;
this.two = this.$refs.two.$refs.multipleTable.$refs.bodyWrapper;
this.one.addEventListener(
"scroll",
_.throttle(
function () {
that.fn1();
},
10,
{
leading: true,
trailing: false,
}
)
);
this.two.addEventListener(
"scroll",
_.throttle(
function () {
that.fn2();
},
10,
{
leading: true,
trailing: false,
}
)
);
},
fn1() {
this.two.scrollLeft = this.one.scrollLeft;
setTimeout(() => {
this.two.scrollLeft = this.one.scrollLeft;
}, 10);
},
fn2() {
this.one.scrollLeft = this.two.scrollLeft;
setTimeout(() => {
this.one.scrollLeft = this.two.scrollLeft;
}, 10);
},
需要引入的节流函数
import _ from "lodash";
结束后移除监听时间
this.one.removeEventListener("scroll", this.fn1);
this.two.removeEventListener("scroll", this.fn2);