2016-11-03 16:45:13 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
|
|
|
import getRequestAnimationFrame from '../_util/getRequestAnimationFrame';
|
|
|
|
|
|
|
|
export const reqAnimFrame = getRequestAnimationFrame();
|
|
|
|
|
|
|
|
export const easeInOutCubic = (t, b, c, d) => {
|
2016-11-09 14:22:38 +08:00
|
|
|
const cc = c - b;
|
|
|
|
t /= d / 2;
|
|
|
|
if (t < 1) {
|
|
|
|
return cc / 2 * t * t * t + b;
|
|
|
|
}
|
|
|
|
return cc / 2 * ((t -= 2) * t * t + 2) + b;
|
2016-11-03 16:45:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export function getDefaultTarget() {
|
|
|
|
return typeof window !== 'undefined' ?
|
|
|
|
window : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOffsetTop(element): number {
|
|
|
|
if (!element) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.getClientRects().length) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
|
|
|
if ( rect.width || rect.height ) {
|
|
|
|
const doc = element.ownerDocument;
|
|
|
|
const docElem = doc.documentElement;
|
|
|
|
return rect.top - docElem.clientTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect.top;
|
|
|
|
}
|
|
|
|
|
2016-11-30 10:10:31 +08:00
|
|
|
export type Section = {
|
|
|
|
top: number;
|
|
|
|
bottom: number;
|
|
|
|
section: any;
|
|
|
|
};
|
|
|
|
|
2017-02-10 16:34:10 +08:00
|
|
|
export function scrollTo(href, offsetTop = 0, target = getDefaultTarget, callback = () => {}) {
|
2016-11-09 14:22:38 +08:00
|
|
|
const scrollTop = getScroll(target(), true);
|
2016-11-14 15:43:55 +08:00
|
|
|
const targetElement = document.getElementById(href.substring(1));
|
2016-11-09 20:17:02 +08:00
|
|
|
if (!targetElement) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-10 16:34:10 +08:00
|
|
|
const eleOffsetTop = getOffsetTop(targetElement);
|
|
|
|
const targetScrollTop = scrollTop + eleOffsetTop - offsetTop;
|
2016-11-09 14:22:38 +08:00
|
|
|
const startTime = Date.now();
|
|
|
|
const frameFunc = () => {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const time = timestamp - startTime;
|
2016-11-09 20:17:02 +08:00
|
|
|
window.scrollTo(window.pageXOffset, easeInOutCubic(time, scrollTop, targetScrollTop, 450));
|
2016-11-09 14:22:38 +08:00
|
|
|
if (time < 450) {
|
|
|
|
reqAnimFrame(frameFunc);
|
2016-11-14 15:43:55 +08:00
|
|
|
} else {
|
|
|
|
callback();
|
2016-11-09 14:22:38 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
reqAnimFrame(frameFunc);
|
2016-11-15 15:33:13 +08:00
|
|
|
history.pushState(null, '', href);
|
2016-11-09 14:22:38 +08:00
|
|
|
}
|
2016-11-03 16:45:13 +08:00
|
|
|
|
|
|
|
class AnchorHelper {
|
|
|
|
private links: Array<string>;
|
|
|
|
private currentAnchor: HTMLElement | null;
|
2016-11-09 20:17:02 +08:00
|
|
|
private _activeAnchor: string;
|
2016-11-03 16:45:13 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.links = [];
|
|
|
|
this.currentAnchor = null;
|
2016-11-09 20:17:02 +08:00
|
|
|
this._activeAnchor = '';
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addLink(link) {
|
|
|
|
if (this.links.indexOf(link) === -1) {
|
|
|
|
this.links.push(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentActiveAnchor(): HTMLElement | null {
|
|
|
|
return this.currentAnchor;
|
2016-11-03 19:28:28 +08:00
|
|
|
}
|
2016-11-03 16:45:13 +08:00
|
|
|
|
|
|
|
setActiveAnchor(component) {
|
|
|
|
this.currentAnchor = component;
|
|
|
|
}
|
|
|
|
|
2017-02-10 16:34:10 +08:00
|
|
|
getCurrentAnchor(offsetTop: number = 0, bounds = 5) {
|
2016-11-03 16:45:13 +08:00
|
|
|
let activeAnchor = '';
|
2016-11-21 14:39:15 +08:00
|
|
|
if (typeof document === 'undefined') {
|
|
|
|
return activeAnchor;
|
|
|
|
}
|
|
|
|
|
2016-11-30 10:10:31 +08:00
|
|
|
const linksPositions = (this.links
|
|
|
|
.map(section => {
|
2016-11-14 15:43:55 +08:00
|
|
|
const target = document.getElementById(section.substring(1));
|
2017-02-10 16:34:10 +08:00
|
|
|
if (target && getOffsetTop(target) < offsetTop + bounds) {
|
2016-11-03 16:45:13 +08:00
|
|
|
const top = getOffsetTop(target);
|
2017-02-10 16:34:10 +08:00
|
|
|
if (top <= offsetTop + bounds) {
|
2016-11-30 10:10:31 +08:00
|
|
|
return {
|
|
|
|
section,
|
|
|
|
top,
|
|
|
|
bottom: top + target.clientHeight,
|
|
|
|
};
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-30 10:10:31 +08:00
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.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 '';
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
|
|
|
|
2017-02-10 16:34:10 +08:00
|
|
|
scrollTo(href, offsetTop, target = getDefaultTarget, callback = () => {}) {
|
|
|
|
scrollTo(href, offsetTop, target, callback);
|
2016-11-03 16:45:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:28:28 +08:00
|
|
|
export default AnchorHelper;
|