Modifying the marginTop attribute using JavaScript causes unexpected outcomes in FireFox

Issue at Hand: I am facing a challenge with adjusting the margin-top property through JavaScript. While it works smoothly in Chrome and Safari, Firefox seems to be causing some trouble. Instead of positioning the element above the viewport and gradually bringing it down until it reaches the desired location, Firefox places it way below the target position, then moves it upwards and downwards, resembling the intended animation.

Disabling JavaScript and inputting the value directly into CSS results in the correct positioning, indicating that the problem may lie within the JavaScript code.

Situation Overview: I am endeavoring to craft a basic animated logo using JavaScript. The aim is for the logo to descend from outside the browser window into its designated spot. The logo itself corresponds to the HTML element

<img id="site_logo" src="logo.png" />

JavaScript Implementation:

var logoValue = -400;

function onLoad(){  
    // ...
    logoStarter();
}

function logoStarter(){
    var site_logo = document.getElementById("site_logo");
    site_logo.style.marginTop = logoValue +"px"; 
    logoAnimationDown();
}

function logoAnimationDown(){
    if(logoValue <= 20){    
        console.log(logoValue);
        logoValue += 17;

        var site_logo = document.getElementById("site_logo");           
        site_logo.style.marginTop = logoValue +"px";            

        setTimeout('logoAnimationDown()', 30);
        }
    else{
        setTimeout('logoAnimationUp()', 30);
        }
}   

function logoAnimationUp(){
    if(logoValue > 0){
        console.log(logoValue);
        logoValue -= 10;

        var site_logo = document.getElementById("site_logo");           
        site_logo.style.marginTop = logoValue + "px";

        setTimeout('logoAnimationUp()', 30);
        }
}   

Answer №1

When using FF, ensure that you only declare your setTimeout functions once to avoid them being run multiple times. Here is the correct way to use them:

setTimeout(logoAnimationDown, 30);
setTimeout(logoAnimationUp, 30);

If you mistakenly use a string with quotes like this setTimeout('doFunction()',30) to assign the function, FF interprets it as eval('doFunction()'), causing the function to be executed immediately and then again after the specified delay time.

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

Encountering an error with jQuery being undefined while utilizing an Immediately Inv

I'm looking to add a Live Search feature to a webpage that will filter matches from an array of objects when a search term is entered in the search box. However, I am encountering an issue while iterating through the array items using a forEach loop ...

A guide on invoking a function within the same JavaScript file

Currently, I am in the process of developing Automation scripts using Protractor. Within my JavaScript file, which I have named PlatformviewCnOPage, I have created a function called searchCapabilityOfferingsName. I am facing a challenge where I need to ca ...

The implementation of `$('tr:even')` in jQuery is failing to generate alternative row colors

My jQuery code is not behaving as expected. When using the following code, the entire table is rendering with a grey background instead of alternating rows: $('#tenantsTbl tr:even').css('background-color','grey'); ...

Exploring a multitude of data within a hefty json log document using node.js

I am dealing with a JSON file named sensorlogs.json that contains data from different sensors transmitting at varying frequencies. The timestamps in the file are not in order and one sensor may have missing entries. The goal is to analyze each sensor&apos ...

Unnecessary scrollbar appears in the Simple Material UI dialog demonstration

My Material UI dialog consists of a grid with a scrollbar that allows scrolling even though the content fits on the screen. <Dialog open={isOpen} onClose={() => changeIsOpen(false)}> <DialogContent> <Grid containe ...

Extracting a specific range of values from a JSON object

Can anyone help me with filtering a range of values from a json object? Here is the range data I am working with: const contractAmountRange = [ { id: '1', min: 0, max: 100000, description: 'Less than $100,000' }, { id: ...

Reacting with Angulatory: Response heads are not being transferred in applications

I'm facing an issue where the cookie containing my authentication token doesn't get passed along with my requests. After authenticating in Chrome, the response sets the cookie correctly, but it's not included in subsequent requests. This is ...

Issue with retrieving elements by tag name

I'm currently working on a project where I need to parse an HTML file and display specific parts on the webpage. I've managed to read in the file using AJAX and requestData.js file successfully, as I can alert out the whole page using the html va ...

What is the best way to incorporate next and previous buttons into a jQuery slider using JavaScript?

I recently watched a tutorial on YouTube about creating a slider using jQuery (check it out here). The tutorial didn't include Previous and Next Buttons. I'm curious how to add Previous and Next Buttons to the Jquery Slider. Below is the code ...

Discovering the true width of an element using JavaScript

Hey there, I'm currently attempting to determine the exact width of a specific element and display an alert that shows this width. The code I am using is as follows: HTML Element <table cellspacing="1" cellpadding="5" style=";width:975px;border: ...

Showing error messages in Angular when a form is submitted and found to be invalid

My form currently displays an error message under each field if left empty or invalid. However, I want to customize the behavior of the submit button when the form is invalid. <form #projectForm="ngForm" (ngSubmit)="onSubmit()"> ...

Developing an Angular directive that accepts a function parameter using TypeScript

I've been attempting to integrate an Angular function parameter into a TypeScript directive, but it seems like my syntax is off. Here's an example of the directive code I'm working with: export class CustomDirective implements ng.IDirective ...

Creating duplicates of a parent mesh in THREE.js with its children and additional materials

I am inquiring about a cloning issue I am having with a mesh that has a parent and 4 children, each with different materials. When I clone the parent mesh using this code: let result = cloudObjects.sideCloudGeometry[texture].clone(); The cloned mesh look ...

What is the best approach for filtering a nested array in this scenario?

Here is the response I am getting: let m = [ { name: 'Summary', subListExpanded: false, subList: [ ] }, { name: 'Upload', subListExpanded: false, subList: [ ...

Managing values in Vue using v-model can sometimes lead to inconsistencies in value increment

When I try to increment or decrement the counter, my input shows a different value than what is in this.count. For example, when I increment the value, the input shows '3' but this.count === 2. Vue.component('product-item', { data: func ...

generate an array composed of promises to be used with Promise.all

Currently, I am working with an array of file paths and my goal is to read all the files inside a Promise.all function, followed by carrying out several tasks. var files = ["./file1.txt", "./file2.txt"] Promise.all(files.forEach(file=>{ /* read file ...

Can we spice up the button with some animation while waiting for the ajax call to complete? Just a little something to

My HTML page features a button that triggers an AJAX call when clicked. During the call, the text on the button is replaced with animated dots to indicate progress. However, there is an issue where sometimes the call takes several seconds, while other time ...

Setting a JavaScript Date object in data- attributes: A tutorial

Utilizing google chart for plotting a graph in my Rails application. The data used for the chart is dynamically generated within a model. Company#my_chart_data def my_chart_data [ ['date', 'value1', 'value2'], ...

Attempting to modify the CSS properties of a specific div element by triggering a click event on an external link using jQuery's

After manually testing out the CSS and confirming that applying display: none; achieves the desired effect, I now want to use jQuery to make this happen by clicking a link on the page. This will be my first time altering CSS through jQuery, so let me e ...

What is the best way to increase the value of a variable using jQuery?

As I work on adding dates to a slider, I find myself needing to increment the values with each click. Initially, I start with the year - 2. $('#adddates').click(function() { var year = 2; $("#slider").dateRangeSlider({ bounds: { ...