Skip to content

Commit 8fb0b52

Browse files
authored
Merge pull request #52 from dohooo/fix51
fix: fix items position
2 parents d0f87ef + 20326c7 commit 8fb0b52

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default function App() {
9999
style={{
100100
width: '100%',
101101
height: '100%',
102+
borderRadius: 15,
102103
}}
103104
/>
104105
</View>

src/hooks/useOffsetX.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,17 @@ export const useOffsetX = (opts: IOpts) => {
3030
const HALF_WIDTH = 0.5 * width;
3131

3232
const x = useDerivedValue(() => {
33-
const defaultPos = width * index;
3433
if (loop) {
35-
function getDefaultPos(
36-
_type: 'positive' | 'negative',
37-
_count: number
38-
): {
39-
MAX: number;
40-
MIN: number;
41-
startPos: number;
42-
} {
43-
let MAX = null;
44-
let MIN = null;
34+
const positiveCount =
35+
type === 'positive' ? viewCount : VALID_LENGTH - viewCount;
4536

46-
let startPos: number = defaultPos;
47-
48-
if (_type === 'positive') {
49-
MAX = _count * width;
50-
MIN = -(VALID_LENGTH - _count) * width;
51-
} else {
52-
MAX = (VALID_LENGTH - _count) * width;
53-
MIN = -_count * width;
54-
}
55-
56-
if (defaultPos > MAX) {
57-
startPos = MAX - defaultPos;
58-
}
59-
60-
return {
61-
startPos,
62-
MAX,
63-
MIN,
64-
};
37+
let startPos = width * index;
38+
if (index > positiveCount) {
39+
startPos = (index - ITEM_LENGTH) * width;
6540
}
6641

67-
const { startPos, MAX, MIN } = getDefaultPos(type, viewCount);
42+
const MAX = positiveCount * width;
43+
const MIN = -((VALID_LENGTH - positiveCount) * width);
6844

6945
const inputRange = [
7046
-TOTAL_WIDTH,
@@ -94,7 +70,7 @@ export const useOffsetX = (opts: IOpts) => {
9470
);
9571
}
9672

97-
return handlerOffsetX.value + defaultPos;
73+
return handlerOffsetX.value + width * index;
9874
}, [loop, data, viewCount, type]);
9975

10076
return x;

0 commit comments

Comments
 (0)