简体   繁体   English

JavaScript function 在反应 js 中重新加载页面时不工作

[英]JavaScript function not working when page reloads in react js

I have implemented javascript function in react js.我在react js中实现了javascript function。 when implement the code, the function run perfectly but as i reload the browser, the function doesnot work.执行代码时,function 运行完美,但当我重新加载浏览器时,function 不起作用。 the function is as i scroll the page, the logo of header get small. function 在我滚动页面时,header 的徽标变小了。 as i'm new to the react js kindy help me in this.因为我是 React JS 的新手,请在这方面帮助我。 thanks in advance.提前致谢。

const Navigation =()=>{
    return(
        <div className="body">
        <div className='container'>
            <Navbar collapseOnSelect expand="lg" variant="dark" fixed="top" className="navbarBg navbar-fixed-top">
            <Container className="logo-className">
                <Navbar.Toggle aria-controls="basic-navbar-nav" />
                <Navbar.Collapse id="basic-navbar-nav">
                <Nav className="me-auto links">
                        <Nav.Link className="nav-items text-nowrap active left" href="#header-container">Home</Nav.Link>
                        <Nav.Link className="nav-items text-nowrap left" href="#about-section">About Us</Nav.Link>
                        <Nav.Link className="nav-items text-nowrap left" href="#sevices-container">Services</Nav.Link>
                        <Nav.Link className="text-nowrap mobile"><img id='navbar-logo' src={Logo} alt='logo' loading="lazy" /></Nav.Link>
                        <Nav.Link className="nav-items text-nowrap right" href="#Our-games">Our Projects</Nav.Link>
                        <Nav.Link className="nav-items text-nowrap right" href="#">Blog</Nav.Link>
                        <Nav.Link className="nav-items text-nowrap right" href="#contact">Contact Us</Nav.Link>
                </Nav>
                </Navbar.Collapse>
            </Container>
            </Navbar>

        
        </div>
        </div>
    );
}

export default Navigation;


window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
    document.getElementById("navbar-logo").style.cssText = "width: 22%; position: relative; top: -38px; left: 150px;";
  } else {
    document.getElementById("navbar-logo").style.cssText = "width: 28%;";
  }
}

Add an event listener for scroll.为滚动添加一个事件监听器。 You can do this if you're using class:如果您使用的是 class,则可以这样做:

componentDidMount() {
    window.addEventListener('scroll', this.scrollFunction);
},
componentWillUnmount() {
    window.removeEventListener('scroll', this.scrollFunction);
},
scrollFunction() {
    if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
        document.getElementById("navbar-logo").style.cssText = "width: 22%; position: relative; top: -38px; left: 150px;";
    } else {
        document.getElementById("navbar-logo").style.cssText = "width: 28%;";
    }
},...

Or this with hooks :或者这个带hooks

import React, { useEffect, useState } from 'react';

function Navigation () {
    const [offset, setOffset] = useState(0);
    useEffect(() => {
        const scrollFunction = () => {
            if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
                document.getElementById("navbar-logo").style.cssText = "width: 22%; position: relative; top: -38px; left: 150px;";
            } else {
                document.getElementById("navbar-logo").style.cssText = "width: 28%;";
            }
        };
        window.removeEventListener('scroll', scrollFunction);
        window.addEventListener('scroll', scrollFunction, { passive: true });
        return () => window.removeEventListener('scroll', scrollFunction);
    }, []);

    console.log(offset); 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM