Skip to content

Commit

Permalink
fix(Indexes): 修复 H5 下索引位置错落的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
koppthe committed Dec 13, 2018
1 parent 04909f0 commit d7d2b5c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/components/indexes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ export default class AtIndexes extends AtComponent {
super(...arguments)
this.state = {
targetView: '',
targetOffsetTop: 0
scrollTop: 0
}
// 右侧导航高度
this.menuHeight = 0
// 右侧导航距离顶部高度
this.startTop = 0
this.itemHeight = 0
this.currentIndex = -1
this._scrollTop = 0
}

handleClick () {
Expand Down Expand Up @@ -91,10 +92,11 @@ export default class AtIndexes extends AtComponent {
} else if (env === Taro.ENV_TYPE.WEB) {
// web环境
const bodyOffsetTop = this.indexesRef.vnode.dom.getBoundingClientRect().top
// 目标节点offsetTop
const targetOffsetTop = this.listRef.vnode.dom.childNodes[i].getBoundingClientRect().top
const targetScrollTop = this._scrollTop + targetOffsetTop - bodyOffsetTop

this.setState({
targetOffsetTop: targetOffsetTop - bodyOffsetTop
scrollTop: targetScrollTop
})
}
if (this.props.isShowToast) {
Expand Down Expand Up @@ -142,6 +144,11 @@ export default class AtIndexes extends AtComponent {

getMenuRef = node => (this.menuRef = node)

handleScroll (ev) {
const { scrollTop } = ev.detail
this._scrollTop = scrollTop
}

render () {
const {
className,
Expand Down Expand Up @@ -187,8 +194,9 @@ export default class AtIndexes extends AtComponent {
className='at-indexes__body'
scrollY
scrollWithAnimation={animation}
scrollTop={this.state.targetOffsetTop}
scrollTop={this.state.scrollTop}
scrollIntoView={this.state.targetView}
onScroll={this.handleScroll.bind(this)}
ref={this.getListRef}
>
<View
Expand Down

0 comments on commit d7d2b5c

Please sign in to comment.