Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript function not working when page reloads in react js

I have implemented javascript function in react js. when implement the code, the function run perfectly but as i reload the browser, the function doesnot work. the function is as i scroll the page, the logo of header get small. as i’m new to the react js kindy help me in this. 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%;";
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Add an event listener for scroll. You can do this if you’re using 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:

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); 
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading