Skip to content

Commit

Permalink
feat: back top #ASM-14132
Browse files Browse the repository at this point in the history
  • Loading branch information
chencc committed Jul 21, 2023
1 parent b392526 commit f3603d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cold-schools-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alauda/ui': patch
---

back to top time optimization
23 changes: 22 additions & 1 deletion src/back-top/back-top.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,31 @@ export class BackTopComponent {
}

handleClick(event: Event) {
this._scrollTarget.scrollTo({ top: 0, behavior: 'smooth' });
this.scrollToTop(300);
this.click.emit(event);
}

scrollToTop(duration: number) {
const startHeight = this.getTargetScrollTop(this._scrollTarget);
let startTime: number;

const scrollStep = (timestamp: number) => {
if (!startTime) {
startTime = timestamp;
}
const scrollHeight = Math.max(
startHeight - ((timestamp - startTime) / duration) * startHeight,
0,
);

this._scrollTarget.scrollTo(0, scrollHeight);
if (scrollHeight) {
requestAnimationFrame(scrollStep);
}
};
requestAnimationFrame(scrollStep);
}

getTarget(target: TargetType): Element | Window {
const scrollTarget =
(typeof target === 'string' && document.querySelector(target)) ||
Expand Down

0 comments on commit f3603d5

Please sign in to comment.