[bugfix] IndexBar: activeAnchor bug fixes (#3832)

修复 sticky 设为 false 时activeAnchor失效的bug;
修复 this.scroller 不为 window 时activeAnchor错乱的bug;
This commit is contained in:
cesc fabregas
2019-07-12 09:49:10 +08:00
committed by neverland
parent 9aab46f3a2
commit c88bab21c1
2 changed files with 19 additions and 18 deletions

View File

@@ -77,11 +77,9 @@ export default createComponent({
methods: {
onScroll() {
if (!this.sticky) {
return;
}
const scrollTop = getScrollTop(this.scroller);
const scrollTop = this.scroller === window
? getScrollTop(this.scroller)
: 0;
const rects = this.children.map(item => ({
height: item.height,
top: getElementTop(item.$el)
@@ -90,18 +88,21 @@ export default createComponent({
const active = this.getActiveAnchorIndex(scrollTop, rects);
this.activeAnchorIndex = this.indexList[active];
this.children.forEach((item, index) => {
if (index === active) {
item.active = true;
item.top = Math.max(this.stickyOffsetTop, rects[index].top - scrollTop);
} else if (index === active - 1) {
const activeItemTop = rects[active].top - scrollTop;
item.active = activeItemTop > 0;
item.top = activeItemTop - item.height;
} else {
item.active = false;
}
});
if (this.sticky) {
this.children.forEach((item, index) => {
if (index === active) {
item.active = true;
item.top = Math.max(this.stickyOffsetTop, rects[index].top - scrollTop);
} else if (index === active - 1) {
const activeItemTop = rects[active].top - scrollTop;
item.active = activeItemTop > 0;
item.top = activeItemTop - item.height;
} else {
item.active = false;
}
});
}
},
getActiveAnchorIndex(scrollTop, rects) {