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

Steps to finish (refresh) a mongoDB record

Currently, I am dealing with the following scenario: An API request from one service is creating multiple MongoDB documents in a single collection. For example: [ {_id: 1, test1: 2, test: 3}, {_id: 2, test1: 3, test: 4} ] Subsequently, a second service ...

Resolving the active tab problem within Angular 2 tab components

Can anyone assist in resolving the active tab problem within an angular 2 application? Check out the Plunker link I am using JSON data to load tabs and their respective information. The JSON format is quite complex, but I have simplified it here for cla ...

Tips for utilizing a variable from a function in one file within a function in another file

Having trouble using the variable counter from one function in a different file? In the first function, I defined counter without using "var" thinking it would make it a global variable. But for some reason, it doesn't seem to work. Help needed! //fun ...

The local variable within the Angular constructor is not initialized until the ngOnInit() function is invoked

I am encountering difficulties with making backend calls from Angular. In my component, I am fetching the "category" parameter from the URL as shown below: export class ProductsComponent{ productList = [] category = "" $params; $products ...

Tips for deleting a button from a DataTable file upload feature

I currently have Datatable set up to upload images using the code snippet below: { label: "Images:", name: "files[].id", type: "uploadMany", display: function ( fileId, counter ) { re ...

Shake up your background with a random twist

Seeking assistance with customizing the Aerial template from HTML5UP. I am interested in selecting a scrolling background randomly from a pool of images. Can someone guide me on how to achieve this? Presently, the background is defined within a code block ...

Tips for incorporating routes in Polka.js in a way that resembles the functionality of express.Route()

One of the challenges I am facing is trying to import route logic from another file in my project. While using Express.js, this could be done easily with express.Route(). However, when attempting polka.Route(), an error occurs stating that Route doesn&apos ...

"Enhancing User Interaction with AngularJS: Leveraging ng-click and ng

Currently, I have a map with markers that trigger an overlay-div to open when clicked. <div class="map" ng-init="loadall()"> <a ng-click="details.show=!details.show" href="#/dealer/{{marker.id}}" class="marker" style="left:{{marker ...

The clearfix feature is ineffective when using AngularJS

<ul class="dropdown-menu wm_search_div" ng-show="searchDivShow"> <li ng-repeat="user in searchUserList"> <a href="javascript:void(0);" class="wm_clearfix3"> <img ng-src="{{user.faceIcon}}" class="pull-left wm_se ...

Successful AJAX value transfer with no corresponding output

I have implemented a functionality where a user types in a textarea, presses tab, and then an Ajax call retrieves data to populate a select box based on the input from the textarea. <body> <textarea id='test' name="test"></te ...

What is the best way to save the previous state data into a variable so that in case of an error during an API call, we can easily undo the changes that were made

I'm dealing with a toggle button that changes data upon clicking and then triggers an API call to update the databases. However, in case the API returns an error, I want to revert the changes made in the UI. Can anyone guide me on how to achieve this? ...

Production environment does not support Meteor environment variables

I am currently deploying my app using Meteor UP and I have set the environment variables in both the mup.json file and a file called server/lib/env.js where they are stored. Here is how the variables are being accessed: Meteor.startup(function() { // ...

What could be the issue with my JSON file?

I am currently utilizing the jQuery function $.getJson. It is successfully sending the desired data, and the PHP script generating the JSON is functioning properly. However, I am encountering an issue at this stage. Within my $.getJSON code, my intention ...

Parallel with one <div> and subsequently another <div>

I have been challenged by HTML and CSS to embrace humility: Attempting to replicate the following design: Here is my effort: <div> <div style="width:800px;"> <div style="float:left;width:25%;background-color:red;">Facility #: COM ...

Close Tooltip when Ajax content is loaded

While working with jQuery tooltip, I encountered an issue. I have content loaded on a DIV element that is supposed to show a tooltip when I hover over an image map element within this DIV. Everything works fine until I click on one of the image map element ...

Ways to retrieve all elements, including those that are hidden, within a CSS grid

My goal is to retrieve all the "Available Items" using Javascript in one go. However, I am facing an issue where not all of them are visible at once due to the need to manually scroll through a CSS grid. <div class="gridWrapper" data-dojo-attach-point= ...

Are there any methods to achieve smoother font icons on Firefox?

When it comes to using icons on different browsers, I've noticed an issue. Icons appear sharp on Google Chrome and Opera, but when viewed on Firefox they tend to look blurry if the font-size is less than 20px. Strangely enough, once the font size reac ...

Discover the #ID and apply AddClass through the URL in the Navigation Jquery

I am currently in the process of developing a website and I am looking to navigate from one link to another using IDs. Here is an example of what I am trying to achieve: Page Name: index.html <a href= "collection.html#rings">Rings</a> Page N ...

Personalize the checkout form based on the presence of applied discounts in the WooCommerce platform

Is there a way to customize the order notes checkout field when a coupon is applied in Woocommerce? I have attempted this without success. add_action('woocommerce_applied_coupon', 'apply_product_on_coupon'); function apply_product_on_co ...

What is the method for creating a line break in text?

Here is some example code I have: <h3 class="ms-standardheader"> <nobr> Reasons for proposals selected and not selected </nobr> </h3> Now I also have an image to show. The problem is that my text appears too large, so I a ...