Skip to content

Commit

Permalink
fix(taro-components): Swiper组件动态传递current无法跳转到指定item,close #639
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjinjin0731 authored and luckyadam committed Nov 8, 2018
1 parent d008756 commit b317e3b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/taro-components/src/components/swiper/swiper.js
Expand Up @@ -55,9 +55,9 @@ class Swiper extends Nerv.Component {

componentWillReceiveProps (nextProps) {
this.updateContainerBox(nextProps.children)
const { interval, autoplay, circular } = nextProps
const { interval, autoplay, circular, current } = nextProps
this.pauseAutoPlay()
// this.updateCurrentIndex(current)
this.updateCurrentIndex(current)
if (!circular) {
this.computedChangeContainer()
}
Expand Down Expand Up @@ -106,20 +106,23 @@ class Swiper extends Nerv.Component {

// 更新下标
updateCurrentIndex (currentIndex) {
let cur = currentIndex === this.props.children.length - 1 ? 0 : currentIndex
let tr = this.state.translate
let slideVal // 纵向还是横向滚动长度

if (currentIndex < 0) currentIndex = this.props.children.length - 1
if (currentIndex >= this.props.children.length) currentIndex = 0

if (!this.props.vertical) {
slideVal = this.state.containerWidth * Math.abs(currentIndex - this.state.currentIndex)
slideVal = this.state.containerWidth * (currentIndex - this.state.currentIndex)
} else {
slideVal = this.state.containerHeight * Math.abs(currentIndex - this.state.currentIndex)
slideVal = this.state.containerHeight * (currentIndex - this.state.currentIndex)
}

this.setState(
{
animating: true,
translate: tr - slideVal,
currentIndex: cur
currentIndex: currentIndex
},
() => {
setTimeout(() => {
Expand Down Expand Up @@ -419,7 +422,8 @@ class Swiper extends Nerv.Component {
vertical,
children,
circular,
duration
duration,
style
} = this.props
const cls = classNames('swiper__container', className, {
'swiper__container-vertical': vertical,
Expand Down Expand Up @@ -450,6 +454,7 @@ class Swiper extends Nerv.Component {
return (
<div
className={cls}
style={style}
ref={SwiperWp => {
this.SwiperWp = SwiperWp
}}
Expand Down

0 comments on commit b317e3b

Please sign in to comment.