What causes Firefox's CPU to spike to 100% when a slideshow begins that adjusts the width and left coordinates of certain divs?

Seeking Advice

I'm in need of some help with identifying whether the code I'm working on is causing high CPU usage in Firefox or if it's a bug inherent to the browser itself. The situation is getting frustrating, and I've run out of solutions. Hardware acceleration is turned on in Firefox.

Experimentation with the same animation in Chrome, Chromium, and PaleMoon has shown no issues. CPU consumption remains relatively stable in these browsers even after prolonged use. However, once the animation begins in Firefox, CPU usage spikes to over 90% immediately and continues to climb (even when the tab is closed) until hitting 100%. Shutting down Firefox entirely seems to be the only way to alleviate this issue.


Device Specifications

My setup consists of a laptop equipped with an Intel core i7-4700mq processor, 16 GB RAM, a 500 GB SSD, and a 1 TB HDD.

Operating on Kubuntu 16.04 (with the KDE Project Neon repository enabled) and upgraded to kernel version 4.10.10.

Slideshow Description

The slideshow function involves:

  1. Fading in of an image.

This effect is achieved through jQuery fadeIn().

  1. Zooming in of the image.

A CSS3 transition is responsible for scaling the image. A jQuery-assigned class alters the image’s dimensions to 120% width and positions it at -10% from the left.

Eliminating this step results in smooth animation playback without affecting Firefox's CPU utilization.

  1. Fading out the image after a specified time interval.

This action employs jQuery fadeOut(). Once the image-containing div fades out, the zoom-in CSS class assigned to the image is removed.

  1. Fading in a new image while the previous one fades out.

Accomplished using jQuery fadeIn(), adding the zoom-in CSS class to the newly displayed image.


Functional Example

You can view a functional sample of the slideshow on CodePen.


Markup

HTML

<div class="slideshow">
    <div class="slide" id="slide-1">
        <img class="slide-img" id="slide_img-1" data-src="https://upload.wikimedia.org/wikipedia/commons/5/5a/Proboscis_monkey_%28Nasalis_larvatus%29_female_Labuk_Bay.jpg"/>
    </div>
    <div class="slide" id="slide-2">
        <img class="slide-img" id="slide_img-2" data-src="https://upload.wikimedia.org/wikipedia/commons/b/b9/Saffron_finch_%28Sicalis_flaveola%29_male.JPG"/>
    </div>
</div>

CSS

.slideshow{
    width: 100%;
    overflow: hidden;
}
.slide{
    width: 100%;
    overflow: hidden;
    position: absolute;
    text-align: center;
    background-position: center;
    background-repeat: no-repeat;
}
.slide img{
    position: relative;
    margin-left:0px;
    width:100%;
    min-width:100%;
    transition: all 8s;
}
.slide_zoom{
    width: 120%!important;
    margin-left: -10%!important;
}

Javascript

var current=1;
var prev=1;
var counter=0;
var slide_duration=8000;
var transition_duration=700;
var interval;
var width=($(window).width()+200)+'px';
$(function(){
    function slideshow_play(){
    console.log(current);
        $('#slide-'+current).fadeOut(transition_duration,function(){
            $('#slide_img-'+prev).removeClass('slide_zoom');
        });
        prev=current;
        if (current<$('.slide').length){
            current=current+1;
        }
        else{
            current=1;
        }

        $('#slide-'+current).fadeIn(transition_duration, function(){
            $('#slide_img-'+current).addClass('slide_zoom');
        });
    }

    $('.slide').each(function(){
        var img=$(this).find('img');
        var src=$(img).data('src');
        var image=new Image();
        image.src=src;
        image.onload=function(){
            counter+=1;
            img.prop('src', this.src);
            if (counter==($('.slide').length)){
                $('#slide-'+current).fadeIn(500);
                var timeout=window.setTimeout(function(){
                    $('#slide_img-'+current).addClass('slide_zoom');
                    interval=window.setInterval(function(){ slideshow_play(); }, slide_duration);
                },500);
            }
        }
    });
});

