11import React from 'react' ;
2- import { StyleSheet } from 'react-native' ;
32import Animated , {
43 Extrapolate ,
54 interpolate ,
@@ -44,17 +43,6 @@ export const ParallaxLayout: React.FC<{
4443 [ vertical , width , height ]
4544 ) ;
4645
47- const layoutStyle = React . useMemo ( ( ) => {
48- return {
49- width : ! vertical
50- ? width * parallaxScrollingScale
51- : `${ 100 * parallaxScrollingScale } %` ,
52- height : vertical
53- ? height * parallaxScrollingScale
54- : `${ 100 * parallaxScrollingScale } %` ,
55- } ;
56- } , [ vertical , parallaxScrollingScale , width , height ] ) ;
57-
5846 const x = useOffsetX (
5947 {
6048 handlerOffsetX,
@@ -67,49 +55,49 @@ export const ParallaxLayout: React.FC<{
6755 ) ;
6856
6957 const offsetXStyle = useAnimatedStyle ( ( ) => {
70- const baseTranslateX = x . value - index * size ;
71- const padding = ( 1 - parallaxScrollingScale ) * size ;
72- const extraOffset = index * padding + padding / 2 ;
58+ const value = x . value / size ;
59+
60+ const translate = interpolate (
61+ value ,
62+ [ - 1 , 0 , 1 ] ,
63+ [
64+ - size + parallaxScrollingOffset ,
65+ 0 ,
66+ size - parallaxScrollingOffset ,
67+ ] ,
68+ Extrapolate . CLAMP
69+ ) ;
7370
7471 const zIndex = interpolate (
75- x . value ,
76- [ - size , 0 , size ] ,
72+ value ,
73+ [ - 1 , 0 , 1 ] ,
7774 [ 0 , size , 0 ] ,
7875 Extrapolate . CLAMP
7976 ) ;
77+
8078 const scale = interpolate (
81- x . value ,
82- [ - size , 0 , size ] ,
83- [ parallaxScrollingScale , 1 , parallaxScrollingScale ] ,
84- Extrapolate . CLAMP
85- ) ;
86- const relatedTranslateX = interpolate (
87- x . value ,
88- [ - size , 0 , size ] ,
89- [ parallaxScrollingOffset , 0 , - parallaxScrollingOffset ] ,
79+ value ,
80+ [ - 1 , 0 , 1 ] ,
81+ [
82+ Math . pow ( parallaxScrollingScale , 2 ) ,
83+ parallaxScrollingScale ,
84+ Math . pow ( parallaxScrollingScale , 2 ) ,
85+ ] ,
9086 Extrapolate . CLAMP
9187 ) ;
9288
93- if ( vertical ) {
94- return {
95- transform : [
96- {
97- translateY :
98- baseTranslateX + extraOffset + relatedTranslateX ,
99- } ,
100- { scale } ,
101- ] ,
102- zIndex,
103- } ;
104- }
105-
10689 return {
10790 transform : [
91+ vertical
92+ ? {
93+ translateY : translate ,
94+ }
95+ : {
96+ translateX : translate ,
97+ } ,
10898 {
109- translateX :
110- baseTranslateX + extraOffset + relatedTranslateX ,
99+ scale,
111100 } ,
112- { scale } ,
113101 ] ,
114102 zIndex,
115103 } ;
@@ -137,14 +125,18 @@ export const ParallaxLayout: React.FC<{
137125 ) ;
138126
139127 return (
140- < Animated . View style = { [ layoutStyle , styles . container , offsetXStyle ] } >
128+ < Animated . View
129+ style = { [
130+ {
131+ width,
132+ height,
133+ position : 'absolute' ,
134+ alignSelf : 'center' ,
135+ } ,
136+ offsetXStyle ,
137+ ] }
138+ >
141139 < LazyView shouldUpdate = { shouldUpdate } > { children } </ LazyView >
142140 </ Animated . View >
143141 ) ;
144142} ;
145-
146- const styles = StyleSheet . create ( {
147- container : {
148- alignSelf : 'center' ,
149- } ,
150- } ) ;
0 commit comments