Skip to content

Commit 4eb7602

Browse files
committed
v1.10.0
1 parent 2818f75 commit 4eb7602

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

package/CHANGE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@
3535
## 1.8.1
3636

3737
- allowScrollUp, allowScrollDown 옵션 추가.
38+
39+
## 1.10.0
40+
41+
- Wheel이벤트에 속력 가중치를 구하여 트랙패드에서 휠이벤트 한번에 섹션이 여러번 이동하는 현상 수정.

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shinyongjun/react-fullpage",
3-
"version": "1.9.8",
3+
"version": "1.10.0",
44
"main": "./dist/cjs/index.js",
55
"module": "./dist/esm/index.js",
66
"source": "./src/index.tsx",

package/src/components/FullpageSection.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ function FullpageSection({
5151
);
5252
const [scrollDelay, setScrollDelay] = useState<boolean>(false);
5353
const { hashValue } = useHash();
54+
const deltaWeightTimer = useRef<number>(0);
55+
const [deltaWeight, setDeltaWeight] = useState<number>(0); // wheel 가중치
56+
const [lastDeltaWeight, setLastDeltaWeight] = useState<number>(0);
57+
// wheel 속도가 마지막 wheel 속도보다 빠르면 true 또는 wheel이벤트가 300ms 이상 없으면 true
58+
const wheelTokenTimer = useRef<NodeJS.Timeout | null>(null);
59+
const wheelToken = useRef<boolean>(true);
5460

5561
useEffect(() => {
5662
if (hashValue) {
@@ -78,13 +84,8 @@ function FullpageSection({
7884
}, [scrollDelay]);
7985

8086
const moveToSection = (newIndex: number) => {
81-
if (
82-
setIsAnimating === undefined ||
83-
setActiveIndex === undefined ||
84-
!allowScroll
85-
)
86-
return;
87-
if (isAnimating || scrollDelay) return;
87+
if (setIsAnimating === undefined || setActiveIndex === undefined) return; // 타입 에러 회피용
88+
if (!allowScroll || isAnimating || scrollDelay) return;
8889
setIsAnimating(true);
8990
setActiveIndex(newIndex);
9091
};
@@ -112,6 +113,23 @@ function FullpageSection({
112113
};
113114

114115
const handelWheel = (e: React.WheelEvent<HTMLDivElement>) => {
116+
const t = new Date().getTime();
117+
if (t - deltaWeightTimer.current < 300) {
118+
const computed = deltaWeight + Math.abs(e.deltaY);
119+
if (computed > lastDeltaWeight) {
120+
// wheel의 속도가 lastDeltaWeight보다 높으면 token 초기화
121+
wheelToken.current = true;
122+
}
123+
setDeltaWeight(computed);
124+
} else {
125+
setLastDeltaWeight(deltaWeight);
126+
deltaWeightTimer.current = t;
127+
setDeltaWeight(0);
128+
}
129+
130+
// token이 false라면 스크립트 중지
131+
if (!wheelToken.current) return;
132+
115133
if (e.deltaY > 0) {
116134
moveToNextSection();
117135
}
@@ -120,6 +138,19 @@ function FullpageSection({
120138
}
121139
};
122140

141+
useEffect(() => {
142+
wheelToken.current = false;
143+
if (wheelTokenTimer.current !== null) {
144+
clearTimeout(wheelTokenTimer.current);
145+
}
146+
wheelTokenTimer.current = setTimeout(() => {
147+
// wheel이벤트가 300ms동안 발생하지 않으면 token 초기화
148+
wheelToken.current = true;
149+
setDeltaWeight(0);
150+
}, 300);
151+
// eslint-disable-next-line react-hooks/exhaustive-deps
152+
}, [deltaWeight]);
153+
123154
const handleSwipeEnd = (direction: SwipeDirection) => {
124155
if (direction === 'UP') {
125156
moveToNextSection();

test/src/routes/Fullpage.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ function Fullpage() {
1919
transitionDuration={700}
2020
activeIndex={activeIndex}
2121
setActiveIndex={setActiveIndex}
22-
topScrollOnChange={true}
23-
onBeforeChange={(beforeIndex, afterIndex) => {
24-
console.log('before', beforeIndex, afterIndex);
25-
}}
26-
onAfterChange={(beforeIndex, afterIndex) => {
27-
console.log('after', beforeIndex, afterIndex);
28-
}}
29-
onAfterLoad={(container) => {
30-
console.log('afterLoad', container);
31-
}}
22+
topScrollOnChange={false}
23+
// onBeforeChange={(beforeIndex, afterIndex) => {
24+
// console.log('before', beforeIndex, afterIndex);
25+
// }}
26+
// onAfterChange={(beforeIndex, afterIndex) => {
27+
// console.log('after', beforeIndex, afterIndex);
28+
// }}
29+
// onAfterLoad={(container) => {
30+
// console.log('afterLoad', container);
31+
// }}
3232
>
3333
<FullpageSection name="first">
3434
<FirstSection />

0 commit comments

Comments
 (0)