Adjusting Navigation bar size according to the size of the screen

I am currently working on a project where I want the navbar (and logo) to shrink when users scroll down on larger screens where the mobile menu button is not visible (> 768px). On smaller screens, I need the navbar to remain at a fixed smaller size.

Despite using standard Bootstrap 3 classes for the HTML and implementing JavaScript code, I am facing an issue with the resizing behavior. The current script applies the desired changes to the navbar on all screen sizes, whereas I only want them to be applied on larger screens.

function resize() {
    if ($(window).width() > 768) {
        $(window).scroll(function() {
            if ($(".navbar").offset().top > 50) {
                $(".navbar-fixed-top").addClass("shrink-nav");
                $(".navbar-brand img").addClass("shrink-logo");
            } else {
                $(".navbar-fixed-top").removeClass("shrink-nav");
                $(".navbar-brand img").removeClass("shrink-logo");
            }
        });
    } else {
        $(".navbar-fixed-top").addClass("shrink-nav");
        $(".navbar-brand img").addClass("shrink-logo");
    }
}

resize();

The CSS styling for the modifier classes used in this implementation can be found below.

.shrink-logo {
    position: relative;
    height: 60px;
    top: 15px;
}

.shrink-nav {
    height: 70px;
    padding: 15px 0;
}

If anyone can offer assistance with this issue, it would be greatly appreciated!

Answer №1

If you're looking to make your navigation bar responsive, check out this code snippet.

adjustNav();
$( window ).resize(adjustNav);
function adjustNav() {
    if ($(window).width() > 768) {
         if ($(".navbar").offset().top > 50) {
             $(".navbar-fixed-top").addClass("shrink-nav");
             $(".navbar-brand img").addClass("shrink-logo");
         } else {
            $(".navbar-fixed-top").removeClass("shrink-nav");
            $(".navbar-brand img").removeClass("shrink-logo");
         }
     } else {
         $(".navbar-fixed-top").addClass("shrink-nav");
         $(".navbar-brand img").addClass("shrink-logo");
     }
 }

Answer №2

Check out this code snippet

resize();
$(window).resize(resize);
$(window).scroll(resize);

function resize() {
    if ($(window).width() > 768) {
         if ($(".navbar").offset().top > 50) {
             $(".navbar-fixed-top").addClass("shrink-nav");
             $(".navbar-brand img").addClass("shrink-logo");
         } else {
            $(".navbar-fixed-top").removeClass("shrink-nav");
            $(".navbar-brand img").removeClass("shrink-logo");
         }
     } else {
         $(".navbar-fixed-top").addClass("shrink-nav");
         $(".navbar-brand img").addClass("shrink-logo");
     }
 }

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

Top method for parsing and processing a dynamic form with PHP

I am in the process of developing a dynamic form for registering contacts, similar to a contact list. To better illustrate my idea, here is a simplified example: <html> <head><title>Sample Form</title></head> <body> ...

What is the best way to choose all the options in a drop-down menu using Selenium Webdriver?

Currently, I am working with Selenium WebDriver and utilizing Java. In my application, there is a dropdown menu named Product containing multiple values (for example: 60). When running the code, I first deselect all options and then select the specific opt ...

Align your content perfectly on a full-screen slick slider

Attempting to center content over a full-screen slick slider from kenwheeler.github.io/slick/, I used a flexbox but the content remains at the edge of the viewport. It seems like an issue with the position tag in the slick CSS, but I can't figure out ...

Unexpected output from Material UI Textfield

When attempting to print a page of my React app using window.print(), everything prints correctly except for the Textfield component from Material UI. It works fine when there are only a few lines of text, but when there is a lot of text, it appears like t ...

Loop through individual divs containing radio buttons and check them one by one with a slight delay

I have implemented HTML tabs using radio buttons, similar to the demo showcased at https://css-tricks.com/examples/CSSTabs/radio.php. Here's the basic markup: <div class="tab"> <input type="radio"> <p>Content displayed when radio bu ...

Leveraging external JavaScript libraries in SAPUI5

