Is it possible to utilize the addition assignment operator (`+=`) to modify the `transform:rotate('x'deg);` CSS property using

This is the code I have been working on:

#move{
height:70px;
width:70px;
border:2px solid black;
    border-radius:15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="button" value="Move Right" onclick="
$('#move').css({'background-color':'aqua'});
$('#move').css({'position':'absolute',
                               'transition':'500ms ease-in-out',
                               'right':'-=50px',
                               'transform':'rotate(+=5deg)'});
"></input>
        
        <br>
        OBJECT
        <br>
<div id="move"></div>

My query arises here:

Can I achieve both movement and rotation of my div element by making it rotate +5 degrees every time the "MOVE RIGHT" button is clicked? Can an operator like += be added to the rotate value in the CSS property, for example:

'transform': 'rotate(+=5deg)'

Answer №1

It's important to note that the transform property can have varied values, and they are not commutative. For instance, check out these examples:

transform: translateX(100px) rotate(90deg); /* Outcome differs from... */
transform: rotate(90deg) translateX(100px); /* ... this result!       */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(90deg);
}
#box2 {
  transform: rotate(90deg) translateX(100px);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

One might assume that adding a rotation of 45deg would be

transform: translateX(100px) rotate(135deg); /* Accepted modification    */
transform: rotate(135deg) translateX(100px); /* Not recommended option!   */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(135deg);
}
#box2 {
  transform: rotate(135deg) translateX(100px);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

However, this approach only works for the initial example, where rotate was the final transformation function. Typically, the correct sequence should be

transform: translateX(100px) rotate(90deg) rotate(45deg); /* Right way to do it */
transform: rotate(90deg) translateX(100px) rotate(45deg); /* Correct order as well */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(90deg) rotate(45deg);
}
#box2 {
  transform: rotate(90deg) translateX(100px) rotate(45deg);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

You can also apply this custom method:

$.fn.addTransform = function(val) {
  return this.each(function() {
    var tr = $(this).css('transform');
    if(tr === 'none') tr = '';
    $(this).css('transform', tr + ' ' + val);
  });
};

Utilize it like so

$('#move').addTransform('rotate(5deg)');

$.fn.addTransform = function(val) {
  return this.each(function() {
    var tr = $(this).css('transform');
    if(tr === 'none') tr = '';
    $(this).css('transform', tr + ' ' + val);
  });
};
$('input').click(function() {
  $('#move').css({
    backgroundColor: 'aqua',
    position: 'absolute',
    transition: '500ms ease-in-out',
    right: '-=50px'
  }).addTransform('rotate(5deg)');
});
#move {
  height: 70px;
  width: 70px;
  border: 2px solid black;
  border-radius: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" value="Move Right" />
<div id="move"></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

Performing a JSONP request using jQuery's .ajax method to a specific URL including an unidentified

Is there a way to make a jsonp request using $.ajax() to a URL with a unique format like this: http://realtimedata.water.nsw.gov.au/cgi/webservice.server.pl?jsoncallback=printData&{"function":"get_ts_traces","version":1,"params":{}} It's worth n ...

Troubleshooting inactive CSS hover animation

Greetings! I'm facing an issue with a CSS hover animation. Here are two demos I've created: In the first DEMO, the animation works perfectly. However, in the second DEMO, it doesn't seem to be functioning. On the second demo, there are two ...

What could be causing the angular Data table to not display properly?

I am currently exploring Angular Datatables and have a question about re-rendering the datatable after it has been hidden. Can anyone provide guidance on how to achieve this? In my project, I have two components - Parent and Child - that can be hidden or ...

Using react-input-mask together with a child component in React is not compatible

I've been exploring ways to mask a Material UI TextField, and after researching some solutions online, I came across this code snippet: <InputMask mask="(0)999 999 99 99" value={phone} disabled={false} ...

Local working fine but encountering issues on Openshift, specifically with syncing npm package versions across environments (local and global)

As I develop a forum using Angular that connects with Node/Mongo, there are numerous requests required to populate the page with necessary data. While everything functions flawlessly locally, once uploaded to Openshift, the site displays various bugs and f ...

