Skip to content

Commit a20dee9

Browse files
committed
Fixes scrollToIndex issue
1 parent 1eff6c7 commit a20dee9

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/ParallaxSwiper.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class ParallaxSwiper extends Component {
1616

1717
componentDidMount() {
1818
const { scrollToIndex } = this.props;
19+
1920
if (scrollToIndex) {
20-
this.scrollToIndex(scrollToIndex);
21+
setTimeout(() => {
22+
this.scrollToIndex(scrollToIndex, false);
23+
});
2124
}
2225
}
2326

@@ -41,26 +44,22 @@ class ParallaxSwiper extends Component {
4144
this.setState({ width, height });
4245
};
4346

44-
scrollToIndex(i) {
47+
scrollToIndex(index, animated = true) {
4548
const { vertical, dividerWidth, animatedValue } = this.props;
46-
47-
const index = vertical
48-
? i * this.state.height
49-
: i * (this.state.width + dividerWidth);
49+
const pageWidth = this.state.width + dividerWidth;
50+
const pageHeight = this.state.height;
51+
const scrollOffset = vertical ? index * pageHeight : index * pageWidth;
5052

5153
if (!this.animatedScrollViewHasScrolled) {
52-
animatedValue.setValue(index);
54+
animatedValue.setValue(scrollOffset);
55+
this.animatedScrollViewHasScrolled = true;
5356
}
5457

5558
this.animatedScrollView._component.scrollTo({
56-
x: vertical ? 0 : index,
57-
y: vertical ? index : 0,
58-
animated: true,
59+
x: vertical ? 0 : scrollOffset,
60+
y: vertical ? scrollOffset : 0,
61+
animated,
5962
});
60-
61-
if (!this.animatedScrollViewHasScrolled) {
62-
this.animatedScrollViewHasScrolled = true;
63-
}
6463
}
6564

6665
animatedScrollViewHasScrolled = false;

0 commit comments

Comments
 (0)