Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 임베디드
- 인접 형제 결합자
- not 의사클래스
- html
- background-color 속성
- Checked 의사 클래스
- RGB
- html 태그
- 전체 선택자
- focus 의사클래스
- height속성
- Live Server
- i 태그
- sub태그
- 일반 형제 결합자
- tag html
- reveal in file explorer
- html tag i
- id 선택자
- go live
- 아두이노
- sup태그
- iframe
- br 태그
- 자식결합자
- RGBA
- css
- padding 속성
- width속성
- iframe 태그
Archives
- Today
- Total
so woon!
[FRAMER MOTION] useScroll 본문
학습일 : 2023. 05. 14
useScroll을 사용하게 되면
스크롤의 MotionValue를 얻을 수 있다.
사용하기 위해 useScroll을 import 해준다.
import { useScroll } from "framer-motion";
useScroll이 넘겨주는 두가지는
1. scrollX, scrollY
2. scrollXProgress, scrollYProgress
스크롤시 배경색 (서서히) 변화시키기
scrollY는 픽셀 단위인 수직 스크롤이 될 것이다. (픽셀 단위로 스크롤 간격을 줄것임.)
scrollYProgress는 0과 1사이 값이고 (0은 끝이고 1은 시작. 반대일수도 있음)
스크롤 위치에 따라 서서히 배경색이 바뀜
import { styled } from "styled-components";
import {
motion,
useMotionValue,
useMotionValueEvent,
useScroll,
useTransform,
} from "framer-motion";
// style
const Wrapper = styled(motion.div)`
height: 500vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
`;
// 박스
const Box = styled(motion.div)`
width: 200px;
height: 200px;
background-color: rgba(255, 255, 255, 1);
border-radius: 40px;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1), 0 10px 20px rgba(0, 0, 0, 0.06);
`;
function App() {
const x = useMotionValue(0);
const rotateZ = useTransform(x, [-800, 800], [-360, 360]);
const gradient = useTransform(
x,
[-800, 800],
[
"linear-gradient(135deg, rgb(255, 255, 255), rgb(1, 10, 29))",
"linear-gradient(135deg, rgb(201, 207, 21), #060c47",
]
);
// useScroll
const { scrollY, scrollYProgress } = useScroll();
useMotionValueEvent(scrollY, "change", (latest) => {
console.log("scrollY : ", latest);
});
useMotionValueEvent(scrollYProgress, "change", (latest) => {
console.log("scrollYProgress : ", latest);
});
return (
<Wrapper style={{ background: gradient }}>
<Box style={{ x, rotateZ: rotateZ }} drag="x" dragSnapToOrigin></Box>
</Wrapper>
);
}
export default App;
실행결과
스크롤시 박스 크기 변화시키기
import { styled } from "styled-components";
import {
motion,
useMotionValue,
useMotionValueEvent,
useScroll,
useTransform,
} from "framer-motion";
// style
const Wrapper = styled(motion.div)`
height: 200vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
`;
// 박스
const Box = styled(motion.div)`
width: 200px;
height: 200px;
background-color: rgba(255, 255, 255, 1);
border-radius: 40px;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1), 0 10px 20px rgba(0, 0, 0, 0.06);
`;
function App() {
const x = useMotionValue(0);
const rotateZ = useTransform(x, [-800, 800], [-360, 360]);
const gradient = useTransform(
x,
[-800, 800],
[
"linear-gradient(135deg, rgb(255, 255, 255), rgb(1, 10, 29))",
"linear-gradient(135deg, rgb(201, 207, 21), #060c47",
]
);
// useScroll
const { scrollYProgress } = useScroll();
const scale = useTransform(scrollYProgress, [0, 1], [1, 5]);
return (
<Wrapper style={{ background: gradient }}>
<Box
style={{ x, rotateZ: rotateZ, scale: scale }}
drag="x"
dragSnapToOrigin
></Box>
</Wrapper>
);
}
export default App;
실행결과
'Framer Motion > 개념정리' 카테고리의 다른 글
[FRAMER MOTION] AnimatePresence + 슬라이더 만들기 (0) | 2023.05.15 |
---|---|
[FRAMER MOTION] svg Animation (0) | 2023.05.14 |
[FRAMER MOTION] useMotionValue + useTransform 사용하기 (0) | 2023.05.13 |
[FRAMER MOTION] Drag Constraint (드래그 제약) (0) | 2023.05.12 |
[FRAMER MOTION] Dragging (0) | 2023.05.11 |
Comments