Delving into the intricacies of Promises/A+ and the mechanics of Asynchronicity in Javascript

I am new to JavaScript programming and may have some questions that seem basic. I was recently following a tutorial on Spring Boot and React. The author used a library called "rest" (package.json - "rest": "^1.3.1") and mentioned it is a Promises/A+ based ...

What is the reason for Rich file manager to include filemanager.config.json instead of simply adding an image to the text

I have integrated Rich File Manager with Laravel 5.3.20 using the default configuration provided below: Javascript <script> CKEDITOR.replace( 'textarea', { filebrowserBrowseUrl: '{!! url('gallery/index.html& ...

What is the best way to handle waiting for an HTTP request to complete from a separate component?

https://i.sstatic.net/q4XYB.png An issue arises when calling the GetData function from a component's controller too early. I would like it to wait for the identification process to complete before triggering. During page loading, there is a server c ...

What could be causing the error I'm encountering while attempting to utilize Array.includes as the function for Array.filter in my JavaScript code?

During a recent javascript project, I attempted something like the following code snippet. To my surprise, it did not work and instead produced an error message. const test = [1, 2, 3, 4]; const something = [1, 2, 3, 4, ,5, 6, 7, 8].filter(test.includes) ...

The most efficient method for interacting with externally generated HTML in Rails

For my Rails page help documentation, I utilized the HelpNDoc help authoring tool to generate a folder containing HTML pages and additional folders for JavaScript, images, stylesheets, and libraries. Now I am seeking the most efficient way to integrate thi ...

Track the number of clicks on the pop-up registered in the PHPMyAdmin database

There seems to be an issue with the IP not appearing in the other table within the same database. On the page "recette1.php?id=1" <?php $bdd = new PDO('mysql:host=localhost;dbname=recettes', 'root', 'root'); include("C:/ ...

Is it possible for jQuery to eliminate script tags that are delivered through AJAX requests?

Upon making an AJAX request in my JavaScript code using jQuery, I noticed that the response includes a <script> tag. However, it appears that jQuery is stripping out this particular tag. Could this be considered typical behavior for jQuery or XHR, o ...

Testing if the App properly renders results in an error message: "No element found with the text: Profil"

I am struggling with testing Jest as I lack experience in this area. Specifically, I need to determine whether the App component is being rendered successfully. However, my test cases are failing. Upon checking the console log, I encountered the following ...

"Reacting to click events, all buttons have been successfully updated in ReactJS

When a button is clicked, all buttons are updated simultaneously. However, I am looking to only change the state of the specific button that is clicked. Please refer to the image links and code provided below. import React from 'react'; import & ...

Dealing with h2 text overflowing within a bordered container

I need help creating an h2 element with a double border. Here is my current HTML+CSS snippet: h2.titulo { width: 100%; margin: 10px 0; padding: 10px 0; text-transform: uppercase; font-weight: 400; font-size: 30px; display: block; color: ...

Set the minimum date for the jQuery datepicker

Having trouble with jQuery datepickers for selecting from and to dates? Currently, the code restricts the selection in the second datepicker to dates after the one selected in the first datepicker. However, what if you need to set the to datepicker to allo ...

Dynamic Weight feature in Prestashop allows for automatically adjusting shipping costs

I'm curious about displaying the dynamic weight of each product combination on my product page. Currently, I have something like this: {l s='Weight: ' js=1}{sprintf("%1\$u",$product->weight)}&nbsp{Configuration::get('PS_WEI ...

What is the best way to create an answer label box that validates your response?

I am looking to design a question box label that resembles a search box. In this unique setup, users will have the option to input their answer into the question label. For example, if the user types 'kfuffle' as the answer, they will automatical ...

Tips for converting a large number into a string format in JavaScript

I created this easy loan calculator by following online tutorials and using my basic coding skills. It works well, but I would like to add spaces in the output numbers for readability. For example, instead of "400000", I want it to display as "400 000". ...

Tips for Preventing Page Scrolling When Clicking on a Hashed A Tag in Content with a Fixed

Currently stuck dealing with a challenging problem related to hashed anchor links. Here's a basic representation of the HTML code: <div class="main-wrap"> <header></header> <aside> <nav> <ul> ...