Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/scoll issue #31

Open
wants to merge 2 commits into
base: petit/team2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions PetitProjects/Team2/src/components/Bullets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ const BulletItem = styled.li`
`;

const LIST_ITEM_HEIGHT = 72;
let rafCache: number | null = null;

const smoothMoveTo = (y: number) => {
let speed = Math.abs(window.scrollY - y) / 15;
const move = () => {
const diff = (window.scrollY - y);
if (diff === 0) return;

if (diff > 0) {
window.scrollTo(0, window.scrollY - Math.min(diff, speed));
} else {
window.scrollTo(0, window.scrollY + Math.min(Math.abs(diff), speed));
}
rafCache = requestAnimationFrame(move);
};
if (rafCache) {
cancelAnimationFrame(rafCache);
rafCache = null;
}
move();
};

const Bullets = () => {
const [focused, setFocused] = useState<string>(
Expand All @@ -41,10 +62,10 @@ const Bullets = () => {

const scrollToElement = (id: string) => {
const target = document.querySelector(`#${id}`) as HTMLElement;
window.scrollTo({
top: target.offsetTop,
behavior: "smooth",
});
// window.scrollTo({
// top: target.offsetTop,
// });
smoothMoveTo(target.offsetTop);
window.history.pushState(null, "", "#" + id);
};

Expand Down