1- import { motion } from "framer-motion" ;
1+ import { useEffect , useRef , useState } from "react" ;
2+ import { motion , useScroll , useTransform } from "framer-motion" ;
3+ import "../../styles/snap.modules.sass" ;
24
35import Phone1 from "./Phone1" ;
46import Phone2 from "./Phone2" ;
57import Phone3 from "./Phone3" ;
68
7- import "../../styles/snap.modules.sass" ;
8-
99const Component2Mobile = ( ) => {
10- const fadeInUp = {
11- initial : { opacity : 0 , y : 40 } ,
12- animate : { opacity : 1 , y : 0 } ,
13- transition : { duration : 0.6 , ease : "easeOut" } ,
14- } ;
10+ const [ activeSection , setActiveSection ] = useState ( 0 ) ;
11+ const ref = useRef < HTMLDivElement > ( null ) ;
12+
13+ const { scrollYProgress } = useScroll ( {
14+ target : ref ,
15+ offset : [ "start end" , "end start" ] ,
16+ } ) ;
17+ const opacity = useTransform ( scrollYProgress , [ 0.1 , 0.12 ] , [ 0 , 1 ] ) ;
18+
19+ useEffect ( ( ) => {
20+ const handleScroll = ( ) => {
21+ if ( ! ref . current || typeof ref === "function" ) return ;
22+
23+ const sectionTop = ref . current . offsetTop ;
24+ const scrollY = window . scrollY ;
25+ const sectionHeight = window . innerHeight * 0.5 ;
26+
27+ const currentSection = Math . floor (
28+ ( scrollY - sectionTop + sectionHeight / 2 ) / sectionHeight
29+ ) ;
30+
31+ setActiveSection ( currentSection ) ;
32+ } ;
33+
34+ window . addEventListener ( "scroll" , handleScroll ) ;
35+ return ( ) => {
36+ window . removeEventListener ( "scroll" , handleScroll ) ;
37+ } ;
38+ } , [ ref ] ) ;
1539
1640 return (
17- < div className = "snap-container" >
41+ < motion . div
42+ className = "mobile-img-cont"
43+ ref = { ref }
44+ style = { {
45+ opacity,
46+ position : "sticky" ,
47+ } }
48+ >
1849 < div className = "mobile-func-box" >
1950 < h1 className = "mobile-title2" >
2051 4년간의 현장 경험으로 탄생한 솔루션 < br />
@@ -23,24 +54,14 @@ const Component2Mobile = () => {
2354 < h4 className = "mobile-sub-title2" >
2455 방탈출에만 몰입할 수 있는 특별한 기능을 제공합니다.
2556 </ h4 >
26- </ div >
2757
28- < div className = "snap-section" >
29- < motion . div { ...fadeInUp } >
30- < Phone1 />
31- </ motion . div >
32- </ div >
33- < div className = "snap-section" >
34- < motion . div { ...fadeInUp } >
35- < Phone2 />
36- </ motion . div >
37- </ div >
38- < div className = "snap-section" >
39- < motion . div { ...fadeInUp } >
40- < Phone3 />
41- </ motion . div >
58+ < div className = "mobile-func-img-box" >
59+ { activeSection <= 0 && < Phone1 key = "phone1" /> }
60+ { activeSection === 1 && < Phone2 key = "phone2" /> }
61+ { activeSection >= 2 && < Phone3 key = "phone3" /> }
62+ </ div >
4263 </ div >
43- </ div >
64+ </ motion . div >
4465 ) ;
4566} ;
4667export default Component2Mobile ;
0 commit comments