I'm currently facing an issue while trying to include an external .js file in my SAPUI5 Controller. jQuery.sap.includeScript("externalLibrary.min.js", function() { // initializing objects from the library }); Despite setting up ...

Steps for incorporating the count() value in Django for web developmentTo embed the count() value while creating

I'm currently working on a web visual board project using Django. Here is the specific HTML section where I need to populate values from the database: <tbody> {% for obj in data_list %} <tr> <th>{{ ob ...

Is it possible to swap out the content within a <div> element with an external piece of HTML code using JQuery or JavaScript whenever the viewport dimensions are adjusted?

<html> <head> </head> function () { var viewportWidth = $(window).width(); if (viewportWidth < 700) { $('#wrapper').load('A.html'); }; <body> &l ...

What is the best way to send a parameter from JavaScript to a UserControl?

Within my ASP.Net application, I have a UserControl named EmployeesUserControl that is displayed within a JQuery modal dialog. Prior to displaying the Employees, I need to provide a filter criteria indicating which entity the employee belongs to. My query ...

Javascript Dilemma: Unable to Access 'object.style.left' and 'object.style.top' Values - What's the Issue?

Why am I unable to access and modify the object.style.left & object.style.top values? I am currently attempting to dynamically reposition a button on a webpage. In order to set new values for the style.left & style.top, I can utilize JavaScript l ...

Populate Dynamically Generated Tabs with Data using jQuery AJAX and PHP

My current scenario is: A PHP script is fetching data from a database, which is then displayed in the first tab. This tab remains fixed and cannot be closed. The code for this is working fine as it is. try { print ' <div class="clientTabs"&g ...

Enhance the appearance of specific wordpress usernames by adding a touch of "flair" next to them

I'm looking to add a special "flair" next to specific users on my WordPress website, similar to how YouTube distinguishes verified users. I have the CSS for changing the color on hover, but I need help keeping it positioned correctly. Examples from Y ...

Place the two buttons in position

I have a section where I need to include two buttons. One button on the left labeled "Sunday" and one on the right labeled "misc". I want the two buttons to be evenly placed within the area, so I assigned them the same class since they are the same size. ...

Is there a way to obtain the function associated with an onclick attribute?

I have a function attached to the onclick event in the HTML, as shown below: <script> function f1(){ alert('hello world'); }; </script> <a onclick="f1()">test</a> I want to manipulate that function by binding it to ...

Remove the class upon clicking

I recently created a toggle-menu for my website that includes some cool effects on the hamburger menu icon. The issue I am facing is that I added a JavaScript function to add a "close" class when clicking on the menu icon, transforming it into an "X". Whil ...

The React Basic Application runs smoothly when tested locally, however, in Azure, there seems to be an issue with loading the .CSS, .I

I have encountered an issue with my single web app where it functions well locally and deploys to Azure seamlessly via a release pipeline. However, after deployment, the .css file is not loading on the browser and returns a 404 error. The index.html file c ...

What is the best way to ensure that this form field remains hidden when the page is loaded?

In my Django project, I have a form that should only display the entity name on page load. However, it is currently showing both the entity name and quote text fields. The quote text field should only be visible when the entity name is not in the database. ...

Dealing with ASP.NET forms that involve a javascript plugin generating substitute input

I'm facing an issue with my ASP.NET MVC webpage where I submit a form using AJAX in the following way: function ValidateFormAndAjaxSubmit(formId, callingElement) { if (IsNotDblClick(callingElement.id)) { var _form = $("#" + formId); ...

Stop the submission of MVC Form

Using AJAX, I have a form with fields in a partial view that is sent to a controller action when submitted. The partial view appears in a pop-up div when a button on the main form is clicked. If the user completes and submits the form, the update is succes ...

Do not decode HTML content within an iframe; instead, extract the data directly from the HTML source

To expedite execution time, we have made the decision to not display the table in the iframe as it is invisible to the client. However, we still need to update the main page table by copying its contents. The approach we are taking is that the iframe shou ...