Answer №1

Transitioning width and left can impact performance; consider using transform instead.

Check out this updated Codepen and the Stack snippet provided. I replaced width/left with transform: scale(1.2) and transform-origin: center top;

I suggest utilizing CSS animations/transitions whenever possible, such as toggling classes, as they generally offer better performance compared to script-based methods.

var current=1;
var prev=1;
var counter=0;
var slide_duration=8000;
var transition_duration=700;
var interval;
var width=($(window).width()+200)+'px';
$(function(){
    function slideshow_play(){
        console.log(current);
        $('#slide-'+current).fadeOut(transition_duration,function(){
            $('#slide_img-'+prev).removeClass('slide_zoom');
        });
        prev=current;
        if (current<$('.slide').length){
            current=current+1;
        }
        else{
            current=1;
        }

        $('#slide-'+current).fadeIn(transition_duration, function(){
            $('#slide_img-'+current).addClass('slide_zoom');
        });
    }

    $('.slide').each(function(){
        var img=$(this).find('img');
        var src=$(img).data('src');
        var image=new Image();
        image.src=src;
        image.onload=function(){
            counter+=1;
            img.prop('src', this.src);
            if (counter==($('.slide').length)){
                $('#slide-'+current).fadeIn(500);
                var timeout=window.setTimeout(function(){
                    $('#slide_img-'+current).addClass('slide_zoom');
                    interval=window.setInterval(function(){ slideshow_play(); }, slide_duration);
                },500);
            }
        }
    });
});
.slideshow{
    width: 100%;
    overflow: hidden;
}
.slide{
    width: 100%;
    overflow: hidden;
    position: absolute;
    text-align: center;
    background-position: center;
    background-repeat: no-repeat;
}
.slide img{
    position: relative;
    margin-left:0px;
    width:100%;
    min-width:100%;
    transition: all 8s;
    transform-origin: center top;
}
.slide_zoom{
    /*
    width: 120%!important;
    margin-left: -10%!important;
    */
    transform: scale(1.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slideshow">
    <div class="slide" id="slide-1">
        <img class="slide-img" id="slide_img-1" data-src="https://upload.wikimedia.org/wikipedia/commons/5/5a/Proboscis_monkey_%28Nasalis_larvatus%29_female_Labuk_Bay.jpg"/>
    </div>
    <div class="slide" id="slide-2">
        <img class="slide-img" id="slide_img-2" data-src="https://upload.wikimedia.org/wikipedia/commons/b/b9/Saffron_finch_%28Sicalis_flaveola%29_male.JPG"/>
    </div>
</div>

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

Incorporating Only XSD Files into an HTML Input Tag: A Simple Guide

Is there a way to restrict a file input element to only display XSD files? I attempted the following: <input type="file" accept="text/xsd" > Unfortunately, this method is not working as it still allows all file formats to be disp ...

Having trouble applying CSS to multiple classes used in an AJAX call

I'm trying to utilize richfaces for some fast AJAX widgets, but I am encountering difficulties in setting CSS parameters for them. After inspecting the generated code, I found that the elements have the classes "rf-ds" and "rpds". Despite applying st ...

Show a pop-up form when a user focuses on it using React

Hello, I have been looking for a way to create an overlay with a form that appears when a specific input field is clicked. I am trying to achieve this using React. Can someone please advise me on how to accomplish this? Here is my code import React, { Co ...

Unpredictable Dependency Outcomes in Node.js

Encountering a warning while running yarn install where all dependencies are installed, but a specific warning is triggered: Warning: Pattern ["esprima-fb@~3001.0001.0000-dev-harmony-fb"] is attempting to unpack in the same destination "/Users/Me/Librar ...

Node.js and Express constantly face the challenge of Mongoose connecting and disconnecting abruptly

I have been running an Express (Node.js) app connected to MongoDB using Mongoose for a while now. Everything was working smoothly until recently, when it started acting up. It appears that the server is establishing a connection with MongoDB only to disco ...

Tips for confirming schedule accuracy

Trying to determine if a specific time falls between two others is the task at hand. Allow me to illustrate: Presently, it's Thursday, and the time reads 11:39 PM. Establishment X operates from 12:00 AM to 11:59 PM on Thursdays (a regular occurrence ...

What is the best way to convert a string in JavaScript to be case-insensitive?

Can anyone assist me? Challenge: Develop a function called indexOfIgnoreCase which takes in two strings and identifies the first instance of the second string within the first string. This function should be insensitive to letter case. For example, indexO ...

Encountering a problem with Vue when running the node-rdkafka package

I added node-rdkafka to my project using the command npm install node-rdkafka. Then, I included it in my code like this: import Kafka from 'node-rdkafka'; created() { console.log(Kafka.features); } An error occurred when running npm r ...

unable to access POST information

I have encountered an issue with getting a basic AJAX POST to function properly. After facing difficulties with using a jQuery .click, I switched to an onclick method. I am unsure if I am making a glaring mistake or if there could be an issue with Apache s ...

Angular 7 offers seamless synchronization for horizontal scrolling in a unique way

Can anyone provide guidance on how to achieve synchronized horizontal scrolling in an Angular project? I found a solution at the following link, but it uses jQuery code. Synchronized scrolling using jQuery? ...

Maintaining Portrait Lock for Viewport in HTML5/CSS3: A Guide

Can the orientation of a mobile device's view port be locked to portrait mode? I tried searching for a solution on Google, but was unable to find clear instructions on how to achieve this. ...

Managing state with Apollo within a component

Greetings and thank you for taking the time to help me out. I am currently diving into the world of Apollo and React, but it seems like I am struggling with some logic here. On my main page index.js, I have initialized Apollo in the following way: export c ...

@mui/x-date-pickers styling for the DatePicker component

Despite numerous attempts, I have been unsuccessful in styling the @mui/x-date-pickers <DatePicker/> component. I've experimented with various methods such as sx={{}}, style={{}}, makeStyles(), .css with the !important rule, renderInput={(param ...

What is the procedure for transferring the inputted data from an HTML file to its corresponding TS file and subsequently to a different component file?

I have created two components, a login and a home-page. I am attempting to capture user input from the login template, pass it to the login component, and then display it on the home-page template using the home-page component. What is the best approach to ...

I wish for the value of one input field to always mirror the value of another input field

There is a checkbox available for selecting the billing address to be the same as the mailing address. If the checkbox is checked, both values will remain the same, even if one of them is changed. Currently, I have successfully achieved copying the mailing ...

Tips for showcasing cards in a horizontal inline layout

Hello everyone! I'm currently working on creating dynamic cards, but I'm having trouble displaying them inline. I'd like to arrange the cards horizontally with a scroll bar. I want to make them display in horizontal line <div className=& ...

Comparison between Filament Group's loadCSS and AJAX technologies

The loadCSS library developed by Filament Group is widely recognized as the standard for asynchronously loading CSS. Even Google recommends its use. However, instead of using this library, some suggest utilizing ajax to achieve the same result. For example ...

Launching a single modal for multiple posts

I have a collection of posts and for each post, I want the ability to open a modal. However, I am looking for a solution where I can use a single dynamic modal instead of creating multiple modals for each post. Here is the code I currently have: https://j ...

Javascript data table pagination and custom attributes resulting in unexpected truncation of strings

Currently, I have implemented an Ajax functionality in my template to receive a JSON response and dynamically populate a datatable with the elements from that JSON: $('.bicam_tests').click(function(){ $.ajax({ type: 'GET', ...

How can multiple arguments be passed to a function using JQuery's post method?

I can't seem to figure out how to pass multiple arguments to a function using jQuery's post method. It might sound like a silly question, but I'm struggling with it. Currently, my code looks something like this: $.post("<?php echo site_ ...