The header is visible above the navigation bar when scrolling on mobile, but it should be hidden

I am facing an issue with fixed content on mobile positioned above a Bootstrap navbar. The problem arises when scrolling down, causing the navbar to move up by 50px (the height of the content above the navbar). Everything seems to work fine initially, but once the address bar disappears on mobile, the top content is displayed again unexpectedly. This behavior does not occur while resizing on desktop. How can I prevent this from happening?

HTML

<div class="container" id="header-top-logo"><a href="/"><img src="assets/logo_reverse.svg" class="logo" alt="Superhoidjad"></a></div>
<nav class="navbar navbar-fixed-top">
   <div class="container">
      <div class="col-md-12">
         <!-- Brand and toggle get grouped for better mobile display -->
         <a class="navbar-brand" class="pull-left" href="{{route('main.home')}}"><img src="assets/logo.svg" class="logo" alt="Superhoidjad"></a>
         <!-- Collect the nav links, forms, and other content for toggling -->
         <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right navbar-item">
               {{--
               <li class="nav-item"><a class="nav-link" href="/login">Sisene</a></li>
               <li class="nav-item"><a class="nav-link" href="/signup/family">Registreeri&nbsp</a></li>
               --}}
               <li class="nav-item {{ Route::current()->getName() == 'main.about' ? 'active' : '' }}"><a class="nav-link" href="{{route('main.about')}}">Meist</a></li>
               <li class="nav-item {{ Route::current()->getName() == 'main.articles.all' ? 'active' : '' }}"><a class="nav-link" href="{{route('main.articles.all')}}">Artiklid</a></li>
               <li class="nav-btn{{ Route::current()->getName() == '' ? 'active' : '' }}"><a href="#" data-toggle="modal" data-target="#parent_signup" class="search-nanny">Saada kutse</a></li>
            </ul>
         </div>
      </div>
   </div>
</nav>

My JS

//top logo scroll function
function logoScroll() {
    $(window).scroll(function() {
        if ($(window).scrollTop() >= 50) {
            $(".navbar").css("top", "0px");
        } else {
            $(".navbar").css("top", "50px");
        }
    });
}

//hide top logo on load
var wdwWidth = $(window).width();
if (wdwWidth <= 991) {
    $(".navbar").css("top", 50);
    logoScroll();
} else {
    $(".navbar").css("top", "0px");
}

Answer №1

It's also important to consider handling the resize event in your code.

$window.resize(function(){
    if ($(window).scrollTop() >= 50) {
        $(".navbar").css("top", "0px");
    } else {
        $(".navbar").css("top", "50px");
    }
});

Answer №2

How about considering updating your JavaScript to the following:

// Function for scrolling top logo
function logoScroll() {
    $(window).scroll(function() {
        if ($(window).scrollTop() >= 50) {
            $(".navbar").css("top", "0px");
            $("#header-top-logo").css("display", "none");
        } else {
            $(".navbar").css("top", "50px");
            $("#header-top-logo").css("display", "block");
        }
     });
}

EDIT:

You can try this (perhaps the .css functions should be rearranged, as I'm not entirely sure what's happening in your code):

// Hide top logo on page load
var wdwWidth = $(window).width();
if (wdwWidth <= 991) {
    $("#header-top-logo").css("display", "block");
} else {
    $("#header-top-logo").css("display", "none");
}

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

Display the previous item I chose

When using jQuery to display an overview of user selections from a large section of select tags, I am facing an issue. Despite changing my selection multiple times, the code continues to show all previous selections along with the most recent one. This can ...

Achieving Perfect Alignment of Images Using HTML and CSS

I am currently working on designing a HTML/CSS layout for a Kiosk-style website. However, I am encountering some challenges in getting the alignment of images and text to appear exactly as desired. The goal is to have the logo and header remain fixed in ...

Combining PHP Streaming with the Power of jQuery

