Search code examples
javascriptcssreactjsscrollonscroll

How do I change the color of this AnchorLink on scroll?


Trying to change the color of a logo on scroll. Currently, the navigation bar changes colors, but I need the logo to change with it. Here's my current code:

navigation.js

return (
      <Nav {...this.props} scrolled={this.state.hasScrolled}>
        <StyledContainer>
          <Brand>
            <Scrollspy offset={-64} item={["top"]} currentClassName="active">
              <AnchorLink href="#top" onClick={this.closeMobileMenu}>
                Brand
              </AnchorLink>
            </Scrollspy>
          </Brand>
        </StyledContainer>
      </Nav>
    )

style.js

export const Brand = styled.div`
  font-family: ${props => props.theme.font.extrabold};
  ${props => props.theme.font_size.regular};
  color: ${props => props.theme.color.black.regular};
  text-decoration: none;
  letter-spacing: 1px;
  margin: 0;
  ul {
    list-style: none;
    margin: 0;
    padding: 0;

    a {
      color: ${props => (props.scrolled ? `black` : `white`)};
      text-decoration: none;
    }
  }
`

Any help is much appreciated - thank you so much!


Solution

  • Found the answer - I simply had to add this bit of code in the navigation.js file:

    navigation.js

    return (
          <Nav {...this.props} scrolled={this.state.hasScrolled}>
            <StyledContainer>
              <Brand {...this.props} scrolled={this.state.hasScrolled}>
                <Scrollspy offset={-64} item={["top"]} currentClassName="active">
                  <AnchorLink href="#top" onClick={this.closeMobileMenu}>
                    Brand
                  </AnchorLink>
                </Scrollspy>
              </Brand>
            </StyledContainer>
          </Nav>
        )