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

Add background wave parallax #56

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 16 additions & 3 deletions client/src/components/layout/RedBackground.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import React from 'react';
import React, {useRef, useEffect} from 'react';


const RedBackground = () => {
const leftColor = 'ff594c';
const rightColor = 'eb144c';

// Parallax
const svg = useRef<SVGSVGElement>(null);

useEffect(() => {
if (!svg.current) return;
const handleScroll = () => {
svg.current!.style.top = '-' + window.scrollY / 5 + 'px';
}
document.addEventListener('wheel', handleScroll);
return function cleanup() {
document.removeEventListener('wheel', handleScroll);
}
Comment on lines +16 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the scroll event since that's the actual event that is dispatched when the scroll position changes. The wheel event is dispatched when the scroll wheel moves, but that is not a perfect correlation with scrolling; the scroll wheel is also used for zooming (zooming by pinching with the trackpad dispatches wheel events with ctrlKey set to true), and the page can be scrolled without using the scroll wheel (using a finger, arrow keys, tab key, URL fragments, and Windows smooth scrolling)

}, [svg])

return (
<svg width="100%" id="red-bg" viewBox="0 0 1440 700" xmlns="http://www.w3.org/2000/svg"
className="transition duration-300 ease-in-out delay-150">
<svg width="100%" id="red-bg" ref={svg} viewBox="0 0 1440 700" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="gradient">
<stop offset="5%" stopColor={`#${rightColor}66`}/>