fix: Anchor issue while scrolling window fast (#4054)

*  Anchor issue while scrolling window fast

 + close #4503

* another implement...
pull/4073/head
陆离 8 years ago committed by Benjy Cui
parent 5db5fed7b9
commit ee2654dbe1

@ -37,6 +37,12 @@ export function getOffsetTop(element): number {
return rect.top; return rect.top;
} }
export type Section = {
top: number;
bottom: number;
section: any;
};
export function scrollTo(href, target = getDefaultTarget, callback = () => {}) { export function scrollTo(href, target = getDefaultTarget, callback = () => {}) {
const scrollTop = getScroll(target(), true); const scrollTop = getScroll(target(), true);
const targetElement = document.getElementById(href.substring(1)); const targetElement = document.getElementById(href.substring(1));
@ -91,18 +97,28 @@ class AnchorHelper {
return activeAnchor; return activeAnchor;
} }
this.links.forEach(section => { const linksPositions = (this.links
.map(section => {
const target = document.getElementById(section.substring(1)); const target = document.getElementById(section.substring(1));
if (target) { if (target && getOffsetTop(target) < bounds) {
const top = getOffsetTop(target); const top = getOffsetTop(target);
const bottom = top + target.clientHeight; if (top <= bounds) {
if ((top <= bounds) && (bottom >= -bounds)) { return {
activeAnchor = section; section,
top,
bottom: top + target.clientHeight,
};
} }
} }
}); return null;
this._activeAnchor = activeAnchor || this._activeAnchor; })
return this._activeAnchor; .filter(section => section !== null) as Array<Section>);
if (linksPositions.length) {
const maxSection = linksPositions.reduce((prev, curr) => curr.top > prev.top ? curr : prev);
return maxSection.section;
}
return '';
} }
scrollTo(href, target = getDefaultTarget, callback = () => {}) { scrollTo(href, target = getDefaultTarget, callback = () => {}) {

Loading…
Cancel
Save