Make sure the navigation bar is fixed to the top of the page only

Looking for a solution to stick the navigation menu to the top of the screen only when the screen is wider than 782px.

Progress so far: https://jsfiddle.net/ah4ta7xc/1/

Encountering an issue where there is an unintended gap at the top when applying the sticky-menu class.

HTML:

<div id="foo">Logo and other content</div>
<div id="main-menu">Main Site Menu</div>

CSS:

body {
    height: 3000px;
}

#foo {
    height: 50px;
    background-color: #ccc;
}

#main-menu {
    width: 100%; 
    height: 30px; 
    background-color: black;
    color: white;
    text-align: center;
    padding: 10px;
}

.sticky-menu {
    z-index: 10;
    width: 100%;
    position: fixed;
}

jQuery:

$(function(){
    if ($(window).width() > 782) {
        $('#main-menu').addClass('sticky-menu');
    }

    $(window).resize(function () {
        if ($(window).width() > 782) {
            $('#main-menu').addClass('sticky-menu');
        }
        else {
            $('#main-menu').removeClass('sticky-menu');
        }
    });  
});

Answer №1

To maintain a clean stylesheet for the body, consider incorporating the following CSS lines:

margin: 0;
padding: 0;

Answer №2

To implement a sticky navigation bar that activates when the user scrolls to a certain point, you can use the following JavaScript code:

$(window).scroll(function () {
     var scroll = $(this).scrollTop();
     var topDist = $('#main-menu').offset().top;

     if (scroll > topDist) {
         $('#main-menu').addClass('sticky-menu');
     } else {
         $('#main-menu').removeClass('sticky-menu');
     }
});

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

What is the best way to choose the final XHTML <span> tag with a specific class using XPath?

My target XHTML document structure is as follows: <html> <head> </head> <body> <span class="boris"> </span> <span class="boris"> </span> <span class="johnson"> </span> </body> </html> ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

What is the best way to implement an object literal method for making an HTTP GET request to retrieve JSON data?

I'm facing an issue with the HTTP GET method when trying to retrieve JSON data. Although the HTTP GET method is successfully fetching the JSON data, I'm struggling to connect it with the object literal. For better clarity, here's the specif ...

Utilize BotDetect in Angular js for advanced functionality

We are licensed to use BotDetect and now, we want to integrate it with Angular js. After searching on Google, I couldn't find a proper solution but stumbled upon angularjs-captcha, which seemed promising at first but didn't quite help me achieve ...

The input form is experiencing issues with the .change event on the drop down selector, specifically with dynamically generated choices

Currently, I am working on a form that allows users to add or remove up to three vehicles. Each vehicle requires the user to select a year, make, and model. However, I have encountered an issue where if a user selects three vehicles and then decides to cha ...

What is the best way to deactivate all input fields, dropdowns, and text areas within the titlediv_1274837

I have a function that I intended to disable all the input, select, and textarea elements within the titlediv_1274837 div. However, when I execute the function, it does not work as expected. I've tried using both a single jQuery statement and an each ...

Error in the Syntax of Project Image Slider

I'm encountering an issue with a WordPress script called Project Slides. Initially, this script was working fine but suddenly stopped. After investigating in the console, I found the following error: VM138 plupload-image.js?ver=4.2.2:67 Uncaught Err ...

Guide to bringing API information into a Material UI Card

I've been working on a Recipe app that leverages data from the edamame API. I successfully imported Material UI cards into my .js file to pull data from the API and stored it in a const named recipes. However, I'm facing difficulties in getting t ...

Updating the web browser does not display the changes made on the page

Currently diving into the world of Angular and I've successfully cloned the repository. After installing the dependencies using npm, I have the web server up and running smoothly. Accessing the page on localhost:4000 works like a charm. Interestingly ...

What is the best way to send a parameter from jQuery to PHP?

I am trying to create a simple button that, when clicked, will pass a value from jQuery to a PHP file for execution. The idea is that when the button is clicked, it triggers an onclick event which sends the number "1" to the jQuery code. Then, jQuery shoul ...

An issue with type conversion arises in Visual Basic when trying to access data from an HTML

I'm currently working with an HTML table that allows for dynamically adding and deleting rows with text-input's in the cells using JavaScript. Instead of ASP.NET TextBox controls, I am sticking to traditional HTML because I believe rows containin ...

Some of the CSS code in my file is not displaying properly on my ejs file

For some reason, only the CSS styles for my footer are rendering on my ejs file. The rest of my CSS doesn't render at all. I've tried defining the path of my static files to my views directory using the path method: app.use(express.static(path.jo ...

What should I name my Bootstrap Modal ID to ensure AdBlock Plus blocks it?

What specific ID should I use to block my Bootstrap Modal with AdBlock Plus? I want AdBlock Plus to block it for users who have it installed and are using it. I don't want to be overly intrusive by trying to circumvent ABP. The only reason I am using ...

Maintain text area spacing using JavaScript

Could anyone assist me in finding a script that can change line breaks from a user's textarea input into HTML upon form submission to ensure that the formatting is maintained in our notification emails? Currently, I am encountering an issue where fie ...

Sticky sidebar panel featuring a stationary content block

I have a sidebar that is set to position:fixed; and overflow:auto;, causing the scrolling to occur within the fixed element. When the sidebar is activated, the element remains static on the page without any movement. My goal: I am looking to keep the .su ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Experience the auditory bliss with Javascript/Jquery Sound Play

I am creating an Ajax-driven application specifically for our local intranet network. After each response from my Ajax requests, I need to trigger a sound in the client's browser. I plan to store a sound file (mp3/wav) on our web server (Tomcat) for ...

The jQuery half rating by Chris Richards is a game-changer in the world

I've been utilizing the Chris Richards jQuery rating plugin and have found it to be incredibly helpful and user-friendly. However, I require the ability to include half ratings, which the current plugin does not support. Is there a way to modify the p ...

Concentrate on what comes next

Within my JavaScript code, I have the following line: $('input')[$('input').index(this)+9].focus(); I intended for this line to focus on the next element. However, when it executes, I encounter the following error: Error: [$rootSc ...

Adjust the background color of JQuery UI modal dialog overlay in real-time

How can I dynamically change the background color of a page when a modal dialog loads, specifically targeting only one specific modal dialog? Setting the background color with CSS works fine: .ui-widget-overlay { background-color: white; } Here's ...