What is the optimal method for displaying or managing a large result upon successful completion while waiting for PHP to process that result? I am interested in utilizing jQuery to submit a form, have PHP handle it, and provide feedback to users during the ...

When scrolling, a new page loads seamlessly

Recently, I came across a website that has an interesting feature where new content is loaded automatically while scrolling, seamlessly appending to the existing page. What's more fascinating is that not only does the content change, but the URL also ...

What is the method for generating three inputs simultaneously in a single line?

I'm facing a challenge in my code where I want each line to contain only three input fields. Whenever I try to add a fourth input field, the first one remains on the previous line while the other three move to the next line. Oddly enough, this issue o ...

What is the best tool for combining CSS and SCSS code together?

My code is becoming hard to manage, so I need to consolidate some lengthy regular CSS statements like this: .aui #content .columns-1-2-equal .row-fluid #column-3 header { prop1 ...prop2 prop3 } into my current SCSS file. For instance, if I have a snippe ...

Instructions on how to send a data array using the jquery form plugin

I encountered an issue while trying to send an array of data to a php processor page for processing tasks like insertion. It appears that the data is not being transmitted, resulting in this error message: Error Message: Notice: Undefined index: reci in ...

Reorganizing Bootstrap 5 columns as the screen size changes

I need to change the display order of columns in bootstrap Currently I have two columns side by side in one row like this: Col A - Col B (on all screens except mobile devices) However, when the screen size is reduced or viewed on a mobile device, I want ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

Is there a way to automatically scroll to the bottom of a div when it first

Looking to enhance my application with a chat feature that automatically scrolls to the bottom of the chat page to display the latest messages. Utilizing VueJs: <template> <div id="app"> <div class="comments" ...

Trouble with executing jQuery $.ajax post to PHP within the same file

Here is the code snippet: <script> $( document ).ready(function() { $( ".button" ).click(function() { var clickBtnValue = $(this).val(); $.ajax({ //url: '', // url is empty because I'm working in the ...

Which javascript alternative most closely mimics the features of html5 form validation?

I just finished creating a form validation app using html5 for client side validation. Now, I am searching for a javascript implementation that can replicate this functionality in older browsers. The key qualities I am looking for include: Replicating t ...

Utilizing ASP.NET with JavaScript to target elements by ID within an iframe

Struggling to populate my form within an iframe by inputting a value in a textbox and selecting an option from a dropdown menu. webform1 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title ...

Solution for unresolvable Ajax token error

I am encountering an error after using setTimeout and receiving an unexpected token. I was attempting to handle errors in my ajax request by displaying a message if there is one, or reloading the webpage after a few seconds. function submit ...

"Combining the power of jQuery plugins with Angular for

Within my Angular application, I have created a custom directive specifically for a certain type of input field... greengo.directive('selectize', function() { return { restrict: 'A', link: function(scope, element, attrs) { ...

There has been a mistake: "Unable to locate a suitable differ supporting object '[object Object]' of type 'object'. NgFor is only able to bind to Iterables like Arrays."

Currently facing an issue: I am trying to extract the values of "place" using ngFor directive. Here's what I have implemented: .html <ng-container *ngFor="let se of List" Here is the JSON data: .json "place": [ { &qu ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

How to send parameters to the jquery .css() method

I'm attempting to create hyperlinks that can dynamically set inline styles for different elements on a webpage. I have managed to save the element and attribute settings in hidden span elements and retrieve them to display in an alert dialog. However, ...

Straightening the border of the final item

In my Angular project, I'm currently implementing a progress bar with the following code: .bar { --d: 1rem; /* arrow depth */ --gap: 0.3rem; /* arrow thickness, gap */ display: flex; margin-right: var(--d); } .bar-step { flex: 1; ...

The jQuery AJAX variable was successfully sent via a POST request, but unfortunately, I am unable to access it in PHP

Objective: The goal is to trigger a jQuery AJAX request when clicking on a table cell (td). This request will send the corresponding reservationid via a POST method to the same page containing the clicked td. In the PHP file, a query will be executed to f ...