JavaScript is running smoothly on Chrome, but experiencing issues on IE and Firefox

I have a navigation bar that uses JavaScript code to shrink in size when scrolling down. The code seems poorly written and I would appreciate any help in improving it.

While the functionality works well in Chrome, it fails to activate in Firefox and IE.

I suspect this issue may be due to the height being set in the CSS file, but changing it there is necessary for the design to appear correctly. However, I could be mistaken.

Below is the JavaScript code:

<script type="text/javascript">
    $(document).ready(function(){
        $('#nav-background').data('size','big');
    });
    $(window).scroll(function(){
        var $nav = $('#nav-background');

        if ($('body').scrollTop() > 0) {
            if ($nav.data('size') == 'big') {
                $nav.data('size','small').stop().animate({
                    height:'60px'
                }, 200);
                $("#nav").animate({margin: "17px 0px 0px 0px"}, 200);
                $(".smoothScroll").animate({margin: "17px 0px 0px 0px"}, 200);
            }
        } else {
            if ($nav.data('size') == 'small') {
                $nav.data('size','big').stop().animate({
                    height:'80px'
                }, 200);
                $("#nav").animate({margin: "27px 0px 0px 0px"}, 200);
                $(".smoothScroll").animate({margin: "27px 0px 0px 0px"}, 200);
            }  
        }
    });
</script>

And here is the corresponding CSS:

#nav-background {
    position: fixed;
    width: 100%;
    background-color: #FFFFFF;
    z-index: 1000;
    height:80px;
}
#nav-bar {
    margin: 0 auto;
    width: 90%;
    height:100%;
    max-width:1070px;
}
.smoothScroll {
    text-decoration: none;
    color: inherit;
    position: relative;
    text-align: left;
    width:auto;
    float:left;
    margin-top:27px;
}
.logo-thin {
    font-weight: 300;
    color: #5B5B5B;
    font-family: 'Ubuntu', sans-serif;
    font-size: 22px;
}
/* More styles... */

To view the website, visit:

Answer №1

Adjust

$('body').scrollTop()

to

$(window).scrollTop()

The recommended method for retrieving the scroll position of the window's content (and therefore the body).

It is also advised to encapsulate everything within the

$(document).ready(function(){
//
});

block. This includes binding the scroll event to the navbar resize function.

On a side note, jQuery allows you to achieve the same functionality with the following syntax:

$(function() {
//
});

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

Utilizing jQuery for automatic redirection in place of performing regular AJAX requests

My form validation script using bootstrapvalidator () performs an ajax call upon successful validation (e.g., checkbox checked, not empty). However, I encountered a problem when trying to integrate it with the ICheck plugin (). Instead of executing the aj ...

What is the best way to pass an array to a JavaScript function from a different page?

My website has a static settings page where my JavaScript function uses AJAX to retrieve data from a MySQL table and display it in a table on an HTML document. It's working perfectly, gathering all the necessary data efficiently. Here's the code ...

The website is not displaying correctly

I've been working on building a website using the CakePHP framework, but I've encountered an issue where the site is sometimes not rendering properly. This results in either the CSS not being applied correctly or only partially applied. It's ...

Using Delta encoding to optimize JSON objects

Are there any standard libraries or tools available for calculating and applying differences to JSON documents? I have several large documents that need to be synchronized across a network, and I don't want to resend the entire state each time I sync ...

Naming tests in the Mocha BDD style

Currently, I am utilizing Mocha as the test framework for evaluating my node.js project. My preference is to adopt the BDD style in order to structure my tests and specifications effectively. In the past, I have written tests using cucumber, which also em ...

Adjusting webpack configuration to incorporate an additional entry file

Hey there! I've run into a bit of an issue with React. I've created a NavBar component, saved it in JSX form in 'home.js', and it's rendering perfectly in my index.html. Now, I'm trying to create a "Login" component, but after ...

What is the process of transforming this jQuery script into AngularJS code?

Currently I am facing a situation where I am utilizing Angular1.x for a project. On a specific subpage, there are small clickable images along with some additional content below these images. The requirement is that only the images should be visible init ...

Unveiling the Evasive Final Element in a JavaScript Array

Having a Javascript array named full_range: const range1 = _.range(1, 10, 0.5); const range2 = _.range(10, 100, 5); const range3 = _.range(100, 1000, 50); const range4 = _.range(1000, 10000, 500); const range5 = _.range(10000, 105000, 5000); const full_ran ...

Is there a way to stop the horizontal scroll bar from appearing when a div exceeds the width of my content?

I have come across similar questions, but the answers don't quite fit my scenario. I am dealing with a content width of 940px and I want to use an 'easel' as a background for my slideshow. The easel image extends about 400px beyond the conte ...

Using Typescript to define unions with multiple callback types

While in the process of converting my code to TypeScript, I encountered a dilemma. I created a generic foreach function that can handle arrays and objects, with different types of callbacks for iteration. However, I'm unsure how to specify the type wh ...

Whenever I attempt to execute yarn build within next.js, an error always seems to occur

When attempting to compile my next.js project using the yarn build command, an error consistently occurs: Error: Export encountered errors on following paths: /settings at D:\web3\futnft\frontend\node_modules\next\ ...

JavaScript errors due to miscalculations Incorrect calculations lead

Here is the formula I am using in my Javascript code: total = parseFloat(unit * rate) + parseFloat(rateamount) + parseFloat(((unit * rate) + (rateamount)) * (tax/100)); The values for the variables are as follows: unit = 5, ra ...

Glitch in CSS causing issues with drop-down menu

When hovering over the Gallery, the sub menu list displays next to each other, but I want Sub 1 and Sub 2 to display below each other as a dropdown. How can I improve this code and is there any unnecessary code in it? Here is the picture: HTML: <div ...

How can you modify the mask of an input mask?

I am attempting to change the mask of <p:inputMask/> using JQuery. I have a <p:selectBooleanCheckbox/> that, when checked or unchecked, changes the mask of the inputMask. JavaScript <script type="text/javascript"> function ...

What is the process for creating a register command using discord.js and MongoDB Atlas?

How can I save my Discord member data using a register command? Please provide assistance! bot.js client.on("message", msg => { if (msg.content === "!register, ign:<input from member>, level:<input from member>"){ ...

Sending attachments via email without saving them on the server in PHP Mailer

I need help with my PHP mailer code. I want to send an attachment without saving it on the server. Can someone guide me on how to achieve this? $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->Host = "localh ...

How can I remove the script from Response.Write?

Instead of using Response.Write to test some values in code with an alert box, I tried writing dynamic javascript directly into the page. Even after reverting the code, rebuilding, and clearing all temp data from IE, the alert still pops up. I followed a ...

The $http.get() function in Angular fails to function properly when used in Phonegap DevApp

Trying to retrieve a JSON file in my Phonegap App using angulars $http is causing some issues for me. I have set up this service: cApp.factory('language', function ($http) { return { getLanguageData: function () { return ...

What is the process for interacting with DOM Elements in Node JS?

JAVASCRIPT FILE const path = require('path'); const http = require('http'); const fs = require('fs'); const dir = '../frontend/'; const server = http.createServer((request, respond) => { console.log(reques ...

How can the reset functionality of a function in an Angular 1.5 component's controller be implemented when transitioning to a different state?

I am facing an issue with my Angular 1.5 component and controller that uses the controllerAs syntax. I have written a function to add an extra css class to the component element if a specific html element exists on the page. However, when switching to a di ...