60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
// index.ts
|
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
import { gsap } from 'gsap';
|
|
import { ScrollToPlugin } from 'gsap/dist/ScrollToPlugin';
|
|
|
|
// export function smoothScrollTo(elementId: string) {
|
|
// const element = document.getElementById(elementId);
|
|
// if (element) {
|
|
// element.scrollIntoView({
|
|
// behavior: 'smooth'
|
|
// });
|
|
// }
|
|
// }
|
|
|
|
export function smoothScrollTo(elementId: string) {
|
|
gsap.registerPlugin(ScrollToPlugin);
|
|
const element = document.getElementById(elementId);
|
|
if (!element) return;
|
|
|
|
// Function to disable scrolling
|
|
const disableScroll = (event: Event) => event.preventDefault();
|
|
|
|
// Disable scrolling through various events
|
|
window.addEventListener('wheel', disableScroll, { passive: false });
|
|
window.addEventListener('touchmove', disableScroll, { passive: false });
|
|
window.addEventListener('keydown', (event) => {
|
|
// Prevent scrolling through spacebar, arrow keys, page up/down
|
|
if (['Space', 'PageUp', 'PageDown', 'ArrowUp', 'ArrowDown'].includes(event.code)) {
|
|
event.preventDefault();
|
|
}
|
|
}, { passive: false });
|
|
|
|
gsap.to(window, {
|
|
scrollTo: { y: element, autoKill: false },
|
|
duration: 0.1,
|
|
ease: "power4.out",
|
|
onComplete: () => {
|
|
// Re-enable scrolling after animation
|
|
window.removeEventListener('wheel', disableScroll);
|
|
window.removeEventListener('touchmove', disableScroll);
|
|
window.removeEventListener('keydown', disableScroll);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
export function handleKeydown(event: KeyboardEvent) {
|
|
// Trigger redirection on Enter key or Space bar
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
smoothScrollTo('home');
|
|
}
|
|
}
|
|
|
|
export function openWebsite(url: string) {
|
|
goto(url);
|
|
}
|