I am trying to make the navbar sticky but my JQuery is not adding the css class that i want to add on scroll function

Below are the HTML, CSS, and JS code in which I am facing the problem, and not able to attain the functionality that I am supposed to.

//This is where I think the prblem is but I am not able to figure out how to correct it

window(function() {
  $(window).scroll(function() {
    if (this.scrollY > 20) {
      $('.navbar').addClass('sticky');
    } else {

    }
  })
});
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&family=Ubuntu:wght@400;500;700&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  text-decoration: none;
}

.max-width {
  max-width: 1300px;
  padding: 0 80px;
  margin: auto;
}


/* Navbar styling */

.navbar {
  position: fixed;
  width: 100%;
  font-family: "Ubuntu", "sans-serif";
  padding: 80px 0;
}

.navbar .max-width {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar.sticky {
  padding: 80px 0;
  *background: #ffffff;
}

.navbar .max-width .logo a {
  color: crimson;
}

.navbar .max-width .logo a span {
  color: #000;
}

.navbar .logo a {
  font-size: 35px;
  font-weight: 600;
}

.navbar .menu li {
  list-style: none;
  display: inline-block;
}

.navbar .menu li a {
  color: crimson;
  font-size: 18px;
  font-weight: 500;
  margin-right: 25px;
  transition: color 0.3s ease;
}

.navbar .menu li a:hover {
  color: #000;
}


/* Home Section */

.home {
  margin-top: -60px;
  display: flex;
  background: url("images/banner.png") no-repeat center;
  height: 100vh;
  min-height: 500px;
  font-family: "Ubuntu", sans-serif;
}

.home .max-width {
  margin: auto 0px auto 370px;
}

.home .home-content .text-1 {
  font-size: 27px;
}

.home .home-content .text-2 {
  font-size: 75px;
  font-weight: 600;
  margin-left: -3px;
}

.home .home-content .text-3 {
  font-size: 40px;
  margin: 5px 0;
}

.home .home-content .text-3 span {
  color: crimson;
  font-weight: 500;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Personal Portfolio Website</title>
  <link rel="stylesheet" href="style.css">
  <script src="https://kit.fontawesome.com/a076d05399.js"></script>
  <script src="src=" https://code.jquery.com/jquery-3.5.1.min.js ""></script>
</head>

<body>
  <nav class="navbar">
    <div class="max-width">
      <div class="logo"><a href="#">Aksh<span>at Saxena</span></a></div>
      <ul class="menu">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Skills</a></li>
        <li><a href="#">Teams</a></li>
        <li><a href="#">Contacts</a></li>
      </ul>
    </div>
  </nav>

  <!-- home section start -->
  <section class="home" id="home">
    <div class="max-width">
      <div class="home-content">
        <div class="text-1">Hello, my name is</div>
        <div class="text-2">Akshat Saxena</div>
        <div class="text-3">And i'm a Web <span>Web Developer</span></div>
      </div>
    </div>
  </section>

  <p>Hi, I am Akshat. I have over four years of experience in WordPress website development. If you are looking for a complete high-end, up-to-date, professional, and responsive WordPress website then you are in the right place. I will develop stunning and
    captivating websites for both businesses and individuals. I would love to help you with your desire website project. Feel free to contact me, Cheers!
  </p>
  <script src="script.js"></script>
</body>

</html>

While scrolling down the background of the nav-bar should turn white but it’s not behaving in that way it should be behaving. What would be the easier way to attain this functionality if I am not supposed to do it this way.

146 thoughts on “I am trying to make the navbar sticky but my JQuery is not adding the css class that i want to add on scroll function”

  1. Pingback: oprah keto diet
  2. I am really enjoying the theme/design of your weblog.
    Do you ever run into any internet browser compatibility issues?

    A number of my blog visitors have complained about my website not working correctly in Explorer but
    looks great in Chrome. Do you have any tips to help fix this
    problem?

    Reply
  3. Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between usability and visual appearance.
    I must say you have done a very good job with this.
    Also, the blog loads extremely fast for me on Firefox. Superb Blog!

    Reply
  4. My brother recommended I might like this blog.

    He was totally right. This post truly made my day.
    You can not imagine simply how much time I had spent for this information! Thanks!

    Reply
  5. Hello! This post couldn’t be written any better! Reading this post reminds me of my old room mate!
    He always kept talking about this. I will forward this write-up to him.
    Fairly certain he will have a good read. Thank you for sharing!

    Reply
  6. I think that what you posted made a great deal of sense.
    However, consider this, suppose you were to write a awesome headline?

    I am not saying your content is not solid, however suppose you added something to
    maybe grab folk’s attention? I mean I am trying
    to make the navbar sticky but my JQuery is not adding the css class that i want to add on scroll function – JavaScript Help is a little plain. You ought to peek at Yahoo’s front page and note how they create news
    headlines to grab people interested. You might add a related video or a pic or two to get people interested about
    everything’ve got to say. In my opinion, it might bring your posts a little livelier.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

    Reply
  7. Hi there I am so excited I found your blog page, I really found you by accident, while I was browsing on Digg for something else, Regardless I am here now and would just like to say
    many thanks for a remarkable post and a all round exciting blog (I also love the theme/design), I don’t have time
    to read it all at the minute but I have saved it and also added your RSS feeds, so when I have time I
    will be back to read a lot more, Please do keep up the great work. https://tadalafili.com/

    Reply

Leave a Comment