Use jQuery to dynamically append the value of will-change on click or hover interaction

According to the information provided in this source, it is recommended not to permanently store the style "will-change: transform;" in a CSS file, but instead add it dynamically using JavaScript.

The author also presents an example script:

var el = document.getElementById('element');

// Apply will-change when the element is hovered
el.addEventListener('mouseenter', hintBrowser);
el.addEventListener('animationEnd', removeHint);

function hintBrowser() {
  // Define the properties that are likely to change during animation keyframes
  this.style.willChange = 'transform, opacity';
}

function removeHint() {
  this.style.willChange = 'auto';
}

I am wondering if I can implement this script and if it would be beneficial for me?

$('.my_class')    
.mouseenter(function() {
$(this).css("will-change", "transform");
})
.mouseleave(function() {
$(this).removeAttr("style");
});

I welcome any suggestions on how to effectively utilize the "will-change" style property.

Answer №1

This particular paragraph in the linked article deserves careful attention (emphasis added by me)

Allow enough time for it to take effect. This feature serves as a way for authors to inform the user-agent about properties that are likely to change in advance. By doing so, the browser can implement any necessary preemptive optimizations for the property change prior to its actual occurrence. Therefore, it is crucial to allocate sufficient time for the browser to carry out these optimizations. Anticipate changes slightly beforehand and set will-change accordingly.

Hence, if you intend to create an effect on hover, applying the will-change property during hover will not yield any noticeable difference

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

Unable to access component data while inside a v-for loop scope

I recently started using Vue and I'm having trouble accessing my component data within a v-for loop. After implementing the code below, I encountered this error message. TypeError: Cannot read property 'whatever' of undefined at eva ...

FIREBASE - ReferenceError: Authorization cannot be accessed until initialized

Currently, I am in the process of learning about Auth with Firebase using NextJS. I have been trying to grasp the concept by referring to multiple sources such as articles and YouTube videos, but I have encountered an error that is hindering my progress. ...

Reversing the sequence of code in JavaScript - react native

I'm currently working on tracking the number of times a button is pressed within one second. For the most part, it's functioning correctly as it tracks and displays the count. The issue arises when it displays the button press count from the pr ...

Disabling the ability to edit the rightmost portion of an input number field

I am looking for something similar to this: https://i.stack.imgur.com/ZMoNf.jpg In this case, the % sign will be shown in the input field by default and cannot be changed or removed. The user is only able to modify the number to the left of the % sign. P ...

Issue with Animation not functioning properly when InterstitialAd is displayed

Ever since I was in grade 7, I have been passionate about building games and apps. Although I learned some XML back then, I lost my ambition over the years due to life's distractions. However, two weeks ago, I decided to teach myself Java and created ...

Establishing specific categories for a universal element

I have been working on creating an input component that functions as a custom select for enums in my application. I have tried defining them for different types using concise one-liners but have run into various typing issues. Here is what I have so far: ...

Ensuring Valid Submission: Utilizing Jquery for a Straightforward Form Submission with 'Alajax'

After searching for a straightforward JQuery plugin to streamline the process of submitting forms via Ajax, I stumbled upon Alajax. I found it quite useful as it seamlessly integrates into standard HTML forms and handles all the necessary tasks. However, I ...

Develop a custom time input mask in AngularJS controller

In my AngularJS controller, I have the following code snippet: $scope.detailConfig = [{ title: $filter('translate')('bundle.app.HORA_MINUTO_INICIAL_DESCONSIDERAR'), property: 'faixaHorariaInicial', type: ' ...

Is there a way to dynamically import several CSS files in React Native depending on the data stored in this.state?

How can I dynamically import multiple CSS files in a React Native project based on language? import React, { Component } from "react"; if(this.state.language === "he"){ import styles from "./he"; }else{ import styles from "./en"; } I attempted the ab ...

jqGrid inline multiple search dropdown selection

I am currently working on a project that requires 3 select dropdowns like the example shown in this search Select Dropdowns. Below is the code I have: $(document).ready(function () { var MgroupFilter = ":[All];Starter:Starter;Controller:Controller;Finis ...

Redirecting to a different page using Jquery

Is there a way to update the status without being redirected to base_url().'/Admin/Jobs/' every time? Code: <?php if ($key['status'] === '1'): ?> <?php echo form_open('Admin/update_job_status'); ?> < ...

Can we rely on a specific sequence for resolving function arguments?

Imagine a scenario where the following code snippet exists: function write3(a, b, c) { document.write(a + " " + b + " " + c); } var arr = [1, 2, 3]; var i = 0; write3(arr[i++], arr[i++], arr[i++]); The expected outcome is 1 2 3 when running this co ...

Tips for obtaining an ID from a dropdown menu and establishing a default value at the top

My code snippet is currently displaying null for the card_id, but I want to ensure that the id and is_default card are displayed on top. How can I achieve this? Whenever I use ng-click, the result displayed is as shown below. My goal is to obtain the selec ...

steps for extracting ajax page content with php

I am interested in retrieving odds from a specific website: The odds on this site are loaded dynamically through ajax, and the source page does not display them directly. I am curious if there is a method to extract this content using PHP? ...

JavaScript allows for inserting one HTML tag into another by using the `appendChild()` method. This method

My goal is to insert a <div id="all_content"> element into the <sector id="all_field"> element using Javascript <section id="all_field"></section> <div id="all_content"> <h1>---&nbsp;&nbsp;Meeting Room Booki ...

I am struggling with clicking on Bootstrap's pagination using AngularJS

I'm still getting the hang of Angularjs. I managed to set up a pagination system, but for some reason, I can't seem to interact with it when I run my project. Take a look at this screenshot that illustrates my issue: https://drive.google.com/fil ...

What is the best way to transfer information from my route handler to my socket.io function?

Wondering how to pass data from a route handler to a socket.io function? Check out the code snippet below: // Game app.get('/game', (req, res) => { const cookies = req.cookies; const currentUser = cookies['current_user']; ...

Tips for fading out two elements after completing a drag and drop action

Visit this Codepen for more examples - Codepen I have been developing a drag and drop feature in my application. Once the item is dropped, it transitions from red to green and fades out smoothly. The droppable element behind the draggable should also fad ...

Having trouble getting the Javascript dropdown script to work on a Wordpress page?

Hello all, I've been experimenting with creating a simple dropdown menu for my Wordpress website. The idea is that when a user clicks on a specific text, a dropdown menu with various links will appear. I followed the guidance from W3 Schools, but unfo ...

jQuery droppable: Encounter of an unexpected TypeError: undefined lacks the characteristics of a function

I'm trying to implement drag and drop functionality on my website. However, I am encountering an error message in the console that says: Uncaught TypeError: undefined is not a function ... presentation-categories.js?version=1:23. The error occurs at ...