Confused about the sticky navigation, not sure what's happening here

Encountering an issue with my sticky nav. It seems like the JS is failing to apply the designated class. View it live here:

Code snippet:

<script>

$(window).scroll(function() {    
var scroll = $(window).scrollTop();
nav = $('.nav-bar').offset();
if (nav.top <= scroll) {
  $('.nav-bar').toggleClass('sticky-nav');
} else {}
});
</script>

the HTML

<div class="row nav-row">
    <div id="nav-bar" class="medium-12 column nav-bar">
        <ul class="links">
            <li><a href="index.php">home</a></li>
            <li><a href="about.php">about us</a></li>
            <li><a href="pricing.php">pricing</a></li>
            <li><a href="contact.php">contact</a></li>
        </ul>
        <ul class="links-right">
            <li class="has-form">
              <div class="row collapse">
                <div class="large-7 small-7 columns">
                  <input type="text" placeholder="enter text">
                </div>
                <div class="large-3 small-4 pull-2 columns">
                    <a href="#" class="search button expand" style="padding-top: 0.6rem;
                    padding-bottom: 0.6rem;">Search</a>
                </div>
              </div>
            </li>
        </ul>
    </div>
</div>

the CSS

.sticky-nav {
position: fixed;
top:0;
width: 100%;
display: block;
}

Appreciate any feedback on this puzzling issue that myself and friend are facing.

Answer №1

You have mistakenly used the incorrect selector. The .nav-bar selector targets a class, whereas you intend to select the element with the id nav-bar, not the class. To achieve this, utilize #nav-bar as your selector instead.

On a side note unrelated to your query, I'd like to inform you that your usage of toggleClass may result in some unusual behavior; it will continuously toggle the class on and off as the user scrolls.

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

How to implement an instance method within a Typescript class for a Node.js application

I am encountering an issue with a callback function in my Typescript project. The problem arises when I try to implement the same functionality in a Node project using Typescript. It seems that when referencing 'this' in Node, it no longer points ...

What options are available to enable the user to input information into the v-time-picker component?

I am looking for a solution where users can input digits into the vuetify v-time-picker, while still being able to select the time on the clock. <v-col align-self="center"> <v-menu ref="menuTimeStart" v-model="me ...

Ways to trigger an onClick event of one div based on the presence of another div

I'm looking to execute a function when a specific type of button is clicked on my HTML page. I have approximately 10 buttons on the page and I've written the following code to call a function when a button is clicked: $('.divname').ea ...

javascript has a funny way of saying "this is not equal to this" (well,

Sample 1 var Animal = function () { var animal = this; this.makeSound = function() { alert(animal.sound); } } var dog = new Animal(); dog.sound = 'woof'; dog.makeSound(); Sample 2 var Animal = function () { this.makeSound = ...

JavaScript Function that Automatically Redirects User or Updates Text upon Clicking "Submit" Button

Looking to create JavaScript functionality that directs users back to the homepage after submitting their name and email. The process should follow these steps: 1.) User clicks "Request a Brochure..." https://i.sstatic.net/KqH2G.jpg 2.) A window opens in ...

The jQuery POST request is mistakenly sent as a GET

I have been attempting to utilize the code below to send a POST request: $.ajax({ type: "post", url: 'http://api.com/'+apiUsername+'/'+apiBucket+'/elements/add', dataType: 'jsonp', contentType: "appl ...

A round-shaped image with a rectangular box containing text beneath it

Looking for ways to create a responsive design that adapts to different screen sizes? I need my layout to be 2 columns on tablet view and 1 column on mobile view. Here's the CSS code snippet for an image inside a circle: Image within a circle CSS ...

Guidelines for triggering a change event based on a specific condition

When a button is clicked, I execute an Ajax call with the following code snippet: $(document).on('click', '#savenewcategoryforT2', function(event ) { $.ajax({ type: 'GET', url: urllocal + '/OMS/oms/categoryleve ...

Retrieving information from CRM online utilizing the Web API

Recently, I developed a webpage to serve as a survey for end users to provide feedback on the helpdesk technician's performance in resolving tickets. The webpage was constructed using HTML, CSS, JS, and PHP. Currently, the page requires access to two ...

Incorporating a JS file into a ReactJS project: A comprehensive

I am currently working on a project using Reactjs and I am facing an issue with linking my "js" file. I have tried including the "js" file in my project by adding the code to my "app.js" file, but it's not working as expected. The js files that I need ...

Performing a task after a process has finished

Currently, I'm working with node JavaScript and facing an issue where I need to run a new function after a loop has completed. In the code snippet provided below, // loop through objects in data, to process it represents a basic for loop iterating ove ...

Utilize React's debounce feature in conjunction with updating state using

Introduction Let's discuss the popular debounce function provided by lodash. Imagine a scenario where a user rapidly enters values like 1, 12, 123, 1234. The debounce function ensures that only one alert, with the final value 1234, is triggered afte ...

What are some strategies for resolving a parse error in a JSON file?

var personalInfo = { "name": "Belphy Baby", "role": "student", "contacts": { "mobile": "9567166100", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3654535a465e4f070076515b575f5a1855595b"&g ...

Disappearing effect on hover outside of a Div

Let me explain the issue at hand. Currently, I have a layout like this: ----------------- | ------ | | |div1| | | ------ | |div2 | |---------------| In this setup, there is div1 placed within div2. Additionally, there is a ...

Scrolling Container Following Down the Page, Halts at Its Own Bottom (Similar to Twitter)

I am facing an issue and need some help. I am working on creating a three-column page layout where the middle section is for posts, the right section is for links and references (a bit long), and the left section is fixed. My question is: How can I preven ...

Tips for enhancing webpage load times by optimizing CSS and jQuery file handling

As a beginner in HTML and CSS, I am facing slow loading times when testing my pages on a local server. This makes me concerned about the potential loading time on the internet. Currently, I have all the files included in the <head> tag at the top of ...

Having trouble loading JSON data into HTML using jQuery

I'm struggling to create a basic menu using data from a JSON file. I've been attempting to figure out the correct logic for days now, but I seem to have hit a dead end: Here's the approach I've taken: $.ajax({ url: 'data ...

Error encountered when attempting to retrieve data from a JSON object

I'm in the process of extracting some information from a JSON object, but I've encountered a confusing issue. The JSON object structure is as follows: { "status": "success", "data": { "image": null, "video": null, "author": null, "publisher": "M ...

Importing or loading a JavaScript file in Vue.js is a crucial step

I'm in need of some assistance. I've been attempting to load my javascript file and listen for changes on the checkbox when it's clicked to show or hide a password. However, I can't seem to get it to work. I've tried everything I c ...

Expanding the Number of Arguments Sent to a Callback Function

I have a scenario where I am using a method that sends a POST request and then triggers a specific callback function to manage the response: myService.verify(id, verificationCallback); function verificationCallback(err, response) { ... } My query is two ...