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

Page scroll #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/components/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function Nav() {
const logoRef = useRef();
useEffect(() => {
const throttled = throttle(() => logoRef.current.rotate(window.scrollY), 200, { trailing: true });
window.addEventListener("scroll", throttled);
window.addEventListener("scroll", throttled, { passive: true });

return () => window.removeEventListener("scroll", throttled);
}, []);
Expand Down
68 changes: 68 additions & 0 deletions src/components/scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { useEffect } from "react";
import { throttle } from "lodash";

let currScreen = 0;
const minScreen = 0;
const maxScreen = 4; // todo: unhardcode?

const scrollScreen = throttle(isScrolledDown => {
const scrollMod = isScrolledDown ? 1 : -1;

console.log(currScreen + scrollMod);
if (currScreen + scrollMod < minScreen || currScreen + scrollMod > maxScreen) return;

console.log((currScreen + scrollMod) * window.innerHeight);
window.scrollTo(0, (currScreen + scrollMod) * window.innerHeight);
currScreen += scrollMod;
}, 1500, { leading: true, trailing: false });

const scrollScreenWheel = e => {
e.preventDefault();
if (e.deltaY === 0) return;
scrollScreen(e.deltaY > 0);
};

let scrollY;

const screenSwipeStart = throttle(e => {
scrollY = e.touches[0].clientY;
}, 200);

const screenSwipeMove = throttle(e => {
if (!scrollY) return;

const currY = e.touches[0].clientY;
if (currY - scrollY === 0) return;
scrollScreen(currY > scrollY);
scrollY = null;

e.preventDefault();
}, 200);


export default function Scroll() {
useEffect(() => {
window.addEventListener("wheel", scrollScreenWheel, { passive: false });
window.addEventListener("touchstart", screenSwipeStart);
window.addEventListener("touchmove", screenSwipeMove);

return () => {
window.removeEventListener("wheel", scrollScreenWheel);
window.removeEventListener("touchstart", screenSwipeStart);
window.removeEventListener("touchmove", screenSwipeMove);
};
}, []);

return (
<div className="fixed top-0 left-0 h-screen w-screen">
{/*<div
className="absolute top-0 left-0 h-1/4 w-screen bg-red-400"
onClick={() => scrollScreen(false)}
></div>
<div
className="absolute bottom-0 left-0 h-1/4 w-screen bg-red-400"
onClick={() => scrollScreen(true)}
></div>*/}
</div>
);
}
9 changes: 7 additions & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react";
import Head from "next/head";

import Nav from "../components/nav";
import Cursor from "../components/cursor";
import Nav from "components/nav";
import Cursor from "components/cursor";
import Scroll from "components/scroll";

import Descriptions from "sections/descriptions";
import Splash from "sections/splash";
Expand All @@ -17,12 +18,16 @@ export default function Home() {
</Head>

<Cursor></Cursor>
<Scroll />

<Nav></Nav>

<main className="flex flex-col justify-center items-center min-h-screen">
<Splash />
<Descriptions />
<Descriptions />
<Descriptions />
<Descriptions />
</main>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/sections/splash.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Title = styled.div`
}
`;

const Header = styled.main`
const Header = styled.header`
${tw`h-screen flex flex-col justify-center`}
width: 80vw;
`;
Expand Down