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] 화면 길이 변경되어도 비디오 타임라인 정상 작동 #126

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
38 changes: 22 additions & 16 deletions src/mainPage/features/introSection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import SpinningCarVideo from "./car-spin.webm";
import Pointer from "./pointer.svg";

function IntroSection() {
const VIDEO_LENGTH = 2;
const videoRef = useRef(null);
const introRef = useRef(null);
const carYPosition = useRef({ top: 0, bottom: 10 });
const frameRef = useRef(null);
const [isTimerVisible, setIsTimerVisible] = useState(false);
const [videoTimeline, setVideoTimeline] = useState(0);

const titleOpacity = useScrollTransition({
scrollStart: 0,
Expand All @@ -20,12 +22,18 @@ function IntroSection() {
valueEnd: 0,
});

const videoTimeline = useScrollTransition({
scrollStart: carYPosition.current.top,
scrollEnd: carYPosition.current.bottom,
valueStart: 0,
valueEnd: 2,
});
function calculateTimeline() {
const frameDOM = frameRef.current;
if (frameDOM) {
const videoHeight = frameDOM.offsetHeight;
const videoTop = frameDOM.getBoundingClientRect().top + window.scrollY;
const startScroll = videoTop + videoHeight / 2 - window.innerHeight;
const endScroll = startScroll + window.innerHeight;
const timeline = ((window.scrollY - startScroll) / (endScroll - startScroll)) * VIDEO_LENGTH;

setVideoTimeline(timeline);
}
}

useEffect(() => {
const introDOM = introRef.current;
Expand All @@ -51,14 +59,12 @@ function IntroSection() {
}, []);

useEffect(() => {
const videoDOM = videoRef.current;
if (videoDOM) {
const rect = videoDOM.getBoundingClientRect();
carYPosition.current = {
top: rect.top + rect.height * 0.5 - window.innerHeight,
bottom: rect.top + rect.height * 0.5,
};
}
window.addEventListener("scroll", calculateTimeline);
window.addEventListener("resize", calculateTimeline);
return () => {
window.removeEventListener("scroll", calculateTimeline);
window.removeEventListener("resize", calculateTimeline);
};
}, []);

useEffect(() => {
Expand All @@ -85,7 +91,7 @@ function IntroSection() {
</div>

<div className="relative mt-[800px] flex flex-col items-center">
<div className="overflow-hidden">
<div ref={frameRef} className="overflow-hidden">
<video
src={SpinningCarVideo}
ref={videoRef}
Expand Down