question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to use Animated to animate the transparency of the header.

See original GitHub issue

Description

I am trying to make the React Navigation header change it’s transparency from 0 to .95 and back to 0 while scrolling up and down. I have followed many different methods to get this working but it does not work with the createNativeStackNavigator, but it does work with createStackNavigator.

I surmise this is because the way it gets handled is adding the opacity to the headerBackground, which does not exist with the Native Stack.

I have also noticed that when scrolling I do not see the change in the headerOpacity value when I try log it in the useEffect hook. It only does it once, and never calls it again.

Code

const headerOpacity = yOffset.interpolate({
  inputRange: [0, 200],
  outputRange: [0, 1],
  extrapolate: "clamp",
});

<Animated.ScrollView
  onScroll={Animated.event(
    [
      {
        nativeEvent: {
          contentOffset: {
            y: yOffset,
          },
        },
      },
    ],
    { useNativeDriver: true }
  )}
  scrollEventThrottle={16}
>

useEffect(() => {
  navigation.setOptions({
    headerStyle: {
      opacity: headerOpacity,
    },
    headerBackground: () => (
      <Animated.View
        style={{
          backgroundColor: "white",
          ...StyleSheet.absoluteFillObject,
          opacity: headerOpacity,
        }}
      />
    ),
    headerTransparent: true,
  });
}, [headerOpacity, navigation]);

Package versions

  • Expo: 38.0.0
  • React: 16.11.0
  • React Native: 0.63
  • React Native Screens: 2.9.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
xmflsctcommented, Dec 15, 2020

@WoLewicki Sure!

So my stack has one screen like this:

    const [opacity, setOpacity] = useState(0)

    <Stack.Screen
      name='name'
      options={({ navigation }) => {
        return {
          headerTranslucent: true,
          headerStyle: {
            backgroundColor: `rgba(255, 255, 255, ${opacity / 200})`
          },
          headerCenter: () => null,
          )
        }
      }}
    >
      {({ route }) => (
        <SomeComponent route={route} setOpacity={setOpacity} />}
    </Stack.Screen>

And then in my component:

    <ScrollView
      bounces={false}
      onScroll={({nativeEvent}) => setOpacity(nativeEvent.contentOffset.y)}
      scrollEventThrottle={10}
    >
      ......
    </ScrollView>
0reactions
WoLewickicommented, Feb 24, 2021

I am closing it due to no response in more than 30 days and I am not sure if there is something more to be done here. Feel free to comment to reopen it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animate navigation header opacity while scrolling in ...
In my screen, I want to change opacity of navigation header 0 to 1 when I scroll up. Here is code in Snack....
Read more >
Change, remove or turn off animation effects - Microsoft Support
On the Animation tab, click Animation Pane. Open the Animation Pane. On the slide, click the animated object that you want to remove...
Read more >
What's In A GIF - Animation and Transparency - GifLib
This file is similar to the ones we've previously encountered. The bytes start out with the GIF header.
Read more >
Animating from "display: block" to "display: none"
For this reason, animating opacity to zero is simply not enough because an element with zero opacity still occupies the same space on...
Read more >
How to Add a CSS Fade-in Transition Animation to Text ...
You can also check out The Main Difference Between CSS Animations & Transitions to ... example of fade in image transition using css...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found