-
My goal is to recreate a 'parallax header', or a header that shrinks in height as a scroll. I am doing this using three components (MVE): Main: const STATUS_BAR_HEIGHT = Platform.OS === 'ios' ? 45 : StatusBar.currentHeight;
const MIN_HEADER_HEIGHT = 25 + STATUS_BAR_HEIGHT;
const MAX_HEADER_HEIGHT = 200 + STATUS_BAR_HEIGHT;
const MainPage = ({navigation, route}) => {
const scrollOffset = useSharedValue(0);
const scrollHandler = useAnimatedScrollHandler({
onScroll: event => {
scrollOffset.value = event.contentOffset.y;
},
});
return (
<>
<Header theme={theme} scrollOffset={scrollOffset}></Header>
<PropertyInfo property={property} scrollHandler={scrollHandler} />
</>
) Header: const Header = ({theme, scrollOffset }) => {
const container = useAnimatedStyle(() => {
return { height: (MAX_HEADER_HEIGHT - scrollOffset.value)}
});
return (
<Animated.View style={[HeaderStyles.container, container]}>
<StatusBar barStyle="dark-content" />
<Appbar.Header style={{backgroundColor: 'rgba(255,255,255,0)'}}>
<Text>Button</Text>
</Appbar.Header>
<LinearGradient colors={['rgba(255,255,255,0)','rgba(1,1,1,1)']} style={HeaderStyles.linearGradient}>
<View style={{backgroundColor: 'cyan'}}>
<Text>
Header
</Text>
</View>
</LinearGradient>
</Animated.View>
)
} ScrollView const PropertyInfo = ({property, scrollHandler}) => {
return (
<Animated.ScrollView scrollEventThrottle={16} onScroll={scrollHandler}>
<Text>{JSON.stringify(property, null,2)}</Text>
</Animated.ScrollView>
)
} Currently this functions, in that it shrinks the header as I scroll. However it seems that if I scroll far enough such that the height becomes 0, or if I scroll back far enough such that the height exceeds
I think I need const container = useAnimatedStyle(() => {
return { height: interpolate(scrollOffset.value, {
inputRange: [(-MAX_HEADER_HEIGHT+scrollOffset.value), 0],,
outputRange: [MAX_HEADER_HEIGHT, MIN_HEADER_HEIGHT],
extrapolateRight: Extrapolate.CLAMP
}
)
}
}); My reasoning being that:
However this gives me an error that is new to me:
I know I am missing some understanding, however I have read through the fundamentals and feel as though I have a grasp on sharedValues, etc. I think I am missing something in terms of using the Can someone help me fill in the holes in my knowledge? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
It looks like you're using the wrong interpolate syntax (v1). interpolate(scrollOffset.value, [(-MAX_HEADER_HEIGHT+scrollOffset.value), 0], [MAX_HEADER_HEIGHT, MIN_HEADER_HEIGHT], Extrapolate.CLAMP) |
Beta Was this translation helpful? Give feedback.
-
Also the error is caused by RCTText, maybe try temporarily removing the |
Beta Was this translation helpful? Give feedback.
It looks like you're using the wrong interpolate syntax (v1).
interpolate
in v2 would look like this: