代码之家  ›  专栏  ›  技术社区  ›  Arebhy Sridaran

如何在reactjs中使用Jquery

  •  1
  • Arebhy Sridaran  · 技术社区  · 6 年前

    我是新来的,我有一个100像素的导航栏。 当我向下滚动我的页面,导航栏应该是顶部粘性。

    我的导航栏类代码是

    export default class NavBar extends React.Component {
      constructor(props) {
        super(props);
    
        this.toggle = this.toggle.bind(this);
        this.state = {
          isOpen: false
        };
      }
      toggle() {
        this.setState({
          isOpen: !this.state.isOpen
        });
      }
      render() {
        return (
          <div id="stickyheader">
            <Navbar color="light" white expand="md">
              <NavbarBrand href="/">
                <ResizeImage
                  src={require('../image/logo.png')}
                  alt="logo"
                  options={{ width: 10 },{ height: 50 },{ mode: 'pad' }}
    
                />
              </NavbarBrand>
              <NavbarToggler onClick={this.toggle} />
                <Collapse isOpen={this.state.isOpen} navbar>
                  <Nav className="ml-auto" navbar>
                    <NavItem>
                      <NavLink href="/components/">HOME</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="#">ALL CARS</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="#">RATES</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="#">ABOUT US</NavLink>
                    </NavItem>
                    <NavItem>
                      <NavLink href="#"><FontAwesomeIcon icon="users" />  BECOME A PARTNER</NavLink>
                    </NavItem>
    
                  </Nav>
                </Collapse>
            </Navbar>
          </div>
    
        );
      }
    }
    

    $(function(){
            // Check the initial Poistion of the Sticky Header
            var stickyHeaderTop = $('#stickyheader').offset().top;
    
            $(window).scroll(function(){
                    if( $(window).scrollTop() > stickyHeaderTop ) {
                            $('#stickyheader').css({position: 'fixed', top: '0px'});
                            $('#stickyalias').css('display', 'block');
                    } else {
                            $('#stickyheader').css({position: 'static', top: '0px'});
                            $('#stickyalias').css('display', 'none');
                    }
            });
      });
    

    当我使用 html格式 我想问题在于 id=“粘滞头” 我参考了一些链接,但没有找到合适的解决方案。请引导我解决这个问题。谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   xadm    6 年前

    您可以在react initial render之后初始化jQuery函数-只需从 componentDidMount .