LudOkapi's picture
can you make this work correctly
833b632 verified
Raw History Blame Contribute Delete
2.03 kB
document.addEventListener('DOMContentLoaded', () => {
// Initialize GSAP and ScrollTrigger
gsap.registerPlugin(ScrollTrigger);
// Get the elements
const container = document.getElementById('diagonal-scroll-container');
const contentWrapper = document.getElementById('content-wrapper');
const panels = gsap.utils.toArray('.panel');
// Calculate total width and height of all panels
const totalWidth = window.innerWidth * panels.length;
const totalHeight = window.innerHeight * panels.length;
// Set initial styles for diagonal layout
gsap.set(contentWrapper, {
position: 'absolute',
top: '50%',
left: '50%',
width: totalWidth,
height: totalHeight,
xPercent: -50,
yPercent: -50,
rotation: 45,
transformOrigin: 'center center'
});
// Set up the diagonal scroll effect
gsap.to(contentWrapper, {
xPercent: -50 + (panels.length * 50),
yPercent: -50 - (panels.length * 50),
ease: 'none',
scrollTrigger: {
trigger: container,
start: 'top top',
end: () => `+=${Math.sqrt(totalWidth * totalWidth + totalHeight * totalHeight)}`,
scrub: 1,
pin: true,
anticipatePin: 1,
invalidateOnRefresh: true
}
});
// Position each panel correctly
panels.forEach((panel, i) => {
gsap.set(panel, {
position: 'absolute',
left: i * window.innerWidth,
top: 0,
width: '100vw',
height: '100vh'
});
});
// Handle resize events
window.addEventListener('resize', () => {
ScrollTrigger.refresh();
});
// Smooth scroll to next panel on arrow click
document.querySelector('.animate-bounce')?.addEventListener('click', () => {
gsap.to(window, {
scrollTo: window.innerHeight * 0.8,
duration: 1.5,
ease: 'power2.inOut'
});
});
});