The CSS transformation is being overridden

Having an issue where I have two functions that both apply a CSS transform to an element using jQuery, but they end up overwriting each other. These functions are called at different times and have no knowledge of each other.

An example showcasing the problem can be viewed here. When you click the "rotate" link, the translation is overridden and only the rotation is applied. Ideally, I would like both transforms to be retained.

I don't believe this is a bug with jQuery, so how can I modify my code to prevent these transformations from conflicting with each other?


box.css({
    '-ms-transform': 'translate(50px,100px)', /* IE 9 */
    '-webkit-transform': 'translate(50px,100px)', /* Safari */
    'transform': 'translate(50px,100px)'
});

rotateIt.on('click', function() {
    box.css({
        '-ms-transform': 'rotate(45deg)', /* IE 9 */
        '-webkit-transform': 'rotate(45deg)', /* Safari */
        'transform': 'rotate(45deg)'
    });
});

Answer №1

If you find yourself replacing the transform property with another when both classes are applied to the same element, try this workaround.

To incorporate both transform functions - rotate and translate, use the following syntax:

 yourSelector{ transform: rotate(45deg) translate(50px,100px); }

For your specific scenario, consider implementing the following solution:

rotateIt.on('click', function() {
 box.css({
  '-ms-transform': 'rotate(45deg)', /* IE 9 */
  '-webkit-transform': 'rotate(45deg)', /* Safari */
  'transform': 'translate(50px,100px) rotate(45deg)'
 });
});

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

Having difficulty assigning a JSON key value to a variable

I am encountering an issue where I keep receiving '[object HTMLParagraphElement]' instead of the ID value that I expected. The desired output should resemble this: I attempted to store the console.log output in a variable, but my attempts were ...

How can I create a menu navigation in Bootstrap 4 similar to the one on the components website

Greetings! I am not a native English speaker, so please excuse any typos in my message. I am attempting to create a menu that functions exactly like the one on the Bootstrap components page, but with the distinction of appearing on the RIGHT SIDE: http:// ...

Adjust the height of the svg

My page contains an SVG that is impacting the height of its container and causing other elements to move. Is there a way to adjust the height of the SVG? Currently, the SVG is a square but I would prefer it to be a rectangle as the height is too large. &l ...

Prepare JSON data for Flot charts

I need to format JSON data from PHP post using Flowcharts in the following format: [2, 16, 0, 12, 22, 5, -10, 5, 15, 2, 11, 55] To render the charts, I use the following javascript command: _initSparklineChart($('#kt_chart_sales_by_apps_1_2') ...

Issue with stretched links in Bootstrap 5 specifically affecting certain recurring elements on mobile devices

I am facing an issue with a table that has a few hundred rows generated by a recurring PHP script. Utilizing Bootstrap 5, I am using a stretched link to enable the entire row of the table to be clickable. The functionality works perfectly on desktop and ev ...

PHP consistently displays [object Object] following an Ajax request

Having an issue with a form on a web page that needs to be processed before being sent to a PHP script for handling data. The form sends data in the format of a JSON Object named "data": [ { "label": "Some label", "value": "Some value" ...

Using jQuery to manipulate arrays in Twig - Harnessing the power of jQuery to work with Twig arrays

Attempting to organize the select options hierarchically using a bootstrap plugin that requires identifying the "order" of each item. To achieve this, I conducted a database search to retrieve the order of each listed item in the select dropdown, placed t ...

css - aligning text below another text in the center

Looking to style a text in a specific color with a message about another text below it? Want the text centered but not sure how to position it correctly? margin: 0px auto; If this code isn't delivering the desired result and is positioning your text ...

Clear input field after successful submission using jQuery

When the form is submitted correctly, I want to clear the input values. However, a problem arises - if the inputs are not filled in, they get erased. Any help would be greatly appreciated. Here's my code: $(document).ready(function(){ $("#Insert" ...

What is causing my `<nav>` buttons to be left-aligned?

The attempt to center the buttons in the viewport using text-align:center for nav, ul, and li has proved ineffective. The alternative of nav {text-align: left;} was no more successful. CSS Snippets: #container { width: 100%; background-color: bla ...

Enhanced Fancybox Version 2 adjusts iframe width to fit content

I have been attempting to adjust the width of the iframe in fancybox v2 to fit my content properly. However, every time I try, the content appears too large within the iframe and requires horizontal scrolling to view it all. My goal is to see the entire wi ...

Retrieve a document from a specified URL using JQuery's Ajax functionality

I have an HTML page with an input type text field. I want to be able to paste a URL, for example , into this text field and then click a button labeled "download" in order to download the linked file onto my computer. I am looking to implement this funct ...

Design a menu featuring two additional submenus

I'm having trouble with the second sub-menu in this menu. It doesn't open the second sub-menu item. What can I do? <div id='cssmenu'> <ul> <li class='active'><a ...

Retrieve JSON from a JSP page and access the data using AJAX

Is there a way to automatically fill a form in a JSP page based on the field codigo_caso? When this field is blurred, I want to send a JSON-like String to another JSP that retrieves the necessary data to complete the form. I have tested the page and it su ...

Struggling with sending JSON data to ASP.NET MVC2 controller using a basic JQuery $.ajax request. Controller unable to recognize the JSON payload

I've spent hours sorting through various solutions in order to understand the technique for sending data from $.ajax to a parameter in MVC 2's Controller. Here's how far I've come: By the way, this method is effective when using GET, b ...

Replicating a Bootstrap element without transferring all event listeners

Recently, I posted a query on Stack Overflow regarding the cloning of a bootstrap element while excluding the copied event listener. The solution provided was to refrain from passing true to the clone() function. Upon further reflection, I've realize ...

Having trouble getting the jQuery on() method to work with the click event?

My jQuery code snippet is as follows: $(document).ready(function(){ $("div#overlay div#getFBid div#overlayClose").on("click", function(){ console.log("test") }); }); Additionally, here is the HTML code that is loaded using AJAX: <div ...

offspring of offspring in jquery animation remains stationary

I'm experiencing an issue with a jquery animation on containers that are set to 100% width and height. Specifically, the children elements that have position absolute move with the container, but when a child of a child has two instances of position a ...

Summary Table Row Expansion and Contraction Feature in AngularJS

Looking for assistance in creating a table layout using angularJS? I'm having trouble figuring out the most efficient way to achieve this. Any ideas on how I can structure my table based on the following object? Here is the object I want to use: var ...

Display Mailchimp subscription form adjacent to button click on a WordPress website

Seeking assistance with integrating a MailChimp simple subscription form (requires email and submit) next to a button on my Wordpress page. The desired functionality is a straightforward button labeled "Newsletter." When clicked, a small form should conve ...