The sticky navigation bar becomes fixed at the top prematurely

Having a sticky navbar on my Bootstrap-based website, I've implemented the following jQuery code:

    $(document).ready(function () {

        var flag = false;

        function stickNav() {
            $(".navbar-default").affix({
                offset: {
                    top: $('.header-img').height()
                }
            });
            $(".navbar-default").css({ "width": $('.container').width(), "z-index": 1000 });
            $(".navbar-wrapper").css("height", $('.navbar-default').height());
            $(".header").css("height", $('.header-img').height());
            flag = true;

        }

        $(".header img").ready(function () {
            if (!flag)
                window.setInterval(stickNav, 10);
        });

        $(window).resize(function () {
            $(window).off('.affix');
            $('.navbar-default').removeData('bs.affix').removeClass('affix affix-top affix-bottom');
            stickNav();
        });

Furthermore, here is the relevant CSS:

.affix {
    top: 0;
    width: 100%;
}

However, the issue arises as the navigation bar sticks to the top prematurely. You can see this live example at:

Does anyone have a solution for this problem?

Thank you.

Answer №1

After encountering an issue in Chrome (44.0.2403.130 m) when loading the page, I found a simple solution by executing two lines of code in the console. This fixed the scrolling problem, making it work smoothly like in Firefox. To automate this fix, consider adding these lines to the page load function:

$(window).off('.affix');
$('.navbar-default').removeData('bs.affix').removeClass('affix affix-top affix-bottom');

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

Is there a way to fetch JSON data for a JQUERY datatable?

Is there a way to create JSON data for binding JQUERY data table? In my ASP.net web service (.asmx) code, I have the following snippet: <WebMethod()> _ Public Function Getcdata() As String Dim dt As New DataTable() Using con As New SqlConne ...

Decision on how to exchange data (JSON or traditional method)

In my current project, I am developing a user-friendly application that allows users to design their own web interface using various tools. Users can create drag-and-drop elements and I need to store this data in a database once they finalize their desig ...

How to use jQuery to recursively assign numbered classes to groups of div elements

I am dynamically grouping a fixed number of divs from an array (e.g. 4 in each group). The total number of .item divs retrieved from the array is uncertain... I need to recursively assign the same class to these groups of wrapped divs: <div class="wrap ...

Is there a way to enable scrolling on a page without editing the HTML? I have a table inside a div that is overflowing, and I want the page to scroll,

I'm facing an issue with a DIV that is 600px wide and contains a table. Due to long email addresses, the wrapping isn't working properly even after using word-wrap: break-word. I've experimented with changing the width, adding !important, a ...

"Troubleshooting Issue: ASP.NET MVC - Unable to make JQuery call from URL without specifying

My current situation involves a http://localhost:54393/CreateTest/4fwp36 link that is functioning properly. However, when I attempt to call http://localhost:54393/CreateTest/RemoveControlsNew using jQuery ajax, it fails to work and throws an error. Moreove ...

Retrieve the chosen option and dynamically showcase a new HTML block by leveraging the power of Laravel and jQuery

I developed a script that dynamically adds input fields or rows. Within the dynamically added rows, there is a selection box. To enhance this functionality, I created another script so that when an item is selected from the dropdown menu, a new HTML block ...

Expand the column to occupy the remaining height of the row

I've been on the hunt for a solution to this issue, but I haven't had any luck finding one. Various flexbox properties like flex-grow: 1; and align-self: stretch; have been attempted, but none seem to achieve the desired outcome. The goal is to ...

I'm looking for a Visual Studio add-in that can identify and flag any unused CSS classes or IDs within the entire solution

Exactly as the headline suggests. If not, what are some reliable online services you've tried for decluttering oversized stylesheets? ...

Is it possible to set a single Tailwind breakpoint that will automatically apply to all CSS styles below it?

Responsive design in Tailwind is achieved by using the following code snippet: <div className="sm: flex sm: flex-row sm: items-center"></div> It can be cumbersome to constantly include the sm: breakpoint for each CSS rule. I want ...

Embed a video clip into an HTML webpage

I attempted to embed a video using HTML. I followed the player script instructions and added the necessary paths and scripts, but unfortunately, the video is not visible on the webpage. Instead, when clicking on the area where the video should be, it opens ...

How to retrieve an element in jQuery without using the onclick event listener

I'm looking to extract the element id or data attribute of an HTML input element without using the onclick event handler. Here is the code I currently have: <button class="button primary" type="button" onclick="add_poll_answers(30)">Submit</ ...

Ways to create an angularJS $watch function that is able to recognize modifications in styling

I have a rich text box equipped with all the necessary style controls. If I input new text, it saves without any issue. Whenever I modify the content (text) and add styles like color highlighting or bold formatting, the changes are saved successfully. H ...

React.js - jQuery method fails to function

The application utilizes node.js and react.js, specifically the use of react-async. Within a react component, there is a click handler that needs to retrieve the class names - specifically minus_decrement and inline_block. To achieve this, jQuery was integ ...

embedding fashion within offspring component in vue

Looking to apply inline styles to a child component? I am trying to pass properties like height: 200px and overflow-y: scroll. I attempted passing it this way: <Childcomponent style="height: 200px; overflow-y: scroll;" /> but had no luck. ...

Updating dropdown values using another dropdown through AJAX in Node.js: A step-by-step guide

My dilemma involves two dropdown menus: one for selecting the main category and another for choosing a subcategory. The goal is to dynamically populate the subcategory based on the main category selection. In my attempts so far, I have utilized JQUERY and ...

Determining the Number of Form Values Equal to 0 Using jQuery

$("#scorecard").change(function() { var total = 0; $("#scorecard :checked").each( function() { if(parseInt($(this).val()) === 0) { total += 1; } else { total += 0; ...

Click Event for Basic CSS Dropdown Menu

My goal is to develop a straightforward feature where a dropdown menu appears below a specific text field once it is clicked. $(".val").children("div").on("click", function() { alert("CLICK"); }); .container { display: inline-block; } .val { d ...

Encountered JSON array, unable to retrieve certain attributes in AngularJS

I have developed a web service that organizes my information into a list and then converts it to JSON format. Below is the JSON output: { GetEventLTResult: [ { eventID: 1, location: "Place A", type: "Communi ...

Various forms utilizing the same submission function

Is it possible to use the same submit code for multiple forms on a page that contain various products? $('#myform').submit(function() { var queryString = $(this).formSerialize(); alert(queryString); $.ajax ({ t ...

Complete the form to redirect to a new webpage

I'm facing an issue here, wondering how I can submit a form on one URL and another form on a different URL simultaneously... For example: form1.php: <form> <input type="text" name="name" id="name"> <input type="submit" href ...