Skip to content

Commit

Permalink
Merge pull request leecade#831 from alattanzio/master
Browse files Browse the repository at this point in the history
added scrollTo
  • Loading branch information
ArrayZoneYour authored Jun 26, 2019
2 parents 5a6a9f5 + cff749e commit ee6c352
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,47 @@ export default class extends Component {
}
}

/**
* Scroll to index
* @param {number} index page
* @param {bool} animated
*/

scrollTo = (index, animated = true) => {
if (this.internals.isScrolling || this.state.total < 2 || index == this.state.index) return

const state = this.state
const diff = this.state.index + (index - this.state.index)

let x = 0
let y = 0
if (state.dir === 'x') x = diff * state.width
if (state.dir === 'y') y = diff * state.height

if (Platform.OS !== 'ios') {
this.scrollView && this.scrollView[animated ? 'setPage' : 'setPageWithoutAnimation'](diff)
} else {
this.scrollView && this.scrollView.scrollTo({ x, y, animated })
}

// update scroll state
this.internals.isScrolling = true
this.setState({
autoplayEnd: false
})

// trigger onScrollEnd manually in android
if (!animated || Platform.OS !== 'ios') {
setImmediate(() => {
this.onScrollEnd({
nativeEvent: {
position: diff
}
})
})
}
}

scrollViewPropOverrides = () => {
const props = this.props
let overrides = {}
Expand Down

0 comments on commit ee6c352

Please sign in to comment.