"Reduce the height of the navigation bar when the user scrolls down the webpage

I am having an issue with my fixed navigation bar created using bootstrap 4. I want to reduce the height of the navbar as I scroll down the page, but it does not seem to be working. Here is a link to a video showing the problem in action:

Link to the video showcasing the issue

I would like my navbar to behave like the one in this video

Below is some code that I have tried:

$(document).ready(function() {
  $('.nav-button').click(function() {
    $('.nav-button').toggleClass('change');
  });

  $(window).scroll(function() {
    var position = $(this).scrollTop();
    if (position >= 200) {
      $('.nav-menu').addClass('.custom-navbar');
    } else {
      $('.nav-menu').removeClass('.custom-navbar');
    }
  });
});
body {
  font-family: 'Montserrat', sans-serif;
}
/* Rest of the CSS code here */

Answer №1

If you're using addClass and removeClass, remember not to include a dot before the class name. Here's an example code snippet:

if(position >= 200) {
  $('.nav-menu').addClass('custom-navbar');
} else {
  $('.nav-menu').removeClass('custom-navbar');
}

Take a look at this CodePen, it might be close to what you need:

https://codepen.io/Masoudm/pen/mgdPVP

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hide dynamic jQuery UI tabs that are not selected

About My Project: Currently, I am developing a chat system that utilizes websockets. In order to enhance the user experience, I have integrated jQuery UI tabs for the design. These tabs will facilitate communication in various chatrooms and private setti ...

Learn the process of covering the entire screen with a video

I'm attempting to achieve this: IMG Below is the code snippet I've been using: <div class="container" id="containervideo"> <div id="video"> <div class="box iframe-box"> <div class="container"> ...

Utilizing tables for inquiries in email communication

Currently tackling a basic mailer with html. It seems like tables are recommended and inline styling is the safer route. However, I'm encountering an issue where there's a mysterious space when setting up my td and I can't seem to pinpoint ...

Applying CSS to color the letters of a text, without needing to alter the HTML code

I'm just beginning to learn CSS and today I received a school assignment: Below is the HTML code provided: <head> <meta charset="UTF-8"> <title>ABC</title> <style> /* CSS section */ </style> ...

Verification of JQuery UI Dialog Prompt using the Enter key

I have attempted the methods recommended in a stack answer but to no avail: Submit jQuery UI dialog on <Enter> There seems to be an issue with my code. Upon logging in, a disclaimer appears to notify users that the content within the site is confid ...

What is the best way to ensure my jQuery plugin is up to date?

I have a question regarding the functionality of this plugin I am using. My goal is to update the timer it provides. To start the countdown timer with 5000 milliseconds remaining, I use the following code: $('#CountdownTimer').countdown({ remai ...

Tips for aligning and emphasizing HTML content with audio stimuli

I am looking to add a unique feature where the text highlights as audio plays on a website. Similar to what we see on television, I would like the text to continue highlighting as the audio progresses. Could someone please provide me with guidance on how ...

Combining the total of numerous inputs that are multiplied by a specific value all at once

Hey, I've been working on a project with a table and an input field with costs using jQuery. You can check out This Fiddle for reference. $( ".total" ).change(function() { let i = 1; var input001 = document.getElementsByName(i)[0]; var ...

JavaScript function is returning 'undefined' instead of an integer

My JavaScript/jQuery function is not functioning correctly and instead of returning an integer, it returns undefined. function __getLastSelectedCategory(table_id) { if ( jQuery('.categories_table[data-table-id="1"]').find('td.active&apo ...

Exploring the intricacies of Knockout JS mapping nested models using fromJS function

I am struggling with understanding how to effectively utilize the Knockout JS Mapping Plugin. My scenario involves nested models, and currently I am only using the ko.mapping.fromJS() in the parent model. However, I have noticed that the computed values ar ...

Form alignment issue: Bootstrap justify-content feature not functioning as expected

I am having trouble aligning my input form that is designed to capture Twitter handles in the center of the page. Despite using Bootstrap and ASP.NET, I cannot seem to get it to work as intended. Here is a snippet of the relevant CSS/HTML code: <form ...

Effortless File Uploading in JERSEY using jquery and AJAX

I'm looking to achieve file uploads with Jersey through jQuery/AJAX, but I'm struggling with extracting the file from the input and sending it via AJAX. Below is my current code: HTML <form action="rest/files/upload" method="post" enctype=" ...

"Enhancing user experience with Ajax by dynamically loading diverse content via

My goal is to change the URL in the address bar and add a class to the anchor tag when clicking on a tab. I came across a great example on jsfiddle that someone shared on stackoverflow. http://jsfiddle.net/VcQKr/2/ The issue I'm facing is that while ...

A guide to entering information into an input field with JavaScript for logging in successfully

https://i.stack.imgur.com/TF51Z.gif https://i.stack.imgur.com/HHsax.png https://i.stack.imgur.com/HUztt.png When attempting to input text using document.getelement('').value="" , it doesn't behave as expected. The text disappear ...

Change the <base> element programmatically during server-side rendering

Searching for a way to obtain the base URL for an HTML page, so that relative URL requests from the browser utilize that base. If you are looking for answers, check out this link: Defining root of HTML in a folder within the site root folder When serving ...

The width of the HTML page seems to adjust or shift when I click on

I'm encountering an issue with my navbar. Whenever I open the dropdown menu, the width of my page changes but returns to normal when it is closed. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="styl ...

Why does the CSHTML button containing a JavaScript onclick function only function intermittently?

I've implemented a download button on a webpage that dynamically assigns an ID based on the number of questions posted. Below is the code for the button: <input data-bind="attr: { id: $index() }" type="button" value="Downlo ...

What could be causing the drop-down menu to function properly when offline but not when accessed on the server?

When visiting the website here and clicking on the contact link, you should see a Google map. However, for some unknown reason, it does not display. The strange part is that when I open the contact.html file directly, the map works perfectly fine. It seem ...

Adjust the height of a DIV element dynamically to match the height of the viewport

How can I make a div's height 30% of the viewport and ensure it scales when the viewport size changes? I attempted setting min-height: 30%; height: 30% but without success. I considered using JQuery's height(); function, but I'm unsure how ...

I need the sidebar to be visible across all interfaces

https://i.stack.imgur.com/iEgAF.png I have developed a website for employee monitoring with six interfaces. The first interface is for sign-up, the second for logging in, the third for creating projects, the fourth for displaying projects, the fifth for c ...