1

I am new to React JS. Anyhow, I able to create the following Web App of my own.

My code works as I needed but with a small issue. The header shrinks and expands in a rugged manner (with a small vibration). The process does not happen smoothly. I suppose you can notice it in the gif image I provided above too. I need to make that transition smooth. How can I modify my code to achieve my aim?

Demo of the web application

This is the function I use to shrink the header upon scroll the page for 50 pixels down.

const [headerShrinked, setHeaderShrinked] = useState(false);
useEffect(() => {
  function resizeHeaderOnScroll() {
    const distanceY = window.pageYOffset || document.documentElement.scrollTop;
    const shrinkOn = 50;

    if (distanceY > shrinkOn) {
      setHeaderShrinked(true);
    } else {
      setHeaderShrinked(false);
    }
  }

  window.addEventListener("scroll", resizeHeaderOnScroll);
}, []);

And this is the external CSS code linked to it.

.header {
    font-size: 55px;
    font-weight: 600;
    height: 70px;
    letter-spacing: 1px;
    padding-right: 40px;
    padding-top: 0px;
}

.droplinks {
    font-family: Arial;
    font-size: 20px;
    font-weight: 300;
    color: black;
    position: relative;
    text-align: center;
}

.header {
    height: 70px;
    padding-right: 40px;
    padding-top: 0px;
    position: relative;
    transition: height 0s linear 0.5s;
}

.header.shrinked {
    height: 50px;
}

.headerBrand {
    font-size: 40px;
    font-weight: 600;
    letter-spacing: 2px;
    transition: font-size 0.5s;
}

.headerBrand.shrinked {
    font-size: 30px;
}

.navlinks {
    font-family: Arial;
    font-size: 20px;
    font-weight: 300;
    letter-spacing: 2px;
    padding-left: 30px;
    padding-right: 30px;
    position: relative;
    text-align: center;
}

.navlinks:hover {
    color: #ffffff;
    text-shadow: 0px 0px 60px #ffffff;
    transition: all 0.2s ease-in;
}

And here is the complete Js code.

import React, { useEffect, useState } from "react";
import { Navbar, Nav, NavDropdown } from "react-bootstrap";
import { Link } from 'react-router-dom';

import styles from "./Header.module.css";

const Header = (props) => {
    const [headerShrinked, setHeaderShrinked] = useState(false);
    useEffect(() => {
        function resizeHeaderOnScroll() {
            const distanceY = window.pageYOffset || document.documentElement.scrollTop;
            const shrinkOn = 50;

            if (distanceY > shrinkOn) {
                setHeaderShrinked(true);
            } else {
                setHeaderShrinked(false);
            }
        }

        window.addEventListener("scroll", resizeHeaderOnScroll);
    }, []);

    return (
        <Navbar sticky="top" collapseOnSelect expand="lg"  bg="dark" variant="dark">
            <Navbar.Brand className={`${styles.headerBrand} ${headerShrinked ? styles.shrinked : ""}`}>
                <Link to="/">
                    MyWebApp
                </Link>
            </Navbar.Brand>
            <Navbar.Toggle aria-controls="responsive-navbar-nav" />
            <Navbar.Collapse id="responsive-navbar-nav">
                <Nav className="mr-auto">
                    <NavDropdown title="Get to Know Us" id="collasible-nav-dropdown" className={styles.navlinks}>
                        <div className={styles.dropboard}>
                            <NavDropdown.Item className={styles.droplinks}>
                                <Link to="ourstory">
                                    Our Story
                                </Link>
                            </NavDropdown.Item>
                            <NavDropdown.Item className={styles.droplinks}>
                                <Link to="about">
                                    About Us
                                </Link>
                            </NavDropdown.Item>
                            <NavDropdown.Divider/>
                            <NavDropdown.Item className={styles.droplinks}>
                                <Link to="newtrends">
                                    New Trends
                                </Link>        
                            </NavDropdown.Item>
                        </div>
                    </NavDropdown>
                </Nav>
                <Nav>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="/">
                            Home
                        </Link>
                    </Nav.Link>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="features">
                            Features
                        </Link>
                    </Nav.Link>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="screenshots">
                            Screenshots
                        </Link>
                    </Nav.Link>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="pricing">
                            Pricing
                        </Link>
                    </Nav.Link>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="faqs">
                            FAQs
                        </Link>
                    </Nav.Link>
                    <Nav.Link className={styles.navlinks}>
                        <Link to="blog">
                            Blog
                        </Link>
                    </Nav.Link>
                    <Nav.Link eventKey={2} className={styles.navlinks}>
                        <Link to="contact">
                            Contact
                        </Link>
                    </Nav.Link>
                </Nav>
            </Navbar.Collapse>
        </Navbar>
    );
};

export default Header;
Pawara Siriwardhane
  • 1,873
  • 10
  • 26
  • 38
SashaaJ
  • 17
  • 4
  • 3
    You can use a different css transition easing function: `transition: height 0s ease 0.5s;` https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function – Håken Lid Mar 19 '21 at 10:20
  • Sir, I tried adding and changing the CSS but I failed. That is why I thought it might be a problem in the JS code – SashaaJ Mar 19 '21 at 11:09
  • You have to be more specific. What do you mean by "failed". You should be able to do this with CSS. – Håken Lid Mar 19 '21 at 11:13
  • The gif has a limited framerate, so I'm not sure if we can see the "rugged" transition there clearly. You should use the browser's devtools to confirm that the expected css transition property is applied. It's also possible that the scroll event handler is triggered multiple times, which could cause rerenders or jankyness. It's common practice to throttle or debounce scroll event listeners. You'll find many questions about this if you search for it. https://stackoverflow.com/questions/54061464/how-to-slowdown-debounce-events-handling-with-react-hooks – Håken Lid Mar 19 '21 at 11:38
  • Also: whenever you add an event listener to the document or window using `useEffect` you should also make sure to return a cleanup function to unregister the even listener when the component unmounts. `return () => { window.addRemoveListener("scroll", resizeHeaderOnScroll) };`. There are lots of things like this that could make a small or cumulative impact on performance that could make your animations look janky. – Håken Lid Mar 19 '21 at 11:48
  • Sir, in changing the CSS, like example: by reducing the time (transition: height 0s ease 0.5 ==> transition:height 0s ease 0.2) I was able to hide that rugged transition. It happened because the speed of the transition is high. so not noticeable to human eye. but it is very quick transition then. which is not what I need. I need the transition time as it is. but need to smoothen that effect. – SashaaJ Mar 19 '21 at 11:50
  • And I forgot to say thank for making the gif visible. since I was blocked to display it when posting question. I beg your pardon for the unclearness of the video. Sir, but there is a visible ruggedness when transition time is delayed. I want to disappear it. – SashaaJ Mar 19 '21 at 11:52
  • I will modify my code as you have advice, thank you very much for the support. – SashaaJ Mar 19 '21 at 11:53

0 Answers0