1 回答
TA贡献1818条经验 获得超11个赞
像这样的东西?
function syncScroll(elements) {
function getScrollPositions() {
return elements.map((el) => ({
scrollLeft: el.scrollLeft,
scrollTop: el.scrollTop,
}));
}
let scrollPositions = getScrollPositions();
elements.forEach((el) =>
el.addEventListener("scroll", () => {
let currentPositions = getScrollPositions();
for (let [i, position] of currentPositions.entries()) {
for (let dim of ["scrollLeft", "scrollTop"]) {
if (position[dim] != scrollPositions[i][dim]) {
for (let element of elements) {
element[dim] = position[dim];
}
}
}
}
scrollPositions = currentPositions;
})
);
}
syncScroll([T1Obj, T2Obj, T3Obj]);
https://jsfiddle.net/xowvp3a9/
还要记住,滚动的元素不是#T1, #T2, #T3,而是其中的一个 div 。您可以使用滚动标签查看它。
是因为:
const T1Obj = document.querySelector('#T1 .wtHolder');
const T2Obj = document.querySelector('#T2 .wtHolder');
const T3Obj = document.querySelector('#T3 .wtHolder');
添加回答
举报