Skip to content

Commit 7be6497

Browse files
shenqihuiyesmeck
authored andcommitted
Fix Table scroll sync
Fix ant-design/ant-design#6537
1 parent 242e9db commit 7be6497

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

src/Table.jsx

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,7 @@ export default class Table extends React.Component {
497497
className={`${prefixCls}-header`}
498498
ref={fixed ? null : 'headTable'}
499499
style={headStyle}
500-
onMouseOver={this.detectScrollTarget}
501-
onTouchStart={this.detectScrollTarget}
502-
onScroll={this.handleBodyScroll}
500+
onScroll={this.handleBodyScrollLeft}
503501
>
504502
{renderTable(true, false)}
505503
</div>
@@ -512,8 +510,6 @@ export default class Table extends React.Component {
512510
className={`${prefixCls}-body`}
513511
style={bodyStyle}
514512
ref="bodyTable"
515-
onMouseOver={this.detectScrollTarget}
516-
onTouchStart={this.detectScrollTarget}
517513
onScroll={this.handleBodyScroll}
518514
>
519515
{renderTable(!useFixedHeader)}
@@ -539,8 +535,6 @@ export default class Table extends React.Component {
539535
className={`${prefixCls}-body-inner`}
540536
style={innerBodyStyle}
541537
ref={refName}
542-
onMouseOver={this.detectScrollTarget}
543-
onTouchStart={this.detectScrollTarget}
544538
onScroll={this.handleBodyScroll}
545539
>
546540
{renderTable(!useFixedHeader)}
@@ -675,46 +669,50 @@ export default class Table extends React.Component {
675669
return typeof this.findExpandedRow(record, index) !== 'undefined';
676670
}
677671

678-
detectScrollTarget = (e) => {
679-
if (this.scrollTarget !== e.currentTarget) {
680-
this.scrollTarget = e.currentTarget;
681-
}
682-
}
683-
684672
hasScrollX() {
685673
const { scroll = {} } = this.props;
686674
return 'x' in scroll;
687675
}
688676

689-
handleBodyScroll = (e) => {
690-
// Prevent scrollTop setter trigger onScroll event
691-
// http://stackoverflow.com/q/1386696
692-
if (e.target !== this.scrollTarget) {
693-
return;
694-
}
677+
handleBodyScrollLeft = (e) => {
678+
const target = e.target;
695679
const { scroll = {} } = this.props;
696-
const { headTable, bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this.refs;
697-
if (scroll.x && e.target.scrollLeft !== this.lastScrollLeft) {
698-
if (e.target === bodyTable && headTable) {
699-
headTable.scrollLeft = e.target.scrollLeft;
700-
} else if (e.target === headTable && bodyTable) {
701-
bodyTable.scrollLeft = e.target.scrollLeft;
680+
const { headTable, bodyTable } = this.refs;
681+
if (target.scrollLeft !== this.lastScrollLeft && scroll.x) {
682+
if (target === bodyTable && headTable) {
683+
headTable.scrollLeft = target.scrollLeft;
684+
} else if (target === headTable && bodyTable) {
685+
bodyTable.scrollLeft = target.scrollLeft;
702686
}
703-
this.setScrollPositionClassName(e.target);
687+
this.setScrollPositionClassName(target);
704688
}
705-
if (scroll.y) {
706-
if (fixedColumnsBodyLeft && e.target !== fixedColumnsBodyLeft) {
707-
fixedColumnsBodyLeft.scrollTop = e.target.scrollTop;
689+
// Remember last scrollLeft for scroll direction detecting.
690+
this.lastScrollLeft = target.scrollLeft;
691+
}
692+
693+
handleBodyScrollTop = (e) => {
694+
const target = e.target;
695+
const { scroll = {} } = this.props;
696+
const { headTable, bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this.refs;
697+
if (target.scrollTop !== this.lastScrollTop && scroll.y && target !== headTable) {
698+
const scrollTop = target.scrollTop;
699+
if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
700+
fixedColumnsBodyLeft.scrollTop = scrollTop;
708701
}
709-
if (fixedColumnsBodyRight && e.target !== fixedColumnsBodyRight) {
710-
fixedColumnsBodyRight.scrollTop = e.target.scrollTop;
702+
if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) {
703+
fixedColumnsBodyRight.scrollTop = scrollTop;
711704
}
712-
if (bodyTable && e.target !== bodyTable) {
713-
bodyTable.scrollTop = e.target.scrollTop;
705+
if (bodyTable && target !== bodyTable) {
706+
bodyTable.scrollTop = scrollTop;
714707
}
715708
}
716-
// Remember last scrollLeft for scroll direction detecting.
717-
this.lastScrollLeft = e.target.scrollLeft;
709+
// Remember last scrollTop for scroll direction detecting.
710+
this.lastScrollTop = target.scrollTop;
711+
}
712+
713+
handleBodyScroll = (e) => {
714+
this.handleBodyScrollLeft(e);
715+
this.handleBodyScrollTop(e);
718716
}
719717

720718
handleRowHover = (isHover, key) => {

0 commit comments

Comments
 (0)